Hello to everyone,
I'm trying to run two UDP server using the function select().
Here is the code, further information follows below:
Code:
int main(void)
{
int port1 = 1234;
int port2 = 1322;
fd_set read;
const int buf = 1024;
char data[buf];
char data_2[buf];
long size;
int ret;
int max_socket_number;
ConnLessServer testServer1(port1); //first server is opened with given port
ConnLessServer testServer2(port2); //second server is opened with given port
FD_ZERO(&read);
FD_SET(testServer1.getSocket(), &read); //returns the socket filedeskriptor
FD_SET(testServer2.getSocket(), &read);
fd_vector.push_back(testServer1.getSocket());
fd_vector.push_back(testServer2.getSocket());
max_socket_number = *max_element(fd_vector.begin(), fd_vector.end());
do
{
ret = select(max_socket_number + 1, &read, NULL, NULL, NULL);
std::cout << std::endl << ret << std::endl;
if(FD_ISSET(testServer1.getSocket(), &read))
{
testServer1.receive(data, size, buf);
fprintf(stdout, "Server 1: %ld %s\n", size, data);
}
if(FD_ISSET(testServer2.getSocket(), &read))
{
testServer2.receive(data_2, size, buf);
fprintf(stdout, "Server 2: %ld %s\n", size, data_2);
}
}
while (0 != strcmp(data_2, "quit"));
testServer1.close();
testServer2.close();
return 0;
}
Intention is to send messages to the first socket, which works fine.
The second socket is intented to receive one message, after which the "do-while"-loop should be left.
Problem is, that the second message doesn't seem to be recognized and the message "quit" is ignored. Using a debugger, everything seems to work fine, "quit" is received and the loop is left.
In case, more information is needed, I can post some code of the member functions, I wrote myself (e.g. for the class ConnLessServer).
Best regards,
PiJ.