-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.practical.work.server.multiplex.c
87 lines (84 loc) · 2.82 KB
/
09.practical.work.server.multiplex.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
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
int sockfd, clientfd;
socklen_t clen;
ssize_t messageSize;
struct sockaddr_in saddr, caddr;
char message[256];
unsigned short port = 8782;
//create socket()
if ((sockfd=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error creating socket\n");
}
//setsockopt() - reuse address/
setsockopt(sockfd, SOL_SOCKET,
SO_REUSEADDR, &(int){ 1 },
sizeof(int));
//fcntl() - nonblocking
int fl = fcntl(sockfd, F_GETFL, 0);
fl |= O_NONBLOCK;
fcntl(sockfd, F_SETFL, fl);
//bind()
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(port);
if ((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)) {
printf("Error binding\n");
}
//listen()
if (listen(sockfd, 5) < 0) {
printf("Error listening\n");
}
int clientfds[100]; // list of connected clients, >0 if valid
memset(clientfds, 0, sizeof(clientfds)); // clear the list
while(1){
fd_set set; // declaration of the set
FD_ZERO(&set); // clear the set
FD_SET(sockfd, &set); // add listening sockfd to set
int maxfd = sockfd; // a required value to pass to select()
for (int i = 0; i < 100; i++) {
// add connected client sockets to set
if (clientfds[i] > 0) FD_SET(clientfds[i], &set);
if (clientfds[i] > maxfd) maxfd = clientfds[i];
}
// poll and wait, blocked indefinitely
select(maxfd+1, &set, NULL, NULL, NULL);
// a «listening» socket?
if (FD_ISSET(sockfd, &set)) {
clientfd = accept(sockfd, (struct sockaddr *) &saddr, &clen);
// make it nonblocking
fl = fcntl(clientfd, F_GETFL, 0);
fl |= O_NONBLOCK;
fcntl(clientfd, F_SETFL, fl);
// add it to the clientfds array
for (int i = 0; i < 100; i++) {
if (clientfds[i] == 0) {
clientfds[i] = clientfd;
break;
}
}
}
for (int i = 0; i < 100; i++) {
if (clientfds[i] > 0 && FD_ISSET(clientfds[i], &set)) {
if (read(clientfds[i], message, sizeof(message)) > 0) {
printf("client %d says: %s\nserver>", clientfds[i], message);
}
else {
// some error. remove it from the "active" fd array
printf("client %d has disconnected.\n", clientfds[i]);
clientfds[i] = 0;
}
}
}
}
return 0;
}