1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<sys/types.h> 5 #include<sys/socket.h> 6 #include<netinet/in.h> 7 #include <unistd.h> 8 9 int main() { 10 /* socket */ 11 int soc_ser_l=socket(PF_INET,SOCK_STREAM,0); 12 13 /* creat server sockaddr_in */ 14 struct sockaddr_in ser_addr; 15 ser_addr.sin_family=AF_INET; 16 ser_addr.sin_addr.s_addr=htonl(INADDR_ANY); 17 ser_addr.sin_port=htons(8888); 18 memset(ser_addr.sin_zero,0,8); 19 20 /* bind connect socket and ip/port */ 21 if(bind(soc_ser_l,(struct sockaddr*)&ser_addr,sizeof(ser_addr))==-1) 22 { 23 printf("bind error"); 24 return 0; 25 } 26 27 /* listen */ 28 if(listen(soc_ser_l,20)==-1) 29 { 30 printf("listen error"); 31 return 0; 32 } 33 /* creat items */ 34 int soc_ser_t; 35 struct sockaddr_in cli_addr; 36 while (1){ 37 /* accept */ 38 int len=sizeof(ser_addr); 39 soc_ser_t=accept(soc_ser_l,(struct sockaddr *)&cli_addr,&len); 40 if(soc_ser_t==-1){ 41 perror("accept error"); 42 exit(1); 43 } 44 45 /* recv data */ 46 char recv_buf[255]; 47 int ret=recv(soc_ser_t,recv_buf,255,0); 48 if(ret>0){ 49 recv_buf[ret]='