1. 程式人生 > >TCP伺服器的單程序實現

TCP伺服器的單程序實現

一、tcp_server(TCP伺服器)

//tcp_server.c程式碼

  1 #include<stdio.h>                                                           
  2 #include<sys/types.h>
  3 #include<sys/socket.h>
  4 #include<string.h>
  5 #include<netinet/in.h>
  6 #include<arpa/inet.h>
  7 #include<string.h>
8 #include<stdlib.h> 9 10 static void usage(const char* proc) 11 { 12 printf("%s [local_ip][local_port]\n",proc); 13 } 14 15 int startup(const char* _ip, int _port) 16 { 17 int sock = socket(AF_INET, SOCK_STREAM, 0); 18 if(sock < 0) 19 { 20 perror("socket"
); 21 exit(2); 22 } 23 struct sockaddr_in local; 24 local.sin_family = AF_INET; 25 local.sin_port = htons(_port); 26 local.sin_addr.s_addr = inet_addr(_ip); 27 if( bind(sock,(struct sockaddr *)&local,sizeof(local)) < 0) 28 { 29 perror("bind"
); 30 exit(3); 31 } 32 if(listen(sock, 5) < 0 ) 33 { 34 perror("listen"); 35 exit(4); 36 } 37 return sock; 38 } 39 40 int main(int argc , char* argv[]) 41 { 42 if(argc < 3) 43 { 44 usage(argv[0]); 45 return 1; 46 } 47 int listen_sock=startup(argv[1],atoi(argv[2])); 48 while(1) 49 { 50 struct sockaddr_in client; 51 socklen_t len = sizeof(client); 52 int new_fd = accept(listen_sock,(struct sockaddr *)&client, &len ); 53 if(new_fd < 0) 54 { 55 perror("accept"); 56 //can not exit; 57 continue; 58 } 59 printf("get a new client\n"); 60 while(1) 61 { 62 char buf[1024]; 63 ssize_t s=read(new_fd,buf,sizeof(buf)-1); 64 if(s > 0) 65 { 66 buf[s]=0; 67 printf("client: %s\n",buf); 68 write(new_fd,buf,strlen(buf)); 69 } 70 else 71 { 72 printf("read done ....,break\n"); 73 break; 74 } 75 } 76 } 77 return 0; 78 }

這裡寫圖片描述

二、tcp_client(TCP客戶端)

//tcp_client程式碼

  1 #include<stdio.h>                                                           
  2 #include<sys/types.h>
  3 #include<sys/socket.h>
  4 #include<string.h>
  5 #include<netinet/in.h>
  6 #include<arpa/inet.h>
  7 #include<stdlib.h>
  8 #include<string.h>
  9 static void usage(const char* proc)
 10 {
 11     printf("%s [local_ip][local_port]\n",proc);
 12 }
 13 
 14 int main(int argc , char* argv[])
 15 {
 16     if(argc < 3)
 17     {
 18         usage(argv[0]);
 19         return 1;
 20     }
 21     int sock = socket(AF_INET, SOCK_STREAM, 0);
 22       if(sock < 0)
 23       {
 24            perror("socket");
 25            return 2;
 26        }
 27       struct sockaddr_in remote;
 28       remote.sin_family=AF_INET;
 29       remote.sin_port=htons(atoi(argv[2]));
 30       remote.sin_addr.s_addr=inet_addr(argv[1]);
 31       if(connect(sock,(struct sockaddr *)&remote, sizeof(remote)) < 0 )
 32       {
 33            perror("connect");
 34            return 3;
 35        }
 36       while(1)
 37          {
 38                char buf[1024];
 39                printf("Please Enter #");
 40                fflush(stdout);
 41                ssize_t s=read(0, buf, sizeof(buf)-1);
 42                if(s > 0)
 43                {
 44                     buf[s-1]=0;
 45                     write(sock, buf, strlen(buf));
 46                     ssize_t _s=read(sock, buf, sizeof(buf)-1);      
 47                     if(_s > 0)
 48                     {
 49                          buf[_s]=0;
 50                          printf("server echo# %s\n",buf);
 51                      }
 52                }
 53          }
 54 return 0;  
 55 }         

這裡寫圖片描述

三、結果圖

這裡寫圖片描述
這裡寫圖片描述

四、總結

以上的程式碼還有待修改它只能實現單程序的連線伺服器