-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_multiple_connections.c
216 lines (179 loc) · 5.77 KB
/
tcp_multiple_connections.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
*
// Adapted from https://beej.us/guide/bgnet/html/#fnref29 (pollserver.c by
// Brian “Beej Jorgensen” Hall <https://beej.us/>)
tcp_multiple_connections.c
https://github.com/andy5995/c_networking_examples
MIT License
Copyright (c) 2022 Andy Alt and James Sherratt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include "netex.h"
// Get sockaddr, IPv4 or IPv6:
void *
get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in *) sa)->sin_addr);
}
return &(((struct sockaddr_in6 *) sa)->sin6_addr);
}
// Add a new file descriptor to the set
void
add_to_pfds(struct pollfd *pfds[], int newfd, int *fd_count, int *fd_size)
{
// If we don't have room, add more space in the pfds array
if (*fd_count == *fd_size)
{
*fd_size *= 2; // Double it
*pfds = realloc(*pfds, sizeof(**pfds) * (*fd_size));
if (pfds == NULL)
{
perror("malloc");
exit(errno);
}
}
(*pfds)[*fd_count].fd = newfd;
(*pfds)[*fd_count].events = POLLIN; // Check ready-to-read
(*fd_count)++;
}
// Remove an index from the set
void
del_from_pfds(struct pollfd pfds[], int i, int *fd_count)
{
// Copy the one from the end over this one
pfds[i] = pfds[*fd_count - 1];
(*fd_count)--;
}
int
main(int argc, char *argv[])
{
parse_server_opts(argc, argv);
struct sockaddr_storage remoteaddr; // Client address
socklen_t addrlen;
if (get_tcp_server_sockfd() < 0)
{
fputs("Error\n", stderr);
return -1;
}
// Start off with room for 5 connections
// (We'll realloc as necessary)
int fd_count = 0;
int fd_size = 5;
struct pollfd *pfds = malloc(sizeof *pfds * fd_size);
if (pfds == NULL)
{
perror("malloc");
return errno;
}
// Add the listener to set
pfds[0].fd = conn_inf.sockfd;
pfds[0].events = POLLIN; // Report ready to read on incoming connection
fd_count = 1; // For the listener
// Main loop
for (;;)
{
int poll_count = poll(pfds, fd_count, -1);
if (poll_count == -1)
{
perror("poll");
break;
}
// Run through the existing connections looking for data to read
for (int i = 0; i < fd_count; i++)
{
// Check if someone's ready to read
if (pfds[i].revents & POLLIN)
{ // We got one!!
if (pfds[i].fd == conn_inf.sockfd)
{
// If listener is ready to read, handle new connection
addrlen = sizeof remoteaddr;
// Newly accept()ed socket descriptor
int newfd = accept(conn_inf.sockfd, (struct sockaddr *) &remoteaddr,
&addrlen);
if (newfd == -1)
{
perror("accept");
}
else
{
add_to_pfds(&pfds, newfd, &fd_count, &fd_size);
char remoteIP[INET6_ADDRSTRLEN];
printf("pollserver: new connection from %s on "
"socket %d\n",
inet_ntop(remoteaddr.ss_family,
get_in_addr((struct sockaddr *) &remoteaddr),
remoteIP, INET6_ADDRSTRLEN), newfd);
}
}
else
{
// If not the listener, we're just a regular client
int sender_fd = pfds[i].fd;
char buf[256]; // Buffer for client data
int nbytes = recv(sender_fd, buf, sizeof buf, 0);
if (nbytes <= 0)
{
// Got error or connection closed by client
if (nbytes == 0)
{
// Connection closed
printf("pollserver: socket %d hung up\n", sender_fd);
}
else
{
perror("recv");
}
close(pfds[i].fd); // Bye!
del_from_pfds(pfds, i, &fd_count);
}
else
{
// We got some good data from a client
for (int j = 0; j < fd_count; j++)
{
// Send to everyone!
int dest_fd = pfds[j].fd;
// Except the listener and ourselves
// Except the listener and ourselves
if (dest_fd != conn_inf.sockfd && dest_fd != sender_fd)
{
if (send(dest_fd, buf, nbytes, 0) == -1)
{
perror("send");
}
}
}
}
} // END handle data from client
} // END got ready-to-read from poll()
} // END looping through file descriptors
}
// END for(;;)--and you thought it would never end!
free(pfds);
return errno;
}