你如何在 C 中编写代码?
所需的流程是:
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
我试过这段代码:
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
}
我知道线程存在,但我想避免使用线程。如果这是唯一的方法,我将使用线程。
编辑:使用非阻塞套接字不是解决方案,因为如果没有数据可用并且队列中没有消息,那么我的程序将退出。