Chinaunix首页 | 论坛 | 博客
  • 博客访问: 894692
  • 博文数量: 119
  • 博客积分: 2493
  • 博客等级: 大尉
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 14:00
文章分类

全部博文(119)

文章存档

2013年(19)

2012年(100)

分类: LINUX

2012-10-14 10:44:14

1,我自己写了一个UDP 的小程序,有一个UDP 的server,而且有UDP的client。
      然后执行server和client,然后用tcpdump将该端口的UDP数据报文抓取出来。
      执行的过程是这样的。
      client向server发送"xiyou"
     server向client应答"wangzhe"
      client程序在主机example上运行(192.168.1.144)
      server程序在主机linux上运行(192.168.1.101)
------------------------------------------------------------------------------------------------------------
2,UDP数据报文。

  1. linux@linux:~$ sudo tcpdump -vvv -X  udp port 7777
  2. [sudo] password for linux: 
  3. tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
  4. 11:03:01.923227 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 48)
  5.     example.local.43521 > linux.7777: [udp sum ok] UDP, length 20
  6. 0x0000:  4500 0030 0000 4000 4011 b677 c0a8 0190  E..0..@.@..w....
  7. 0x0010:  c0a8 0165 aa01 1e61 001c 4c34 7869 796f  ...e...a..L4xiyo
  8. 0x0020:  7500 0000 0000 0000 0000 0000 0000 0000  u...............
  9. 11:03:01.923343 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 48)
  10.     linux.7777 > example.local.43521: [bad udp cksum 6869!] UDP, length 20
  11. 0x0000:  4500 0030 0000 4000 4011 b677 c0a8 0165  E..0..@.@..w...e
  12. 0x0010:  c0a8 0190 1e61 aa01 001c 8473 7761 6e67  .....a.....swang
  13. 0x0020:  7a68 6500 0000 0000 0000 0000 0000 0000  zhe.............
由上面的报文可知,有两个UDP数据报文。
第一个报文是example主机上的client向server发送数据。
4500 0030 0000 4000 4011 b677 c0a8 0190 c0a8 0165 这20个数据是IP首部。
aa01 1e61 001c 4c34 这8个字节是UDP的首部。
7869 796f 7500 0000 0000 0000 0000 0000 0000 0000 这20个数据是我用sendto函数发送的
数据。

而将char req[20] = "xiyou" 的ASCII码(16进制)就是:
78 69 79 6f 75 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

第二个报文是linux向主机example做出的应答。
4500 0030 0000 4000 4011 b677 c0a8 0165 c0a8 0190 这20个数据是IP首部。
1e61 aa01 001c 8473 这8个字节是UDP首部。
7761 6e67 7a68 6500 0000 0000 0000 0000 0000 0000 这20个数据是应用层的数据。

而将char reply[20] = "wangzhe"的ASCII码(16进制)就是:
77 61 6e 67 7a 68 65 0 0 0 0 0 0 0 0 0 0 0 0 0

由此看出,应用层的数据没有夹杂其他的参数,全部是数据,均是字符的ASCII码。
-----------------------------------------------------------------------
附带网络程序:
udp_server.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <sys/types.h>
  6. #include <string.h>

  7. int main(void)
  8. {
  9.         struct sockaddr_in server,client;
  10.         int sockfd;
  11.         int cli_len = 0,n;
  12.         char req[20] = {0},reply[20] = {0};
  13.         
  14.         sockfd = socket(AF_INET,SOCK_DGRAM,0);
  15.         if (sockfd < 0) {
  16.                 perror("socket error!\n");
  17.                 exit(-1);
  18.         }
  19.         
  20.         memset(&server,0,sizeof(struct sockaddr_in));
  21.         server.sin_family = AF_INET;
  22.         server.sin_addr.s_addr = htonl(INADDR_ANY);
  23.         server.sin_port = htons(7777);
  24.         
  25.         if (bind(sockfd,(struct sockaddr *)&server,sizeof(server)) < 0) {
  26.                 perror("bind error!\n");
  27.                 exit(-1);
  28.         }
  29.         
  30.         for (;;) {
  31.                 cli_len = sizeof(struct sockaddr_in);
  32.                 n = recvfrom(sockfd,req,20,0,(struct sockaddr *)&client,&cli_len);
  33.                 if (n < 0) {
  34.                         perror("recvfrom error!\n");
  35.                         exit(-1);
  36.                 }
  37.                 printf("hello!\n");
  38.                 
  39.                 strncpy(reply,"wangzhe",sizeof("wangzhe"));
  40.                 if (sendto(sockfd,reply,20,0,(struct sockaddr *)&client,sizeof(client)) != 20) {
  41.                         perror("sendto error!\n");
  42.                         exit(-1);
  43.                 }
  44.         }
  45.         return 0;
  46. }
-----------------------------------------------------------------------------------------------------------
udp_client.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <sys/types.h>
  6. #include <string.h>

  7. int main(void)
  8. {
  9.         int sockfd,n;
  10.         struct sockaddr_in server;
  11.         char req[20]={0},reply[20]={0};
  12.         
  13.         sockfd = socket(AF_INET,SOCK_DGRAM,0);
  14.         if (sockfd < 0) {
  15.                 perror("socket error!\n");
  16.                 exit(-1);
  17.         }
  18.         
  19.         memset(&server,0,sizeof(server));
  20.         server.sin_family = AF_INET;
  21.         server.sin_addr.s_addr = inet_addr("192.168.1.101");
  22.         server.sin_port = htons(7777);
  23.         
  24.         strncpy(req,"xiyou",sizeof("xiyou"));
  25.         
  26.         printf("sendto req to server:%s\n",req);
  27.         
  28.         if (sendto(sockfd,req,20,0,(struct sockaddr *)&server,sizeof(server)) != 20) {
  29.                 perror("sendto error!\n");
  30.                 exit(-1);
  31.         }
  32.         
  33.         if ((n = recvfrom(sockfd,reply,20,0,(struct sockaddr *)NULL,(int *)NULL)) < 0) {
  34.                 perror("recvfrom error!\n");
  35.                 exit(-1);
  36.         }
  37.         
  38.         printf("recv reply from server :%s\n",reply);
  39.         
  40.         exit(0);
  41. }
--------------------------------------------------------------------------------------------------------



阅读(11285) | 评论(0) | 转发(1) |
0

上一篇:dns

下一篇:TTL字段

给主人留下些什么吧!~~