How do you code this in C?
The desired flow is:
Create socket
Create window
loop: Wait until data can be read from socket or message are added to the queue
if data then
do stuff with data
goto loop
else if message then
do stuff with message
goto loop
I have tried this code:
MSG msg;
DWORD last=msg.time;
short bRet;
char command[20];
int n=0;
while((
(n = recv(sock, command, sizeof command - 1, 0)) >= 0
)||(
(bRet = GetMessage( &msg, 0, 0, 0 )) != 0
//I can't use peek message because it will have the same issue than non-blocking socket
)){
//here we check time of message if it is equal to last we know that is socket
}
I know threads exist, but I want to avoid use of threads. I will use a thread if it is the only way to go.
Edit: Using a non-blocking socket is not a solution because if no data is available and no message is in the queue then my program will exit.