linux udp c/s

一、UDP C/S编程的步骤如下图所示

二、与TCP C/S通信的区别在于:
服务端没有设置监听和等待连接的过程。客户端没有连接服务端的过程。
基于UDP的通信时不可靠地,面向无连接的,发送的数据无法确切知道对方收到没有,就算对方根本不存在,也可以发送数据出去。
这样的通信通常用在对可靠性、安全性要求不高的地方,比如语音通信(没听清楚可以让对方再说一遍)。

三、server端编程

 1 #include <sys/types.h>
 2 #include <sys/socket.h>
 3 #include<pthread.h>
 4 #include <netinet/in.h>
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <unistd.h>
 8 #include <stdlib.h>
 9 
10 int main(int argc, char **argv)
11 {
12     if (argc != 2)
13     {
14         printf("Usage: %s port\n", argv[0]);
15         exit(1);
16     }
17     printf("Welcome! This is a UDP server, I can only received message from client and reply with same message\n");
18     
19     struct sockaddr_in addr;   
20     addr.sin_family = AF_INET;
21     addr.sin_port = htons(atoi(argv[1]));    //协商的发送端的端口号
22     addr.sin_addr.s_addr = htonl(INADDR_ANY); //本机地址
23 
24     int sock;
25     if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
26     {
27         perror("socket");
28         exit(1);
29     }
30     if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) //绑定socket套接字到本地的监听端口
31     {
32         perror("bind");
33         exit(1);
34     }
35     char buff[512];
36     struct sockaddr_in clientAddr;
37     int n;
38     int len = sizeof(clientAddr);
39     while (1)
40     {
41         n = recvfrom(sock, buff, 511, 0, (struct sockaddr*)&clientAddr, &len);//接收UDP包
42         if (n>0)
43         {
44             buff[n] = 0;
45             printf("%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff);
46             n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));//往发送方发送UDP回复包
47             if (n < 0)
48             {
49                 perror("sendto");
50                 break;
51             }
52         }
53         else
54         {
55             perror("recv");
56             break;
57         }
58     }
59     return 0;
60 }

四、client端

 1 #include <sys/types.h>
 2 #include <sys/socket.h>
 3 #include<pthread.h>
 4 #include <netinet/in.h>
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <unistd.h>
 8 #include <stdlib.h>
 9 
10 
11 int main(int argc, char **argv)
12 {
13     if (argc != 3)
14     {
15         printf("Usage: %s ip port", argv[0]);
16         exit(1);
17     }
18     printf("This is a UDP client\n");
19     struct sockaddr_in addr;
20     int sock;
21 
22     if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)//创建socket
23     {
24         perror("socket");
25         exit(1);
26     }
27     addr.sin_family = AF_INET;
28     addr.sin_port = htons(atoi(argv[2]));
29     addr.sin_addr.s_addr = inet_addr(argv[1]);//对方的IP地址、端口号
30     if (addr.sin_addr.s_addr == INADDR_NONE)
31     {
32         printf("Incorrect ip address!");
33         close(sock);
34         exit(1);
35     }
36 
37     char buff[512];
38     int len = sizeof(addr);
39     while (1)
40     {
41         gets(buff);
42         int n;
43         n = sendto(sock, buff, strlen(buff), 0, (struct sockaddr *)&addr, sizeof(addr));//往对方地址发送UDP包
44         if (n < 0)
45         {
46             perror("sendto");
47             close(sock);
48             break;
49         }
50         n = recvfrom(sock, buff, 512, 0, (struct sockaddr *)&addr, &len);//接收来自对方IP地址的UDP包
51         if (n>0)
52         {
53             buff[n] = 0;
54             printf("received:");
55             puts(buff);
56         }
57         else if (n==0)
58         {
59             printf("server closed\n");
60             close(sock);
61             break;
62         }
63         else if (n == -1)
64         {
65             perror("recvfrom");
66             close(sock);
67             break;
68         }
69     }
70     
71     return 0;
72 }

转自:http://www.cnblogs.com/uvsjoh/archive/2013/01/01/2841764.html

posted on 2013-07-23 17:04  meizixiong  阅读(347)  评论(0编辑  收藏  举报