-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect_demo.cpp
executable file
·112 lines (99 loc) · 2.21 KB
/
select_demo.cpp
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
107
108
109
110
111
112
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <sys/select.h>
#include <vector>
#include <algorithm>
#include <cassert>
int main(int argc, char* argv[])
{
if(argc != 2)
{
std::cout << "Error: Usage is ./server <listen_port>\n";
return 1;
}
int portNum = atoi(argv[1]);
// Bind server to sd and set up listen server
int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in self;
self.sin_family = AF_INET;
self.sin_addr.s_addr = INADDR_ANY;
self.sin_port = htons((u_short) portNum);
int err = bind(sd, (struct sockaddr*) &self, sizeof(self));
if(err == -1)
{
std::cout << "Error binding the socket to the port number\n";
return 1;
}
err = listen(sd, 10);
if(err == -1)
{
std::cout << "Error setting up listen queue\n";
return 1;
}
// Set of file descriptors to listen to
fd_set readSet;
// Keep track of each file descriptor accepted
std::vector<int> fds;
while(true)
{
// Set up the readSet
FD_ZERO(&readSet);
FD_SET(sd, &readSet);
for(int i = 0; i < (int) fds.size(); ++i)
{
FD_SET(fds[i], &readSet);
}
int maxfd = 0;
if(fds.size() > 0)
{
maxfd = *std::max_element(fds.begin(), fds.end());
}
maxfd = std::max(maxfd, sd);
// maxfd + 1 is important
int err = select(maxfd + 1, &readSet, NULL, NULL, NULL);
assert(err != -1);
if(FD_ISSET(sd, &readSet))
{
int clientsd = accept(sd, NULL, NULL);
if(clientsd == -1)
{
std::cout << "Error on accept" << std::endl;
}
else
{
fds.push_back(clientsd);
}
}
for(int i = 0; i < (int) fds.size(); ++i)
{
if(FD_ISSET(fds[i], &readSet))
{
char buf = 'x';
int bytesRecvd = recv(fds[i], &buf, 1, 0);
if(bytesRecvd < 0)
{
std::cout << "Error recving bytes" << std::endl;
std::cout << strerror(errno) << std::endl;
exit(1);
}
else if(bytesRecvd == 0)
{
std::cout << "Connection closed" << std::endl;
fds.erase(fds.begin() + i);
}
buf = 'a' + fds[i];
int bytesSent = send(fds[i], &buf, 1, 0);
if(bytesSent <= 0)
{
std::cout << "Error sending bytes" << std::endl;
}
}
}
}
}