TCP服务器端
/* * ===================================================================================== * * Filename: server.c * * Description: * * Version: 1.0 * Created: 2012年02月20日 13时11分25秒 * Revision: none * Compiler: gcc * * Author: MaZheng (blog.csdn.net/mazheng1989), mazheng19891019@gmail.com * Company: Dalian University Of Technology * * ===================================================================================== */ #include <stdio.h> #include <strings.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define PORT 1234 /* */ #define BACKLOG 1 /* */ int main() { int listenfd,connectfd;//socket descriptors struct sockaddr_in server;//server's address information struct sockaddr_in client;//client's address information socklen_t addrlen; /*crate TCP socket*/ if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1) { //handle exception perror("socket() error."); exit(1); } /*set socket option*/ int opt=SO_REUSEADDR; setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(PORT); server.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(listenfd,(struct sockaddr *)&server,sizeof(server))==-1) { /*handle exception*/ perror("Bind() error."); exit(1); } if(listen(listenfd,BACKLOG)==-1) { perror("listen() error.\n"); exit(1); } addrlen=sizeof(client); if((connectfd=accept(listenfd,(struct sockaddr *)&client,&addrlen))==-1) { perror("accept() error.\n"); exit(1); } //printf client's IP and port printf("You got a connection from client's ip is %s,port is %d\n",inet_ntoa(client.sin_addr),htons(client.sin_port)); send(connectfd,"Welcome!\n",9,0); close(connectfd);//close connectfd close(listenfd);//close listenfd return 0; } 运行命令:$:gcc tcpserver.c -o tcpserver.out$:/tcpserver.out
TCP客户端
/* * ===================================================================================== * * Filename: client.c * * Description: * * Version: 1.0 * Created: 2012年02月20日 13时30分02秒 * Revision: none * Compiler: gcc * * Author: MaZheng (blog.csdn.net/mazheng1989), mazheng19891019@gmail.com * Company: Dalian University Of Technology * * ===================================================================================== */ #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <strings.h> #include <unistd.h> #include <netinet/in.h> #include <netdb.h> #define PORT 1234 /* */ #define MAXDATASIZE 100 /* */ int main(int argc,char **argv) { int sockfd,num;//files descriptors char buf[MAXDATASIZE];//buf will store received text struct hostent *he ;//structure that will get information about remote host struct sockaddr_in server; if(argc !=2) { printf("Usage: %s <IP Address>\n",argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { printf("gethostbyname() error.\n"); exit(1); } if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { printf("socket() error.\n"); exit(1); } bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(PORT); server.sin_addr=*((struct in_addr *)he->h_addr); if(connect(sockfd,(struct sockaddr *)&server,sizeof(server))==-1) { printf("connect() error.\n"); exit(1); } if((num=recv(sockfd,buf,MAXDATASIZE,0))==-1) { printf("recv() error.\n"); exit(1); } buf[num-1]='\0'; printf("server message: %s\n",buf); close(sockfd); return 0; }运行命令:$:gcc tcpclient.c -o tcpclient.out
$:./tcpclient.out 127.0.0.1
UDP 服务器端
/* * ===================================================================================== * * Filename: udpserver.c * * Description: * * Version: 1.0 * Created: 2012年02月20日 16时44分36秒 * Revision: none * Compiler: gcc * * Author: MaZheng (blog.csdn.net/mazheng1989), mazheng19891019@gmail.com * Company: Dalian University Of Technology * * ===================================================================================== */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #define PORT 1234 /* */ #define MAXDATASIZE 100 /* */ int main() { int sockfd; struct sockaddr_in server;//server's address information struct sockaddr_in client;//client's address information socklen_t len; int num; char buf[MAXDATASIZE];//buffer for message /*creating udp socket */ if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) { perror("creating socket failed."); exit(1); } int opt=SO_REUSEADDR; setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(PORT); server.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(sockfd,(struct sockaddr *)&server,sizeof(server))==-1) { perror("Bind() error."); exit(1); } len=sizeof(client); while(1) { num=recvfrom(sockfd,buf,MAXDATASIZE,0,(struct sockaddr *)&client,&len); if(num<0) { perror("recvfrom() error.\n"); exit(1); } buf[num]='\0'; printf("You got a message <%s> from client.\nIt's ip is %s ,port is %d.\n",buf,inet_ntoa(client.sin_addr),htons(client.sin_port)); char data[8]="Welcome"; num=sendto(sockfd,data,MAXDATASIZE,0,(struct sockaddr *)&client,sizeof(client)); printf("num==%d\n",num); if(1||!strcmp(buf,"bye")) { break; } } close(sockfd); return 0; }运行命令:$:gcc udpserver.c -o udpserver.out
$:./udpserver.out
UDP客户端
/* * ===================================================================================== * * Filename: udpclient.c * * Description: * * Version: 1.0 * Created: 2012年02月20日 16时57分37秒 * Revision: none * Compiler: gcc * * Author: MaZheng (blog.csdn.net/mazheng1989), mazheng19891019@gmail.com * Company: Dalian University Of Technology * * ===================================================================================== */ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <netinet/in.h> #include <string.h> #include <netdb.h> #define PORT 1234 /* */ #define MAXDATASIZE 100 /* */ int main(int argc,char **argv) { int sockfd,num; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in server,peer; if(argc!=3) { printf("Usage: %s <IP Address> <message>\n",argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { printf("gethostbyname()error.\n"); exit(1); } if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) { printf("socket() error.\n"); exit(1); } bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(PORT); server.sin_addr=*((struct in_addr *)he->h_addr); sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr *)&server,sizeof(server)); socklen_t len=sizeof(server); while(1) { num=recvfrom(sockfd,buf,MAXDATASIZE,0,(struct sockaddr *)&peer,&len); if(num<0) { printf("recvfrom() error.\n"); exit(1); } if(len!=sizeof(server)||memcmp((const void *)&server,(const void *)&peer,len)!=0) { printf("Receive message from other server.\n"); continue; } buf[num]='\0'; printf("Server Message:%s.\n",buf); break; } close(sockfd); return 0; }运行命令:$:gcc udpclient.c -o udpclient.out
$:./udpclient.out 127.0.0.1 helo