Reputation: 308
I'm trying to create a server that listens to two different ports, reads a message from the respective socket and prints it to stdout. When I connect for the first time to any of the available ports, the program works as expected. Then, when I try to connect to the second port, connect() will simply not unblock.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char** argv)
{
struct sockaddr_in addr1;
struct sockaddr_in addr2;
int srv_fd1;
int srv_fd2;
int srv_fd;
int accept_fd;
fd_set srv_fds;
char mem[1024];
if ((srv_fd1 = socket(AF_INET, SOCK_STREAM, 0)) == -1 ||
(srv_fd2 = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "socket(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
addr1.sin_family = AF_INET;
addr1.sin_port = htons(16000);
addr1.sin_addr.s_addr = htonl(INADDR_ANY);
addr2.sin_family = AF_INET;
addr2.sin_port = htons(16001);
addr2.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(srv_fd1, (struct sockaddr *)&addr1, (socklen_t)sizeof(addr1)) == -1 ||
bind(srv_fd2, (struct sockaddr *)&addr2, (socklen_t)sizeof(addr2)) == -1)
{
fprintf(stderr, "bind(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (listen(srv_fd1, 128) == -1 || listen(srv_fd2, 128) == -1) {
fprintf(stderr, "listen(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
FD_ZERO(&srv_fds);
FD_SET(srv_fd1, &srv_fds);
FD_SET(srv_fd2, &srv_fds);
while (select(srv_fd2 +1, &srv_fds, NULL, NULL, NULL) != -1) {
printf("Inside loop\n");
fflush(stdout);
memset(mem, 0, 1024);
if (FD_ISSET(srv_fd1, &srv_fds))
srv_fd = srv_fd1;
else if (FD_ISSET(srv_fd2, &srv_fds))
srv_fd = srv_fd2;
accept_fd = accept(srv_fd, NULL, NULL);
if (accept_fd == -1) {
fprintf(stderr, "accept(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
read(accept_fd, mem, 1023);
printf("%s", mem);
close(accept_fd);
}
close(srv_fd1);
close(srv_fd2);
return 0;
}
Demonstration of the problem with telnet:
$ telnet 127.0.0.1 16000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
FIRST ATTEMPT
Connection closed by foreign host.
$ telnet 127.0.0.1 16001
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SECOND ATTEMPT
^]
telnet> quit
Connection closed.
Server response:
$ ./test
Inside loop
FIRST ATTEMPT
^C
Upvotes: 1
Views: 60
Reputation: 32586
you need to do
FD_ZERO(&srv_fds);
FD_SET(srv_fd1, &srv_fds);
FD_SET(srv_fd2, &srv_fds);
each time before you call select, because that one resets srv_fds
to indicate which fd has something. In your case the first call of select
cancel FD_SET(srv_fd2, &srv_fds);
so the second call of select
only manages srv_fd1
Note doing srv_fd2 +1
you also suppose srv_fd2 > srv_fd1
which can be false.
You also suppose you have only one ready doing
if (FD_ISSET(srv_fd1, &srv_fds))
srv_fd = srv_fd1;
else if (FD_ISSET(srv_fd2, &srv_fds))
srv_fd = srv_fd2;
...
but you can have them both ready.
A way to do can be :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define NSOCK 2
#define FIRST_PORT 16000
/* returns the max descriptor */
int do_fd_set(int * srv_fd, fd_set * srv_fds)
{
int r = 0;
int i;
FD_ZERO(srv_fds);
for (i = 0; i != NSOCK; ++i) {
FD_SET(srv_fd[i], srv_fds);
if (srv_fd[i] > r)
r = srv_fd[i];
}
return r;
}
int main(int argc, char** argv)
{
int srv_fd[NSOCK];
fd_set srv_fds;
int i;
for (i = 0; i != NSOCK; ++i) {
struct sockaddr_in addr;
if ((srv_fd[i] = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "socket(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(FIRST_PORT + i);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(srv_fd[i], (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) == -1)
{
fprintf(stderr, "bind(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (listen(srv_fd[i], 128) == -1) {
fprintf(stderr, "listen(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
while (select(do_fd_set(srv_fd, &srv_fds) + 1, &srv_fds, NULL, NULL, NULL) != -1) {
printf("Inside loop\n");
for (int i = 0; i != NSOCK; ++i) {
if (FD_ISSET(srv_fd[i], &srv_fds)) {
char mem[1024];
int accept_fd;
memset(mem, 0, 1024);
accept_fd = accept(srv_fd[i], NULL, NULL);
if (accept_fd == -1) {
fprintf(stderr, "accept(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
read(accept_fd, mem, sizeof(mem) - 1);
printf("from port %d : %s\n", FIRST_PORT + i, mem);
close(accept_fd);
}
}
}
/* probably never reach that code */
for (i = 0; i != NSOCK; ++i)
close(srv_fd[i]);
return 0;
}
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -Wall so.c
pi@raspberrypi:/tmp $ ./a.out
Inside loop
from port 16000 : aze
Inside loop
from port 16000 : qsd
Inside loop
from port 16001 : wxc
^C
pi@raspberrypi:/tmp $
doing in an other terminal :
pi@raspberrypi:/tmp $ telnet localhost 16000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
aze
Connection closed by foreign host.
pi@raspberrypi:/tmp $ telnet localhost 16000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
qsd
Connection closed by foreign host.
pi@raspberrypi:/tmp $ telnet localhost 16001
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
wxc
Connection closed by foreign host.
Upvotes: 3
Reputation: 73039
bruno's answer diagnoses the problems correctly. As a supplement, here is a working version of the relevant code:
while(1) {
FD_ZERO(&srv_fds);
FD_SET(srv_fd1, &srv_fds);
FD_SET(srv_fd2, &srv_fds);
int maxFD = (srv_fd1 > srv_fd2) ? srv_fd1 : srv_fd2;
if (select(maxFD+1, &srv_fds, NULL, NULL, NULL) == -1) break;
[...]
Upvotes: 2