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