Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c9f40f

Browse files
committed
Add select() for reading udp socket
Signed-off-by: Seungha Son <seungha.son@samsung.com>
1 parent 252a84c commit 5c9f40f

File tree

1 file changed

+22
-7
lines changed
  • server-client/Multithreaded-UDP-model/src

1 file changed

+22
-7
lines changed

‎server-client/Multithreaded-UDP-model/src/server.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int main()
3232
struct sockaddr_in c_addr;
3333
client_h handle = NULL;
3434
pthread_t thr;
35+
fd_set r_fds;
3536

3637
sfd = socket(AF_INET, SOCK_DGRAM, 0);
3738
if (sfd == -1) {
@@ -58,19 +59,33 @@ int main()
5859
if (handle == NULL)
5960
continue;
6061

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);
6564

66-
ret = pthread_create(&thr, 0, __work, handle);
65+
ret = select(sfd+1, &r_fds, 0, 0, 0);
6766
if (ret < 0) {
68-
perror("pthread_create");
67+
perror("select");
6968
continue;
7069
}
7170

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+
}
7386
}
7487

88+
close(sfd);
89+
7590
return 0;
7691
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /