1
|
#include <stdlib.h>
|
2
|
#include <unistd.h>
|
3
|
#include <string.h>
|
4
|
#include <stdio.h>
|
5
|
#include <sched.h>
|
6
|
#include <sys/socket.h>
|
7
|
#include <netinet/in.h>
|
8
|
#include <arpa/inet.h>
|
9
|
#include <fcntl.h>
|
10
|
|
11
|
int main(int argc, char **argv) {
|
12
|
struct sched_param param;
|
13
|
int rc;
|
14
|
int socketFd, clientFd;
|
15
|
struct sockaddr_in host;
|
16
|
socklen_t hostlen = sizeof(host);
|
17
|
|
18
|
memset(¶m, 0, sizeof(param));
|
19
|
param.sched_priority = 18;
|
20
|
fprintf(stderr, "Setting SCHED_RR priority %d\n", param.sched_priority);
|
21
|
rc = sched_setscheduler(getpid(), SCHED_RR, ¶m);
|
22
|
if (rc != 0) {
|
23
|
fprintf(stderr, "Setting SCHED_RR failed");
|
24
|
return -1;
|
25
|
}
|
26
|
|
27
|
socketFd = socket(AF_INET, SOCK_STREAM, 0);
|
28
|
if (socketFd < 0)
|
29
|
{
|
30
|
fprintf(stderr, "socket error %d\n", socketFd);
|
31
|
return -1;
|
32
|
}
|
33
|
|
34
|
fcntl(socketFd, F_SETFL, O_NONBLOCK);
|
35
|
|
36
|
memset((void *)&host, 0, sizeof(host));
|
37
|
host.sin_family = AF_INET;
|
38
|
host.sin_addr.s_addr = INADDR_ANY;
|
39
|
host.sin_port = htons(5000);
|
40
|
if (bind(socketFd, (struct sockaddr*)&host, sizeof(host)) < 0)
|
41
|
{
|
42
|
fprintf(stderr, "bind error\n");
|
43
|
return -1;
|
44
|
}
|
45
|
|
46
|
if (listen(socketFd, SOMAXCONN))
|
47
|
{
|
48
|
fprintf(stderr, "listen error\n");
|
49
|
return -1;
|
50
|
}
|
51
|
|
52
|
|
53
|
while (1) {
|
54
|
memset(&host, 0, sizeof(host));
|
55
|
clientFd = accept(socketFd, (struct sockaddr*)&host, &hostlen);
|
56
|
}
|
57
|
return 0;
|
58
|
}
|