Chinaunix首页 | 论坛 | 博客
  • 博客访问: 423548
  • 博文数量: 132
  • 博客积分: 2511
  • 博客等级: 大尉
  • 技术积分: 1385
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-11 15:10
文章分类

全部博文(132)

文章存档

2012年(18)

2011年(35)

2010年(60)

2009年(19)

分类: LINUX

2012-04-15 21:32:20

在水木上看到一个关于在客户端调用bind的讨论,

如果不调用bind,则客户端在向外发包时,会由系统自己决定使用的接口的源端口,而调用bind则可以指定相应的参数。

另外有个哥们提到“果是udp,使用bind以后,可以不使用sendto/recvform函数,而直接用write/read函数了,少去了写一个参数。”,这应该是调用connect后的效果。

关于UDP中客户端调用connect的好处,还有一个作用是能够捕获错误
由于UDP是无连接的,connect在调用时其实没有向外发包,只是在协议栈中记录了该状态,应该是生成了一个类似TCB的结构。之后如果发生网络异常,比如对端不可达,客户端在往对端写数据后,本机会收到一个ICMP回应,则回来的ICMP不可达的响应能够被协议栈处理,通知客户端进程;当客户端再次对该fd进行操作时,比如读数据时,read等调用会返回一个错误。而不调用connect时,对于返回的ICMP响应,协议栈不知道该传递给上层的哪个应用,所以客户端进程中捕获不到相应的错误。
在两种情况下,write或者sendto操作都是把数据放到协议栈的发送队列之后就返回成功,而相应的ICMP回应则要等数据到达对端后才能返回,所以通常这种情况叫做“异步错误”。

使用下列代码进行验证:

点击(此处)折叠或打开

  1. #include <sys/socket.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <arpa/inet.h>
  6. #include <stdlib.h>

  7. #define MAXLINE 80
  8. #define SERV_PORT 8888

  9. struct sockaddr_in servaddr;

  10. void do_cli(FILE *fp,int sockfd,struct sockaddr *pservaddr,socklen_t servlen)
  11. {
  12.     int n;
  13.     char sendline[MAXLINE],recvline[MAXLINE + 1];

  14.     #ifdef UDP_CONNECT
  15.     /* connect to server */
  16.     if(connect(sockfd,(struct sockaddr *)pservaddr,servlen) == -1)
  17.     {
  18.         perror("connect error");
  19.         exit(1);
  20.     }
  21.     #endif

  22.     while(fgets(sendline,MAXLINE,fp) != NULL)
  23.     {
  24.         #ifdef UDP_CONNECT
  25.         /* read a line and send to server */
  26.         write(sockfd,sendline,strlen(sendline));
  27.         #else
  28.         sendto(sockfd, sendline, strlen(sendline), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
  29.         #endif

  30.         printf("write over\n");
  31.         /* receive data from server */
  32.         n = read(sockfd,recvline,MAXLINE);
  33.         if(n == -1)
  34.         {

  35.             perror("read error");
  36.             exit(1);
  37.         }
  38.         recvline[n] = 0; /* terminate string */
  39.         fputs(recvline,stdout);
  40.     }
  41. }

  42. int main(int argc,char **argv)
  43. {
  44.     int sockfd;

  45.     /* check args */
  46.     if(argc != 2)
  47.     {
  48.         printf("usage: udpclient serverip\n");
  49.         exit(1);
  50.     }

  51.     /* init servaddr */
  52.     bzero(&servaddr,sizeof(servaddr));
  53.     servaddr.sin_family = AF_INET;
  54.     servaddr.sin_port = htons(SERV_PORT);
  55.     if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0)
  56.     {
  57.         printf("[%s] is not a valid IPaddress\n",argv[1]);
  58.         exit(1);
  59.     }
  60.     sockfd = socket(AF_INET,SOCK_DGRAM,0);
  61.     do_cli(stdin,sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
  62.     return 0;
  63. }


########################实验1,客户端进行connect######################
客户端执行:
mt@ubuntu:~/code$ gcc -o udpclient_connect -DUDP_CONNECT udpclient.c
mt@ubuntu:~/code$ ./udpclient_connect 192.168.0.1
abcd
write over
read error: Connection refused

在另一窗口抓包:
mt@ubuntu:~$ sudo tcpdump -i eth0 port 8888 or icmp -v
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:49:40.300735 IP (tos 0x0, ttl 64, id 20973, offset 0, flags [DF], proto UDP (17), length 33)
    localhost.42774 > localhost.8888: UDP, length 5
21:49:40.303965 IP (tos 0x0, ttl 64, id 22696, offset 0, flags [none], proto ICMP (1), length 56)
    localhost > localhost: ICMP localhost udp port 8888 unreachable, length 36
        IP (tos 0x0, ttl 64, id 20973, offset 0, flags [DF], proto UDP (17), length 33)
    localhost.42774 > localhost.8888: UDP, length 5

可以看到,客户端发送数据之后,收到了ICMP不可达的回应,此时客户端进程的read()操作返回了错误,通过perror打印出来的错误信息为:Connection refused
##########################实验1 结束###################################

###########################实验2,客户端不进行connect###############
mt@ubuntu:~/code$ gcc -o udpclient udpclient.c
mt@ubuntu:~/code$ ./udpclient 192.168.0.1
abcd
write over

在另一窗口抓包:
mt@ubuntu:~$ sudo tcpdump -i eth0 port 8888 or icmp -v
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
22:14:23.863178 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 33)
    localhost.46642 > localhost.8888: UDP, length 5
22:14:23.864000 IP (tos 0x0, ttl 64, id 22730, offset 0, flags [none], proto ICMP (1), length 56)
    localhost > localhost: ICMP localhost udp port 8888 unreachable, length 36
        IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 33)
    localhost.46642 > localhost.8888: UDP, length 5

尽管有ICMP回应返回,但客户端没有捕获到该错误,此时阻塞在了read调用上。
##############################实验2 结束############################

另外,调用connect之后,会发现应用程序只会对调用了connect的fd进行相应的操作,如果它同时监听在某fd上,则不会响应该监听fd上的数据。比如参考资料2中提到一个程序先用UDP监听在机器B的9000上,同时用udp connect到另一台机器A的8000端口,结果发现使用其他机器往机器B的9000端口发送数据时,它不会做出响应。



参考:
[1]
 [3]~hollingd/netprog/notes/udp/udp.pdf



阅读(4758) | 评论(0) | 转发(5) |
给主人留下些什么吧!~~