In my C application, I wait for data on the socket in the following way:
printf("Opening socket and wait for data.\n");
while (i < 5)
while((connection_fd = accept(socket_fd,
(struct sockaddr *) &address,
&address_length)) > -1)
{
bzero(buffer, 64);
n = read(connection_fd,buffer,64);
if (n < 0) printf("ERROR reading from socket");
printf("Here is the message of length %d bytes:\n\n", n);
for (int i = 0; i < n; i++)
{
printf("%02X", buffer[i]);
}
printf("\n\n");
break;
}
i++
}
That means I read 5 times data from the Socket, however, by the looks of it I seem to be opening 5 different connections is that right? Is it possible to open the connection just once, keep it alive, and then check whether there is any data available on this connection?
Thanks, Patrick!
your code needs some restructuring, you should only accept once for each new connection: