參考自 21天學通linux c編程
socket編程可參考:
socket編程:
http://blog.csdn.net/fengyv/article/details/659980
http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html
http://www.kuqin.com/networkprog/20080512/8361.html
網路位元組順序以及轉換函式
http://blog.sina.com.cn/s/blog_4ad7c25401019qqb.html
建立socket
綁定連接埠bind
用戶端:發送連結請求connect
伺服器:監聽 listen
伺服器:接受連結 accept
發送資料send write
接受資料recv read
綁定函數執行個體
#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include <stdlib.h>#include <strings.h>#include <stdio.h>#define PORT 5678main(){ int sockfd; struct sockaddr_in addr; int addr_len = sizeof(struct sockaddr_in); if ((sockfd = socket(AF_INET,SOCK_STREAM,0))<0) //ipv4,tcp { perror("socket"); exit(1); } else { printf("socket created \n"); printf("socked id: %d \n",sockfd); } bzero(&addr,sizeof(addr)); addr.sin_family =AF_INET; //tcp addr.sin_port = htons(PORT); //port addr.sin_addr.s_addr = htonl(INADDR_ANY); //local ip if(bind(sockfd,(struct sockaddr*) &addr,sizeof(addr))<0) { perror("connect"); exit(1); } else { printf("connected.\n"); printf("local port:%d\n",PORT); }close(sockfd);return 0;}
read函數執行個體 編譯下面代碼並執行 開啟瀏覽器輸入自己的ip:7788/ 斷行符號 就能在終端看到讀到的資訊
#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<strings.h>#include <stdio.h>#include <stdlib.h>#define PORT 6677main(){ int sockfd,newsockfd,fd; struct sockaddr_in addr; int addr_len = sizeof(struct sockaddr_in); fd_set myreadfds; char msgbuffer[256]; char msg[] ="This is the message from server.Connected.\n"; if ((sockfd = socket(AF_INET,SOCK_STREAM,0))<0) { perror("socket"); exit(1); } else { printf("socket created .\n"); printf("socked id: %d \n",sockfd); } bzero(&addr,sizeof(addr)); addr.sin_family =AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sockfd,(struct sockaddr*) &addr,sizeof(addr))<0) { perror("connect"); exit(1); } else { printf("connected.\n"); printf("local port:%d\n",PORT); } if(listen(sockfd,3)<0) { perror("listen"); exit(1); } else { printf("listenning......\n"); } if((newsockfd = accept (sockfd,(struct sockaddr*) &addr,&addr_len))<0) { perror("accept"); } else { printf("cnnect from %s\n",inet_ntoa(addr.sin_addr)); if(read(newsockfd,msgbuffer,sizeof(msgbuffer))<=0) { perror("accept"); } else { printf("message:\n%s \n",msgbuffer); } }}
綜合執行個體
執行個體1:
程式說明
本使用tcp協議進行通訊,服務端進行監聽,在收到用戶端的串連後,發送資料給用戶端;用戶端在接受到資料後列印出來,然後關閉。
1、client.c
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int cfd;
int recbytes;
int sin_size;
char buffer[1024]={0};
struct sockaddr_in s_add,c_add;
unsigned short portnum=0x8888;
printf("Hello,welcome to client !\r\n");
cfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == cfd)
{
printf("socket fail ! \r\n");
return -1;
}
printf("socket ok !\r\n");
bzero(&s_add,sizeof(struct sockaddr_in));
s_add.sin_family=AF_INET;
s_add.sin_addr.s_addr= inet_addr("192.168.1.2");
s_add.sin_port=htons(portnum);
printf("s_addr = %#x ,port : %#x\r\n",s_add.sin_addr.s_addr,s_add.sin_port);
if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
{
printf("connect fail !\r\n");
return -1;
}
printf("connect ok !\r\n");
if(-1 == (recbytes = read(cfd,buffer,1024)))
{
printf("read data fail !\r\n");
return -1;
}
printf("read ok\r\nREC:\r\n");
buffer[recbytes]='\0';
printf("%s\r\n",buffer);
getchar();
close(cfd);
return 0;
}
2、server.c
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int sfp,nfp;
struct sockaddr_in s_add,c_add;
int sin_size;
unsigned short portnum=0x8888;
printf("Hello,welcome to my server !\r\n");
sfp = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sfp)
{
printf("socket fail ! \r\n");
return -1;
}
printf("socket ok !\r\n");
bzero(&s_add,sizeof(struct sockaddr_in));
s_add.sin_family=AF_INET;
s_add.sin_addr.s_addr=htonl(INADDR_ANY);
s_add.sin_port=htons(portnum);
if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
{
printf("bind fail !\r\n");
return -1;
}
printf("bind ok !\r\n");
if(-1 == listen(sfp,5))
{
printf("listen fail !\r\n");
return -1;
}
printf("listen ok\r\n");
while(1)
{
sin_size = sizeof(struct sockaddr_in);
nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
if(-1 == nfp)
{
printf("accept fail !\r\n");
return -1;
}
printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
if(-1 == write(nfp,"hello,welcome to my server \r\n",32))
{
printf("write fail!\r\n");
return -1;
}
printf("write ok!\r\n");
close(nfp);
}
close(sfp);
return 0;
}
在cygwin下,使用gcc命令編譯如下:
gcc -o server server.c
gcc -o client client.c
然後運行程式:
./server
./client
server執行效果如下:
Hello,welcome to my server !
socket ok !
bind ok !
listen ok
accept ok!
Server start get connect from 0xc0a80102 : 0xc927
write ok!
client執行效果如下:
Hello,welcome to client !
socket ok !
s_addr = 0x201a8c0 ,port : 0x8888
connect ok !
read ok
REC:
hello,welcome to my server
本文歡迎轉載,轉載請註明出處和作者
出處:http://blog.sina.com.cn/staratsky
作者:流星
執行個體2:
用戶端代碼
#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/socket.h>#include <strings.h>#define PORT 5678#define REMOTE_IP "127.0.0.1"int main(int argc,char *argv[]){ int s ; struct sockaddr_in addr ; char mybuffer[256]; if( (s=socket(AF_INET,SOCK_STREAM,0))<0 ) { perror("socket"); exit(1); } else { printf("socket created .\n"); printf("socked id: %d \n",s);} bzero(&addr,sizeof(addr)); addr.sin_family =AF_INET; addr.sin_port=htons(PORT); addr.sin_addr.s_addr=inet_addr(REMOTE_IP); if(connect(s,(struct sockaddr*) &addr,sizeof(addr))<0) { perror("connect"); exit(1); } else { printf("connected ok!\n"); printf("remote ip:%s\n",REMOTE_IP); printf("remote port:%d\n",PORT); } recv(s ,mybuffer,sizeof(mybuffer),0); printf("%s\n",mybuffer); while(1) { bzero(mybuffer,sizeof(mybuffer)); read(STDIN_FILENO,mybuffer,sizeof(mybuffer)); if(send(s,mybuffer,sizeof(mybuffer),0)<0) { perror("send"); exit(1); } else { bzero(mybuffer,sizeof(mybuffer)); recv(s ,mybuffer,sizeof(mybuffer),0); printf("received:%s\n",mybuffer); } }}
伺服器代碼
#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#define PORT 5678#define MAX 10main(){ int sockfd,newsockfd,is_connected[MAX],fd; struct sockaddr_in addr; int addr_len = sizeof(struct sockaddr_in); fd_set myreadfds; char msgbuffer[256]; char msg[] ="This is the message from server.Connected.\n"; if ((sockfd = socket(AF_INET,SOCK_STREAM,0))<0) { perror("socket"); exit(1); } else { printf("socket created .\n"); printf("socked id: %d \n",sockfd); } bzero(&addr,sizeof(addr)); addr.sin_family =AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sockfd,(struct sockaddr*) &addr,sizeof(addr))<0) { perror("connect"); exit(1); } else { printf("connected.\n"); printf("local port:%d\n",PORT); } if(listen(sockfd,3)<0) { perror("listen"); exit(1); } else { printf("listenning......\n"); } for(fd=0;fd<MAX;fd++) { is_connected[fd]=0;/*設定所有*/ } while(1) { FD_ZERO(&myreadfds); FD_SET(sockfd,&myreadfds); for(fd=0;fd<MAX;fd++) { if(is_connected[fd]) { FD_SET(fd,&myreadfds); } } if(!select(MAX,&myreadfds,NULL,NULL,NULL)) { continue; } for(fd=0;fd<MAX;fd++) { if(FD_ISSET(fd,&myreadfds)) { if(sockfd==fd) { if((newsockfd = accept (sockfd,&addr,&addr_len))<0) { perror("accept"); } write(newsockfd,msg,sizeof(msg)); is_connected[newsockfd] =1; printf("cnnect from %s\n",inet_ntoa(addr.sin_addr)); } else { bzero(msgbuffer,sizeof(msgbuffer)); if(read(fd,msgbuffer,sizeof(msgbuffer))<=0) { printf("connect closed.\n"); is_connected[fd]=0; close(fd); } else { write(fd,msgbuffer,sizeof(msgbuffer)); printf("message:%s \n",msgbuffer); } } } } }}