TCP服务器: TCP_Server.c #include<stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <sys/stat.h> #include <netinet/in.h> #include <sys/socket.h> #define PORTNUMBER 3333 int main(int argc,char*argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size; int nbytes; char buffer[1024]; //socket if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket error:%s\n",strerror(errno)); exit(1); } //address bzero(&server_addr,sizeof(structsockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(PORTNUMBER); //bind if(bind(sockfd,(structsockaddr *)(&server_addr),sizeof(structsockaddr_in))==-1) { fprintf(stderr,"Bind error:%s\n",strerror(errno)); exit(1); } //listen if(listen(sockfd,5)==-1) { fprintf(stderr,"Listen error:%s\n",strerror(errno)); exit(1); } while(1) { sin_size=sizeof(structsockaddr_in); if((new_fd=accept(sockfd,(structsockaddr *)(&client_addr),&sin_size))==-1) { fprintf(stderr,"Accept error:%s\n",strerror(errno)); exit(1); } fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr)); if((nbytes=read(new_fd,buffer,1024))==-1) { fprintf(stderr,"Read Error:%s\n",strerror(errno)); exit(1); } buffer[nbytes]='\0'; printf("Server received %s\n",buffer); close(new_fd); } } | TCP客户端: TCP_client.c #include<stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define PORTNUMBER 3333 int main(int argc,char*argv[]) { int sockfd; char buffer[1024]; struct sockaddr_in server_addr; struct hostent*host; //使用hostent查询host name if(argc!=2) { fprintf(stderr,"Uage:%s hostname\a\n",argv[0]); exit(1); } if((host=gethostbyname(argv[1]))==NULL) fprintf(stderr,"Gethostname error\n"); exit(1); } //socket if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } //ip port bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(PORTNUMBER); server_addr.sin_addr=*((structin_addr *)host->h_addr); //connect if(connect(sockfd,(structsockaddr *)(&server_addr),sizeof(structsockaddr_in))==-1) { fprintf(stderr,"Connect error:%s\a\n",strerror(errno)); exit(1); } printf("Please input char:\n"); fgets(buffer,1024,stdin); write(sockfd,buffer,strlen(buffer)); close(sockfd); exit(0); } | --------------**************分割线***************------------------- UDP服务器: udp_server.c 1 #include <stdlib.h> 2 #include<stdio.h> 3 #include<errno.h> 4 #include<string.h> 5 #include<unistd.h> 6 #include<netdb.h> 7 #include<sys/socket.h> 8 #include<netinet/in.h> 9 #include<arpa/inet.h> 10 #include<sys/types.h> 11 12 #define SERVER_PORT 8888 13 #define MAX_MSG_SIZE 1024 14 15 void udps_respon(int sockfd) 16 { 17 struct sockaddr_in addr; 18 int addrlen,n; 19 char msg[MAX_MSG_SIZE]; 20 21 while(1) 22 { 23 bzero(msg,sizeof(msg)); 24 addrlen=sizeof(structsockaddr); 25 n=recvfrom(sockfd,msg,MAX_MSG_SIZE,0,(structsockaddr *)&addr,&addrlen); 26 msg[n]='\0'; 27 fprintf(stdout,"Server have received %s",msg); 28 } 29 } 30 31 32 int main(void) 33 { 34 int sockfd; 35 struct sockaddr_in addr; 36 37 //socket 38 sockfd=socket(AF_INET,SOCK_DGRAM,0); 39 if(sockfd<0) 40 { 41 fprintf(stderr,"Socket Error:%s\n",strerror(errno)); 42 exit(1); 43 } 44 45 //ip port 46 bzero(&addr,sizeof(structsockaddr_in)); 47 addr.sin_family=AF_INET; 48 addr.sin_addr.s_addr=htonl(INADDR_ANY); 49 addr.sin_port=htons(SERVER_PORT); 50 51 //bind 52 if(bind(sockfd,(structsockaddr *)&addr,sizeof(structsockaddr_in))<0) 53 { 54 fprintf(stderr,"Bind Error:%s\n",strerror(errno)); 55 exit(1); 56 } 57 udps_respon(sockfd); 58 close(sockfd); 59 } | UDP客户端: udp_client.c 1 #include <stdlib.h> 2 #include<stdio.h> 3 #include<errno.h> 4 #include<string.h> 5 #include<netdb.h> 6 #include<sys/types.h> 7 #include<arpa/inet.h> 8 #include<sys/socket.h> 9 10 #define SERVER_PORT 8888 11 #define MAX_BUF_SIZE 1024 12 13 void udpc_requ(int sockfd,conststruct sockaddr_in*addr,int len) 14 { 15 char buffer[MAX_BUF_SIZE]; 16 int n; 17 while(1) 18 { 19 printf("please input char:\n"); 20 fgets(buffer,MAX_BUF_SIZE+1,stdin); 21 sendto(sockfd,buffer,strlen(buffer),0,(structsockaddr *)addr,len); 22 bzero(buffer,MAX_BUF_SIZE); 23 } 24 } 25 26 27 int main(int argc,char**argv) 28 { 29 int sockfd; 30 struct sockaddr_in addr; 31 struct hostent*host; 32 33 if(argc!=2) 34 { 35 fprintf(stderr,"Usage:%s server_ip\n",argv[0]); 36 exit(1); 37 } 38 39 if((host=gethostbyname(argv[1]))==NULL) 40 { 41 fprintf(stderr,"Gethostname error\n"); 42 exit(1); 43 } 44 45 //socket 46 sockfd=socket(AF_INET,SOCK_DGRAM,0); 47 if(sockfd<0) 48 { 49 fprintf(stderr,"Socket Error:%s\n",strerror(errno)); 50 exit(1); 51 } 52 53 //ip port 54 bzero(&addr,sizeof(structsockaddr_in)); 55 addr.sin_family=AF_INET; 56 addr.sin_port=htons(SERVER_PORT); 57 /* if(inet_aton(argv[1],&addr.sin_addr)<0) 58 { 59 fprintf(stderr,"IP error:%s\n",strerror(errno)); 60 exit(1); 61 } */ 62 addr.sin_addr=*((structin_addr *)host->h_addr); 63 64 65 udpc_requ(sockfd,&addr,sizeof(structsockaddr_in)); 66 close(sockfd); 67 } | | | | |