我一直在處理非阻塞udp套接字。只要有任何數據要通過套接字讀取,我開發的代碼就會生成一個Window Message。下面的代碼片段:使udp插座非阻塞
void createSocket(HWND hwnd)
{
///Socket Binding///
WSADATA wsa;
///Initialise winsock///
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
exit(EXIT_FAILURE);
}
///Create a socket///
if((socketIdentifier = socket(AF_INET , SOCK_DGRAM , 0)) == INVALID_SOCKET)
{
//Socket Creation Failed
}
///Socket Created///
///Prepare the sockaddr_in structure///
serverSocket.sin_family = AF_INET;
serverSocket.sin_addr.s_addr = INADDR_ANY;
serverSocket.sin_port = htons(PORT);
///Bind///
if(bind(socketIdentifier ,(struct sockaddr *)&serverSocket , sizeof(serverSocket)) == SOCKET_ERROR)
{
//Bind Failed
}
WSAAsyncSelect (socketIdentifier, hwnd, MY_MESSAGE_NOTIFICATION, FD_READ | FD_CONNECT | FD_CLOSE | FD_ACCEPT); //Set
///Bind Done///
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd
{
//Window Created
createSocket() //Socket Created
while(GetMessage(&Msg, NULL, 0, 0) > 0) //Check on Window Messages
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case MY_MESSAGE_NOTIFICATION: //Is a message being sent?
{
switch (lParam) //If so, which one is it?
{
case FD_ACCEPT:
//Connection request was made
break;
case FD_CONNECT:
//Connection was made successfully
break;
case FD_READ:
receiveAtSocket();
break;
case FD_CLOSE:
//Lost the connection
break;
}
}
break;
}
}
此代碼工作正常,而插座沒有呼叫等待snedto()或recvfrom的()。相反,只要數據已準備好在套接字上讀取或寫入,就會生成一個窗口消息。
現在,我想找出一些其他方式通知數據數據已準備就緒,而不是窗口消息。也就是說,只要有數據要在套接字上讀取或寫入,我不希望產生任何窗口消息。
是否有任何其他方式實現上述功能,而不使用窗口消息>請幫助我。
等待救援:(
爲什麼你不想窗口消息 – 2013-05-04 08:28:43