按着版主的说法我改了,没问题,应用感觉上和微软的ping 以及linux 的ping 没什么区别,我又加了个select,可是好像不好用,无论怎样都是超时,下面的是我的超时。
; k/ [6 v. B5 W- l6 J) |; X1 _7 A2 |& V% F: h
' D2 }" x$ ^* [& Y
void sock_select(int fd, int sige) //fd:套接字描述符。 sige:标志位,1为read,0位write
2 I- S9 ~( }) C7 r{! Z, o) W0 ~. n
int fs_sel, s_fd;
9 I, E! ]0 x9 \3 \# n fd_set fd_read, fd_write;. z9 Z1 I5 ^3 d. [
struct timeval o_timeout;
8 w3 j% j) f& k/ }+ c! U! h s_fd = fd;" N( c i. ?3 y; v5 s
o_timeout.tv_sec = 2;
9 x/ w" ~; N: z2 {9 D$ V) ]: x o_timeout.tv_usec = 0;
8 j7 b% V: Z2 U2 @0 D4 h+ a if (sige) 0 \( M8 I) Q# g6 d3 }' s( ?
{
; m- I0 |. z+ }+ Z d, ~! _ FD_ZERO(&fd_read);1 X# h0 P2 M8 i- B7 Y
FD_SET(s_fd, &fd_read);$ C0 P7 J( I; f* V' m, h! p& _
fs_sel = select(s_fd, &fd_read, NULL, NULL, &o_timeout);7 ?! @) _1 I' P# P6 ~5 c/ @1 j
if (fs_sel == -1)
0 m7 M3 M2 T! A8 w5 E! B1 y printf("read error\n");
8 s# L) |3 u4 w V8 M* b5 P8 b else if (fs_sel == 0)
9 z- V4 e- \! r' n% c: Z printf("read time out\n");
! {3 a2 f2 k+ Y* q; }0 q9 P' ] }
+ W& I b' W7 Y else if (!sige){
- m3 a9 d" d0 ~% W, D FD_ZERO(&fd_write);
2 y& L2 ^$ \3 V' N FD_SET(s_fd, &fd_write);, C; r, m0 G+ W& c1 E/ a0 d
fs_sel = select(s_fd, NULL, &fd_write, NULL, &o_timeout);
0 A8 t" A5 V; b+ Y if (fs_sel == -1)$ U0 O1 Y" s; y7 N9 @
printf("write error\n");0 W* Y" T& n. O# x0 g9 B
else if (fs_sel == 0)9 m9 u3 t) P1 `; S9 @1 L
printf("write time out\n");( X" [$ N6 l! p; e* @
+ V% y, h' [( V" T}