-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnblk_svr.c
100 lines (89 loc) · 1.75 KB
/
nblk_svr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "rudp.h"
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
struct sockaddr_in sai;
RUDPSOCKET s;
RUDPStart();
s = RUDPSocket();
memset(&sai, 0, sizeof(sai));
sai.sin_family = AF_INET;
sai.sin_addr.s_addr = argc==2?inet_addr(argv[1]):htonl(INADDR_ANY);
sai.sin_port = htons(5001);
if(RUDPBind(s, (struct sockaddr*)&sai, sizeof(sai)) < 0)
{
perror("bind");
return -1;
}
RUDPListen(s, 5);
int opt = 1, rlt = 0;
//RUDPSetSockOpt(s, OPT_NBLK, &opt, sizeof(opt));
while(1)
{
int r;
struct timeval tv = { 1, 0 };
#if 0
r = RUDPSELECT_READABLE;
rlt = RUDPSelectSock(s, -1, &r, &tv);
if(rlt < 0)
{
printf("RUDPSelectSock: %d\n", rlt);
break;
}
if(rlt == 0)
{
printf("No incoming connection\n");
continue;
}
#else
RUDPSOCKCHNO r_schs[2];
int n_schs = 0;
RUDP_SET(s, -1, r_schs, n_schs);
rlt = RUDPSelect(r_schs, &n_schs, NULL, 0, NULL, 0, &tv);
if(rlt < 0)
{
printf("RUDPSelect: %d\n", rlt);
break;
}
if(rlt == 0)
{
printf("No incoming connection\n");
continue;
}
#endif
int sa_len = sizeof(sai);
RUDPSOCKET a;
if(RUDPAccept(s, &a, (struct sockaddr*)&sai, &sa_len) < 0)
{
printf("accept error\n");
}
else
{
char line[1100];
int len, chno;
tv.tv_sec = 1; tv.tv_usec = 0;
while(1)
{
r = RUDPSELECT_READABLE;
if( (r = RUDPSelectSock(a, -1, &r, &tv)) < 0 )
{
printf("RUDPSelectSock on accepted socket: %d\n", r);
break;
}
if(r == 0) { continue; }
if( (len = RUDPRecv(a, &chno, line, 1000, 0)) > 0)
{
line[len] = '\0';
//printf("%s\n", line);
if(line[0] == '\0') break;
}
}
printf("receiving finished.\n");
RUDPClose(a);
}
}
RUDPClose(s);
RUDPCleanup();
return 0;
}