Chinaunix首页 | 论坛 | 博客
  • 博客访问: 743904
  • 博文数量: 741
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 4825
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-18 11:18
文章分类

全部博文(741)

文章存档

2011年(1)

2008年(740)

我的朋友

分类:

2008-09-18 11:23:42

写一个程序想要接受网络上的UDP数据包,可是多数的数据包却蒸发掉了.
0 A( `6 ]! ]) K; ^$ m3 o8 p简化源码:
% t: }5 p6 C+ D0 j4 t    int sock;
. X$ L* u/ Z# A0 f$ Z9 a    struct sockaddr_in addr;2 i) N  M. A" c( @
    socklen_t len;* l5 N- v* L* ]3 ~! V, v
    char buf[4096];
1 m4 E' r; t0 g: w# Q. }    size_t size;# G. \  _. M& L% P$ K
  ^1 `( o5 N5 N8 x2 e3 ?' f# M
    sock = Socket(PF_INET,SOCK_DGRAM,0);
# _) e3 d1 A9 J2 l3 p+ I! P    bzero(&addr,sizeof(addr));
9 k; N6 ^) B4 w! Z' G    addr.sin_family = AF_INET;
5 b2 b. @2 D& p/ Y3 }1 a    addr.sin_port = htons(2425);7 d* \; ]8 r5 R
    addr.sin_addr.s_addr = htonl(INADDR_ANY);) Z+ Y& d$ O" h9 W. b; {, U) a0 I
    bind(sock,(SA*)&addr,sizeof(addr))6 I1 D. e2 o! K7 D
    while (1) {
/ s( w. V2 s: o$ j. [3 A        len = sizeof(addr);( n3 q6 w2 j: L6 U. q% g3 q
        size = recvfrom(sock,buf,4096,0,&addr,&len);9 _* r! F$ Q# }( x, x* X2 v
        buf[size] = '\0';# |* ~) R3 z. f; K1 D
        printf(buf);' g5 S2 w- J, m3 b) t! g
    }
0 j) D3 O, L6 m程序 tcpdump (tcpdump -i eth0 -A 'udp and port 2425') 能够抓取到数据包,如下:
( I" Y3 T( x& k# J, e20:46:27.155650 IP 192.168.1.103.fjitsuappmgr > 192.168.1.115.35325: UDP, length 50
7 ?/ g# \' g. b: `- ^) ?) ~[email=E..N....@......g...s]E..N....@......g...s[/email]    y...:._1:1221252224:OwnerENOVO-29C59B44:6291459:Owner.* N9 x5 C9 I, N. J/ I+ L/ c
可是我的程序却什么也得不到.
" |8 v3 A5 ?: c6 y愿各位高手不吝赐教!
% y; V/ s: w8 M
3 s0 v9 k3 d$ t' n/ w5 h[ 本帖最后由 jallyx 于 2008-9-16 15:30 编辑 ]      
--------------------next---------------------
引用:
-(dearvoid@:Forum)-(~/void/c/network)-( J5 b: w0 V9 ^% s' c, }
[$$=3891 $?=0]
; cat udp_1.c
1 B3 A# }6 R" H/ t
#include
: v/ j( k" ~6 a& Z5 W#include ; o6 n3 W7 I1 b  I$ Z
#include - S1 u5 O6 n( j" D* }. @
#include , ?8 Y$ P  S9 I* P+ Y; W
#include 1 S/ z0 V$ [; x  c
#include
( G/ t" B$ N# S- G0 c, s, H8 t#include
3 V9 o9 w# C. E& m# W1 ?' [#include - N+ B( W6 ]6 ~
#include
1 o- B8 V$ Q4 |#include
' i2 I& |+ [! ^& l6 }#include $ N+ X* U5 ?0 N; t4 \$ }- h  J8 a
#include
" V# b5 |  d& i( ~
3 M" m) p2 C3 x6 j- e. r; Y4 Vconst int g_serverPort = 20001;
, d+ B6 \3 t; [$ z6 I! G
! S( W: Q5 s# u* @) A+ B, P7 @int
* M) R, l6 k1 }main()
" r' c. ~0 C' u$ Q$ {6 B{
& ]- U! o& o1 |  \1 u( a+ ^    int sockServer, len;
& y# u4 N' o% r. A5 T    char buf[1024];
: e& O" C- f: U* s* T8 \$ |6 a; N    struct sockaddr_in saServer, saPeer;
. |: }4 ?9 Z% u3 E    socklen_t socklen;8 C1 L* x4 d/ g

! ~  A% Q3 y& x    sockServer = socket(AF_INET, SOCK_DGRAM, 0);3 m( j; C  |7 B- X
    if (sockServer < 0) {2 f6 }# \' m7 n% i
        perror("socket");  `8 c: S8 s6 @8 Y" s2 H- n
        exit(1);
8 F% ^. _# i: }4 Y* t* j    }1 Y2 c. @  W" c% Y+ [- ~) a

7 Z' W, g/ x1 Q    memset(&saServer, 0, sizeof saServer);
( \0 v) O6 c& i- G$ L6 O    saServer.sin_family = AF_INET;& [; [: ~  p5 S. p
    saServer.sin_addr.s_addr = INADDR_ANY;
2 `# q. m% f( P9 Q  Z    saServer.sin_port = htons(g_serverPort);( a# z' ]! }" B
    if (bind(sockServer, (void *)&saServer, sizeof saServer) < 0) {- u+ @5 K% a2 G1 r
        perror("bind");
2 {; V2 f% J  ]* z& C( h4 T        exit(1);  G% Z) J+ r) T) Y; E
    }
; {' ?  p7 i8 u$ o* d: |9 U( z1 f3 e! r
    /*
1 x7 s8 e% w- E1 q8 ]4 s( g/ L     * don't forget to init `socklen' !/ ^" j( k/ _1 d5 `7 c2 z
     */0 y  L* Y5 V- X5 y- U
    socklen = sizeof saPeer;
9 _  }$ C: g7 R2 k8 Y: M1 N8 N    len = recvfrom(sockServer, buf, sizeof buf, 0,
! y# P/ ^9 x  n5 R+ ~4 y                   (void *)&saPeer, &socklen);  z0 c6 x1 k- A. a* @) |5 p! F$ _
    if (len < 0) {2 e0 T& C( z1 T  P
        perror("recvfrom");2 A9 A5 L, C* s
        exit(1);5 ?7 Q' A6 N/ K5 b
    }! q. T0 I! g2 i2 f/ c# c$ a( S
    if (len) {
% ~7 C. B* x$ E/ Q  f8 z        buf[len] = 0;2 {1 ]: \. |0 |' W* }
        printf(">>> Received %d bytes from %s:%d\n", len,5 s( a5 Z* e. q9 u4 K$ m
               inet_ntoa(saPeer.sin_addr), ntohs(saPeer.sin_port));
+ n; V2 ^# ~" `" |# `7 {( r        printf("%s\n", buf);* b7 A1 D( U. o% @' I' N1 \3 ^
    }4 z4 C: d0 f5 s; t
7 N; a3 G: c9 Z; R; k0 Z2 L
    close(sockServer);
$ h3 b- f5 Y  f, P- H- m+ o! y; T
    return 0;  @( F, p, L5 c4 ]9 @' F
}. J( e! _# x1 R2 I
-(dearvoid@:Forum)-(~/void/c/network)-4 o7 O6 b7 x5 o0 E0 r& B
[$$=3891 $?=0]
; gcc udp_1.c2 g: V$ c! t. s+ g9 `# e1 y2 _
-(dearvoid@:Forum)-(~/void/c/network)-
! t7 ~1 H  p: b! I [$$=3891 $?=0]
; { sleep 10; echo -n 'hello, world' > /dev/udp/localhost/20001; } &
  I' o6 i- U  g
[1] 52957 ^5 |  o0 }' x: i; X
-(dearvoid@:Forum)-(~/void/c/network)-
6 N5 x7 Q) J0 e/ k' l; v% W! \0 S0 n9 w [$$=3891 $?=0]
; ./a.out
& o1 I# [9 S* T1 S+ @: A* w$ v
>>> Received 12 bytes from 127.0.0.1:32768
$ J( A  S  h0 o% [1 E& N( b8 l% Thello, world8 e" C. G( k" V: f' a8 C
[1]+  Done                    { sleep 10; echo -n 'hello, world' > /dev/udp/localhost/20001; }
' w5 K! w6 R+ L/ S/ o; X, |-(dearvoid@:Forum)-(~/void/c/network)-- S7 ?* `' s! W+ F& P# L
[$$=3891 $?=0]
; bye
# _: Y1 T0 y4 Y/ c

--------------------next---------------------

阅读(302) | 评论(0) | 转发(0) |
0

上一篇:欢迎阅读我的文章

下一篇:2008-9-17 15:33

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