-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
107 lines (88 loc) · 2.5 KB
/
server.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
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include <fcntl.h>
#include <event.h>
#include <errno.h>
#include <linux/if.h>
#define D_READ_BUFFER 255
#define D_MAX_BUFFER 4095
#define LIBEVENT
int createDgramSocket (void)
{
return socket (AF_INET, SOCK_DGRAM, 0);
}
int bindToSocket (int i32Socket, struct sockaddr_in *pSockAddr)
{
return bind (i32Socket, (struct sockaddr *)pSockAddr, sizeof (struct sockaddr));
}
void readEvent (int i32Socket, short event, void *pVoidArg)
{
char i8Buffer [D_MAX_BUFFER + 1] = {0};
struct sockaddr_in clientAddr;
memset (&clientAddr, 0, sizeof (struct sockaddr_in));
int i32RcvSize = -1;
int sockSize = sizeof (struct sockaddr);
i32RcvSize = recvfrom (i32Socket, i8Buffer, D_MAX_BUFFER, 0, (struct sockaddr *)&clientAddr, (socklen_t *)&sockSize);
if (i32RcvSize < 0)
{
return;
}
printf("Mesage received=%s\n",i8Buffer);
return;
}
int main (int argc, char **argv)
{
struct sockaddr_in servAddr1;
struct sockaddr_in servAddr2;
struct event servEvent1;
struct event servEvent2;
int i32ServerFd1 = -1;
int i32ServerFd2 = -1;
i32ServerFd1 = createDgramSocket ();
i32ServerFd2 = createDgramSocket ();
if (i32ServerFd1 < 0 || i32ServerFd2 < 0)
{
printf("Socket creation failed: %s\n", strerror (errno));
exit (EXIT_FAILURE);
}
memset (&servAddr1, 0, sizeof (struct sockaddr_in));
memset (&servAddr2, 0, sizeof (struct sockaddr_in));
servAddr1.sin_family = AF_INET;
servAddr1.sin_addr.s_addr = htonl (INADDR_ANY);
servAddr1.sin_port = htons (5000);
servAddr2.sin_family = AF_INET;
servAddr2.sin_addr.s_addr = htonl (INADDR_ANY);
servAddr2.sin_port = htons (5001);
if (bindToSocket (i32ServerFd1, &servAddr1) < 0)
{
printf("Socket bind failed: %s\n", strerror (errno));
exit (EXIT_SUCCESS);
}
if (bindToSocket (i32ServerFd2, &servAddr2) < 0)
{
printf("Socket bind failed: %s\n", strerror (errno));
exit (EXIT_SUCCESS);
}
#ifdef LIBEVENT
event_init ();
event_set (&servEvent1, i32ServerFd1, EV_READ|EV_PERSIST, readEvent, &servEvent1);
event_set (&servEvent2, i32ServerFd2, EV_READ|EV_PERSIST, readEvent, &servEvent2);
event_add (&servEvent1, NULL);
printf("Added event to listen on port 5000\n");
event_add (&servEvent2, NULL);
printf("Added event to listen on port 5001\n");
event_dispatch();
#else
readEvent(i32ServerFd1,0,NULL);
#endif
exit (EXIT_SUCCESS);
}