I am writing a simple C game that accepts key inputs over the network. As it is my first time handling sockets in C, I am facing some problems with some functions. This function called 'recv', seems to wait for any network messages in the TCP connection until data is received. The problem is that since this 'freezes' the program while there are no messages, my normal game code that is supposed to run in an infinite loop won't work properly. Is there a way I could wait for network messages AND run the game at the same time?
while (1) //Infinite 'game loop' start
{
read_size = recv(newsockfd , client_message , 2000 , 0);
if(read_size > 0)
{
//Do something
}
//Game code here (Doesn't work when there are no incoming messages!)
}