@@ -32,6 +32,7 @@ int main()
32
32
struct sockaddr_in c_addr ;
33
33
client_h handle = NULL ;
34
34
pthread_t thr ;
35
+ fd_set r_fds ;
35
36
36
37
sfd = socket (AF_INET , SOCK_DGRAM , 0 );
37
38
if (sfd == -1 ) {
@@ -58,19 +59,33 @@ int main()
58
59
if (handle == NULL )
59
60
continue ;
60
61
61
- handle -> buf_size = recvfrom (sfd , handle -> buf , CLIENT_BUF_MAX , 0 ,
62
- (struct sockaddr * )& handle -> sock_addr , & handle -> sock_len );
63
- if (handle -> buf_size < 0 )
64
- continue ;
62
+ FD_ZERO (& r_fds );
63
+ FD_SET (sfd , & r_fds );
65
64
66
- ret = pthread_create ( & thr , 0 , __work , handle );
65
+ ret = select ( sfd + 1 , & r_fds , 0 , 0 , 0 );
67
66
if (ret < 0 ) {
68
- perror ("pthread_create " );
67
+ perror ("select " );
69
68
continue ;
70
69
}
71
70
72
- pthread_detach (thr );
71
+ if (FD_ISSET (sfd , & r_fds )) {
72
+ handle -> buf_size = recvfrom (sfd , handle -> buf , CLIENT_BUF_MAX , 0 ,
73
+ (struct sockaddr * )& handle -> sock_addr , & handle -> sock_len );
74
+ if (handle -> buf_size < 0 )
75
+ continue ;
76
+ printf ("%ld" , handle -> buf_size );
77
+
78
+ ret = pthread_create (& thr , 0 , __work , handle );
79
+ if (ret < 0 ) {
80
+ perror ("pthread_create" );
81
+ continue ;
82
+ }
83
+
84
+ pthread_detach (thr );
85
+ }
73
86
}
74
87
88
+ close (sfd );
89
+
75
90
return 0 ;
76
91
}
0 commit comments