http://wenku.baidu.com/view/0803b63610661ed9ad51f32b.html
一、编写Linux下socket协议TCP的ClientServer程序
客户端,client:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/stat.h> #include<arpa/inet.h> #define MAXBUF 256 int main() { int ssock; int clen; struct sockaddr_in server_addr; char buf[MAXBUF]; if((ssock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){ perror("socket error:"); exit(1); } clen = sizeof(server_addr); memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family =AF_INET; server_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); server_addr.sin_port =htons(7758); if(connect(ssock,(struct sockaddr *)&server_addr,clen)<0){ perror("connect error:"); exit(1); } memset(buf,0,MAXBUF); if(read(ssock,buf,MAXBUF)<=0) { perror("read error:"); exit(1); } close(ssock); printf("\nread: %s\n",buf); return 0; }
服务端,server:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/stat.h> #include<arpa/inet.h> #define MAXBUF 256 int main() { int clen; int ssock,csock; struct sockaddr_in client_addr,server_addr; char buf[MAXBUF]; if((ssock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){ perror("socket error:"); exit(1); } clen = sizeof(client_addr); memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family =AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(7758); if(bind(ssock,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){ perror("bind error"); exit(1); } if(listen(ssock,8)<0){ perror("listen error:"); exit(1); } while(1){ csock=accept(ssock,(struct sockaddr *)&client_addr,&clen); if(write(csock,buf, MAXBUF)<=0) perror("write error:"); close(csock); } return 0; }
二、编写Linux下的socket协议UDP Client/Server程序
客户端,client:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/stat.h> #include<arpa/inet.h> #define MAXBUF 256 int main() { int ssock; int clen; struct sockaddr_in client_addr,server_addr; char buf[MAXBUF]; strcpy(buf,"I miss you already!\n"); if((ssock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0){ perror("socket error:\n"); exit(1); } memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family =AF_INET; server_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); server_addr.sin_port=htons(7758); sendto(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&server_addr,sizeof(server_addr)); printf("Send message:%s\n",buf); clen=sizeof(client_addr); recvfrom(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&server_addr,&clen); printf("Receive message:%s\n",buf); close(ssock); return 0; }
服务端,server:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/stat.h> #include<arpa/inet.h> #define MAXBUF 256 int main() { int ssock; int clen; struct sockaddr_in client_addr,server_addr; char buf[MAXBUF]; if((ssock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0){ perror("socket error:"); exit(1); } memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(7758); if(bind(ssock,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){ perror("bind error:"); exit(1); } clen=sizeof(client_addr); recvfrom(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&client_addr,&clen); strcpy(buf,"I miss you"); sendto(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&client_addr ,sizeof(client_addr)); close(ssock); return 0; }