linux下tcp server,运行文件./tcpserver 8001
1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<string.h> 4 #include<errno.h> 5 #include<string.h> 6 #include<netdb.h> 7 #include<sys/types.h> 8 #include<netinet/in.h> 9 #include<sys/socket.h> 10 #include<unistd.h> 11 #include<arpa/inet.h> 12 //portnumber 8080 13 14 int main(int argc ,char *argv[]) 15 { 16 int sockfd,new_fd; 17 struct sockaddr_in server_addr; 18 struct sockaddr_in client_addr; 19 int sin_size,iDataNum; 20 int portnumber; 21 // const char hello[]="Hello cpp, you are success.\n"; 22 char buffer[4096]; 23 strcpy(buffer, "test111\n"); 24 25 if(argc!=2) 26 { 27 fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); 28 return 0; 29 } 30 portnumber = atoi(argv[1]); 31 if(portnumber < 0) 32 { 33 fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); 34 return 0; 35 } 36 if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) 37 { 38 fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); 39 return 0; 40 } 41 bzero(&server_addr,sizeof(struct sockaddr_in)); 42 server_addr.sin_family=AF_INET; 43 server_addr.sin_addr.s_addr=htonl(INADDR_ANY); 44 server_addr.sin_port=htons(portnumber); 45 if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) 46 { 47 fprintf(stderr,"Bind error:%s\n\a",strerror(errno)); 48 return 0; 49 } 50 if(listen(sockfd,5)==-1) 51 { 52 fprintf(stderr,"Listen error:%s\n\a",strerror(errno)); 53 return 0; 54 } 55 while(1) 56 { 57 sin_size=sizeof(struct sockaddr_in); 58 if((new_fd = accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size)) == -1) 59 { 60 fprintf(stderr,"Accept error:%s\n\a",strerror(errno)); 61 return 0; 62 } 63 fprintf(stdout,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr)); 64 // iDataNum=recv(new_fd,buffer,4096,0); 65 // if(iDataNum<0) 66 // { 67 // perror("Recv\n"); 68 // exit(1); 69 // } 70 //printf("Recv data is %s\n",buffer); 71 send(new_fd, buffer, strlen(buffer), 0); 72 // if(write(new_fd,hello,strlen(hello))==-1) 73 // { 74 // fprintf(stderr,"write error:%s\n",strerror(errno)); 75 // return 0; 76 // } 77 close(new_fd); 78 79 } 80 close(sockfd); 81 // exit(0); 82 return 0; 83 }
linux 下tcp client, ./tcpclient localhost 8001 或者 ./tcpclient 127.0.0.1 8001
1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<string.h> 4 #include<netdb.h> 5 #include<sys/types.h> 6 #include<netinet/in.h> 7 #include<sys/socket.h> 8 #include<unistd.h> 9 #include<arpa/inet.h> 10 #include<errno.h> 11 12 int main(int argc,char *argv[]) 13 { 14 int sockfd; 15 int i = 0; 16 int temp = 0; 17 char sendbuffer[200]; 18 char recvbuffer[200]; 19 char buffer[1024]; 20 struct sockaddr_in server_addr; 21 struct hostent *host; 22 int portnumber,nbytes; 23 if(argc!=3) 24 { 25 fprintf(stderr,"Usage :%s hostname portnumber\a\n",argv[0]); 26 exit(1); 27 } 28 if((host=gethostbyname(argv[1]))==NULL) 29 { 30 herror("Get host name error\n"); 31 exit(1); 32 } 33 if((portnumber=atoi(argv[2]))<0) 34 { 35 fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]); 36 exit(1); 37 } 38 39 while (1) 40 { 41 if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) 42 { 43 fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); 44 exit(1); 45 } 46 bzero(&server_addr,sizeof(server_addr)); 47 server_addr.sin_family=AF_INET; 48 server_addr.sin_port=htons(portnumber); 49 server_addr.sin_addr=*((struct in_addr *)host->h_addr); 50 51 52 if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) 53 { 54 fprintf(stderr,"Connect error:%s\n",strerror(errno)); 55 //exit(1); 56 } 57 /* while(1) 58 { 59 printf("Please input your word:\n"); 60 scanf("%s",sendbuffer); 61 printf("\n"); 62 if(strcmp(sendbuffer,"quit")==0) 63 break; 64 send(sockfd,sendbuffer,sizeof(sendbuffer),0); 65 recv(sockfd,recvbuffer,200,0); 66 printf("recv data of my world is :%s\n",recvbuffer); 67 } */ 68 // sleep(3); 69 if((nbytes = recv(sockfd, buffer, 1024, 0)) == -1) 70 { 71 fprintf(stderr,"read error:%s\n",strerror(errno)); 72 //exit(1); 73 } 74 else 75 { 76 // buffer[nbytes] = '\0'; 77 // printf("I have received:%s\n", buffer); 78 for(i = 0; i < nbytes; i++) 79 { 80 temp = buffer[i]; 81 //temp = temp & 0x000000FF; 82 printf("%02X ", temp); 83 } 84 printf("\n"); 85 } 86 sleep(3); 87 close(sockfd); 88 } 89 exit(0); 90 }