-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
72 lines (63 loc) · 1.34 KB
/
main.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
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include "lib/common.h"
#include "lib/utils.h"
#include "lib/session.h"
#define PORT 4000
#define LISTEN_BACKLOG 10
int main(int argc, char **argv)
{
int32 listenSockFd;
//1. listen
{
if ((listenSockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
ut_merror("socket");
exit(1);
}
int sockoptEnable = 1;
if (setsockopt(listenSockFd, SOL_SOCKET, SO_REUSEADDR, &sockoptEnable, sizeof(int)) == -1)
{
ut_merror("setsockopt");
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(PORT);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((bind(listenSockFd, (struct sockaddr *) &saddr, sizeof(struct sockaddr_in))) == -1)
{
ut_merror("bind");
exit(1);
}
if (listen(listenSockFd, LISTEN_BACKLOG) == -1)
{
ut_merror("listen");
exit(1);
}
printf("Listening on %d\n", PORT);
}
//2. accept
for(;;)
{
int result = accept(listenSockFd, NULL, NULL);
if (result == -1 && errno == EINTR)
{
continue;
}
else if (result == -1)
{
ut_merror("accept");
exit(1);
}
else
{
ss_onConnect(result);
}
}
return 0;
}