I am trying to bind both UDP and TCP sockets to the same IP address and port, and then handle both types of connections. I have implemented the following code:
The main function initializes both sockets and uses poll()
to monitor them:
int main(int argc, char **argv) {
if (argc != 3) {
return EXIT_FAILURE;
}
struct sockaddr_in addr = derive_sockaddr(argv[1], argv[2]);
int server_socket = setup_server_socket(addr, SOCK_STREAM);
int server_socket_udp = setup_server_socket(addr, SOCK_DGRAM);
struct pollfd sockets[2] = {
{.fd = server_socket, .events = POLLIN},
{.fd = server_socket_udp, .events = POLLIN}
};
struct connection_state state = {0};
while (true) {
int ready = poll(sockets, sizeof(sockets) / sizeof(sockets[0]), -1);
if (ready == -1) {
perror("poll");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < sizeof(sockets) / sizeof(sockets[0]); i++) {
if (sockets[i].revents != POLLIN) {
continue;
}
int s = sockets[i].fd;
if (s == server_socket_udp) {
// Handle UDP
} else if (s == server_socket) {
int connection = accept(server_socket, NULL, NULL);
if (connection == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
close(server_socket);
perror("accept");
exit(EXIT_FAILURE);
} else {
connection_setup(&state, connection);
sockets[0].events = 0;
sockets[1].fd = connection;
sockets[1].events = POLLIN;
}
} else {
assert(s == state.sock);
bool cont = handle_connection(&state);
if (!cont) {
sockets[0].events = POLLIN;
sockets[1].fd = -1;
sockets[1].events = 0;
}
}
}
}
return EXIT_SUCCESS;
}
Here’s the helper function for setting up the sockets:
static int setup_server_socket(struct sockaddr_in addr, int type) {
const int enable = 1;
const int backlog = 1;
int sock = socket(AF_INET, type, 0);
if (sock == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl");
exit(EXIT_FAILURE);
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
if (type == SOCK_DGRAM) {
int broadcast = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) == -1) {
perror("setsockopt(SO_BROADCAST)");
close(sock);
exit(EXIT_FAILURE);
}
}
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("bind");
close(sock);
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "UDP or TCP socket bound to %s:%dn", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
}
if (type == SOCK_STREAM) {
if (listen(sock, backlog)) {
perror("listen");
exit(EXIT_FAILURE);
}
}
return sock;
}
All the other functions work properly it is just the logic im not sure about:
Is it correct and safe to bind both UDP and TCP sockets to the same address and port?
Does the poll() logic properly handle these sockets? Specifically, can I rely on poll() to monitor and differentiate between the TCP connection, the UDP socket, and incoming data?
Are there potential issues or race conditions in the code when switching between handling a UDP socket, accepting a TCP connection, and processing the TCP client connection?
Any insights or suggestions would be greatly appreciated!
6
The set of UDP ports is completely separate from the set of TCP ports.
So there’s no problem having one socket with TCP port n open and another with UDP port n open.
4