码迷,mamicode.com
首页 > 其他好文 > 详细

UDP编程

时间:2015-03-12 13:16:25      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

1.UDP:面向无连接的传输层协议,不可靠,能满足时效性高的要求。

2.模型:

技术分享

 

 

 

 

 

 

 

 

 

 

 

 

 

3.通信过程

  1)socket()创建socket对象

  2)bind()绑定IP端口

  3)发送消息:sendto()/sendmsg()

  ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr * to, int addrlen)

  4)接收消息:recvfrom()/recvmsg()

  ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr * from, size_t *addrlen)

  5)关闭:close()/shutdown()

  在没有特殊处理的情况下不能用read()/write()函数。

4.实例:

  1)接收端:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <sys/types.h>
 4 #include <netinet/in.h>
 5 #include <sys/socket.h>
 6 #include <errno.h>
 7 #include <stdlib.h>
 8 #include <arpa/inet.h>
 9 
10 int main(int argc, char **argv)
11 {
12     struct sockaddr_in s_addr;
13     struct sockaddr_in c_addr;
14     int sock;
15     socklen_t addr_len;
16     int len;
17     char buff[128];
18 
19     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 
20     {
21         perror("socket");
22         exit(errno);
23     } else
24         printf("create socket.\n\r");
25 
26     memset(&s_addr, 0, sizeof(struct sockaddr_in));
27 
28     s_addr.sin_family = AF_INET;
29     s_addr.sin_port = htons(7838);
30     s_addr.sin_addr.s_addr = INADDR_ANY;
31 
32     if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) 
33     {
34         perror("bind");
35         exit(errno);
36     } else
37         printf("bind address to socket.\n\r");
38 
39     addr_len = sizeof(c_addr);
40     while (1) 
41     {
42         len = recvfrom(sock, buff, sizeof(buff) - 1, 0,
43                 (struct sockaddr *) &c_addr, &addr_len);
44         if (len < 0) 
45         {
46             perror("recvfrom");
47             exit(errno);
48         }
49 
50         buff[len] = \0;
51         printf("recive come from %s:%d message:%s\n\r",
52             inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff);
53     }
54     return 0;
55 }

  2)发送端:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <sys/types.h>
 4 #include <netinet/in.h>
 5 #include <sys/socket.h>
 6 #include <errno.h>
 7 #include <stdlib.h>
 8 #include <arpa/inet.h>
 9 
10 int main(int argc, char **argv)
11 {
12     struct sockaddr_in s_addr;
13     int sock;
14     int addr_len;
15     int len;
16     char buff[128];
17 
18     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 
19     {
20         perror("socket");
21         exit(errno);
22     } else
23         printf("create socket.\n\r");
24 
25     s_addr.sin_family = AF_INET;
26     s_addr.sin_port = htons(7838);
27     if (argv[1])
28         s_addr.sin_addr.s_addr = inet_addr(argv[1]);
29     else {
30         printf("input sever ip!\n");
31         exit(0);
32     }
33 
34     addr_len = sizeof(s_addr);
35     strcpy(buff, "hello i‘m here");
36     len = sendto(sock, buff, strlen(buff), 0,
37             (struct sockaddr *) &s_addr, addr_len);
38     if (len < 0) {
39         printf("\n\rsend error.\n\r");
40         return 3;
41     }
42 
43     printf("send success.\n\r");
44     return 0;
45 }

 

UDP编程

标签:

原文地址:http://www.cnblogs.com/xp12/p/4332026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!