Client-Server C程序示例C (使用 Sockets 和 TCP)

下面你将看到一个非常简单的client-server 的C程序示例。 基本上客户端连接到服务器上,服务器发一个消息 “Hello World”,然后客户端打印接收到的消息。

请注意我是手动配置设置的。如果你想你的代码是IPV4-IPV6 无关, IP 无关且可移植到不同的平台上你可以使用该教程介绍过的getaddrinfo() 函数。
其次,我在大多数的函数调用上没有执行错误检查。如果你想把代码用到一个实际项目上的话你应该实现这些检查。 
第三,如果你想到关于函数和其参数的更多细节的话,请看每个函数的man page。

最后一点,为了测试代码你仅仅需要在一个终端上跑对应的服务器,然后在另一个终端上跑客户端(或者将服务器做为一个后台进程执行,然后在相同的终端上执行客户端。

 

服务端代码

 

[cpp] view plain copy
 
  1. /****************** SERVER CODE ****************/  
  2.   
  3. #include <stdio.h>  
  4. #include <sys/socket.h>  
  5. #include <netinet/in.h>  
  6. #include <string.h>  
  7.   
  8. int main(){  
  9.   int welcomeSocket, newSocket;  
  10.   char buffer[1024];  
  11.   struct sockaddr_in serverAddr;  
  12.   struct sockaddr_storage serverStorage;  
  13.   socklen_t addr_size;  
  14.   
  15.   /*---- Create the socket. The three arguments are: ----*/  
  16.   /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */  
  17.   welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);  
  18.     
  19.   /*---- Configure settings of the server address struct ----*/  
  20.   /* Address family = Internet */  
  21.   serverAddr.sin_family = AF_INET;  
  22.   /* Set port number, using htons function to use proper byte order */  
  23.   serverAddr.sin_port = htons(7891);  
  24.   /* Set IP address to localhost */  
  25.   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
  26.   /* Set all bits of the padding field to 0 */  
  27.   memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);    
  28.   
  29.   /*---- Bind the address struct to the socket ----*/  
  30.   bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));  
  31.   
  32.   /*---- Listen on the socket, with 5 max connection requests queued ----*/  
  33.   if(listen(welcomeSocket,5)==0)  
  34.     printf("Listening\n");  
  35.   else  
  36.     printf("Error\n");  
  37.   
  38.   /*---- Accept call creates a new socket for the incoming connection ----*/  
  39.   addr_size = sizeof serverStorage;  
  40.   newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);  
  41.   
  42.   /*---- Send message to the socket of the incoming connection ----*/  
  43.   strcpy(buffer,"Hello World\n");  
  44.   send(newSocket,buffer,13,0);  
  45.   
  46.   return 0;  
  47. }  



 

客户端代码

 

[cpp] view plain copy
 
    1. /****************** CLIENT CODE ****************/  
    2.   
    3. #include <stdio.h>  
    4. #include <sys/socket.h>  
    5. #include <netinet/in.h>  
    6. #include <string.h>  
    7.   
    8. int main(){  
    9.   int clientSocket;  
    10.   char buffer[1024];  
    11.   struct sockaddr_in serverAddr;  
    12.   socklen_t addr_size;  
    13.   
    14.   /*---- Create the socket. The three arguments are: ----*/  
    15.   /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */  
    16.   clientSocket = socket(PF_INET, SOCK_STREAM, 0);  
    17.     
    18.   /*---- Configure settings of the server address struct ----*/  
    19.   /* Address family = Internet */  
    20.   serverAddr.sin_family = AF_INET;  
    21.   /* Set port number, using htons function to use proper byte order */  
    22.   serverAddr.sin_port = htons(7891);  
    23.   /* Set IP address to localhost */  
    24.   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
    25.   /* Set all bits of the padding field to 0 */  
    26.   memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);    
    27.   
    28.   /*---- Connect the socket to the server using the address struct ----*/  
    29.   addr_size = sizeof serverAddr;  
    30.   connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);  
    31.   
    32.   /*---- Read the message from the server into the buffer ----*/  
    33.   recv(clientSocket, buffer, 1024, 0);  
    34.   
    35.   /*---- Print the received message ----*/  
    36.   printf("Data received: %s",buffer);     
    37.   
    38.   return 0;  
posted @ 2017-09-14 16:19  苍月代表我  阅读(235)  评论(0编辑  收藏  举报