Linux程序设计之套接字:UDP

    编写与客户端之间维持连接的应用程序,我们用面向连接的TCP套接字来完成这一工作。但在有些情况下,在程序中花费时间来建立和维持一个套接字连接是不必要的。比如 daytime 服务,我们首先创建一个套接字,然后建立连接,读取一个响应,最后关闭连接。在这一过程中,我们使用了很多操作步骤,仅仅为了获取一个日期。

     daytime 服务还可以用数据报通过UDP来访问。为了访问它,发送一个数据报给该服务,然后在响应中获取一个包含日期和时间的数据报。这一过程非常简单。当客户需要发送一个短小的查询请求给服务器,并且期望接受到一个短小的响应时,我们一般使用由UDP提供的服务。如果服务器处理客户请求的时间足够短,服务器就可以通过一次处理一个客户请求的方式来提供服务,从而允许操作系统将客户进入的请求放入队列。这简化服务器程序的编写。

    为了使用UDP提供的服务,你需要像往常一样使用套接字和 close 系统调用,但你需要用两个数据报专用的系统调用 sendto 和 recvfrom 来代替使用TCP服务时的 read 和 write 调用。下面是一个简单的示范程序,如有需要可以扩展其功能:

 

  
  
  
  
  1. /* server.cc */
  2.  
  3. #include <cstdio>                                                                 
  4. #include <cstdlib>
  5.                                                                                   
  6. #include "sys/socket.h"                                                           
  7. #include "netinet/in.h"                                                           
  8. #include "unistd.h"                                                               
  9.                                                                                   
  10. #define SRV_PORT 6500 

  11. using namespace std;                                                              
  12.                                                                                   
  13. char buffer[512];                                                                 
  14.                                                                                   
  15. int main(int argc, char *argv[])                                                  
  16. {                                                                                 
  17.     /* Create a UDP socket */                                                     
  18.     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);                                  
  19.                                                                                   
  20.     /* Construct the address for use with sendto/recvfrom */                      
  21.     struct sockaddr_in address;                                                   
  22.     address.sin_family = AF_INET;                                                 
  23.     address.sin_addr.s_addr = htonl(INADDR_ANY);                                  
  24.     address.sin_port = htons(SRV_PORT);                                           
  25.     int len = sizeof(address);                                                    
  26.                                                                                   
  27.     /* Bind address to socket */                                                  
  28.     bind(sockfd, (struct sockaddr *)&address, sizeof(address));                   
  29.                                                                                   
  30.     /* serving */                                                                 
  31.     while(1) {                                                                    
  32.         /* receive */                                                             
  33.         recvfrom(sockfd, buffer, sizeof(buffer), 0,                               
  34.                  (struct sockaddr *)&address, (socklen_t *)&len);                 
  35.                                                                                   
  36.         /* handle */                                                              
  37.         sprintf(buffer, "i am server");                                           
  38.                                                                                   
  39.         /* response */                                                            
  40.         sendto(sockfd, buffer, sizeof(buffer), 0,                                 
  41.                (struct sockaddr *)&address, sizeof(address));                     
  42.                                                                                   
  43.     }                                                                             
  44.                                                                                   
  45.     /* close */                                                                   
  46.     close(sockfd);                                                                
  47.                  
  48.     return 0;                                                                     
  
  
  
  
  1. /* client.cc */
  2.  
  3. #include <cstdio>                                                                 
  4. #include <cstdlib>                                                                
  5.                                                                                   
  6. #include "arpa/inet.h"                                                            
  7. #include "sys/socket.h"                                                           
  8. #include "netinet/in.h"                                                           
  9. #include "unistd.h"                                                               
  10.                        
  11. #define SRV_ADDR "127.0.0.1"                                                 
  12. #define SRV_PORT 6500 
  13.  
  14. #include "client.h"                                                               
  15.                                                                                   
  16. using namespace std;                                                              
  17.                                                                                   
  18. char buffer[512];                                                                 
  19.                                                                                   
  20. int main(int argc, char *argv[])                                                  
  21. {                                                                                 
  22.     /* Create a UDP socket */                                                     
  23.     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);                                  
  24.                                                                                   
  25.     /* Construct the address for use with sendto/recvfrom */                      
  26.     struct sockaddr_in address;                                                   
  27.     address.sin_family = AF_INET;                                                 
  28.     address.sin_addr.s_addr = inet_addr(SRV_ADDR);                                
  29.     address.sin_port = htons(SRV_PORT);                                           
  30.     int len = sizeof(address);                                                    
  31.                                                                                   
  32.     /* sendto */                                                                  
  33.     sendto(sockfd, buffer, sizeof(buffer), 0,                                     
  34.            (struct sockaddr *)&address, sizeof(address));                         
  35.                                                                                   
  36.     /* receive */                                                                 
  37.     recvfrom(sockfd, buffer, sizeof(buffer), 0,                                   
  38.              (struct sockaddr *)&address, (socklen_t *)&len);                     
  39.                                                                                   
  40.     /* debug */                                                                   
  41.     printf("RECV: %s", buffer);                                                   
  42.                                                                                   
  43.     /* close */                                                                   
  44.     close(sockfd);                                                                
  45.  
  46.     return 0;                                                                     

 

你可能感兴趣的:(linux,socket,UDP)