使udp套接字无阻塞 [英] Making udp socket non-blocking

查看:94
本文介绍了使udp套接字无阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究非阻塞的udp套接字。当有任何数据要通过套接字读取时,我开发的代码会生成一个窗口消息。下面是代码片段:

I had been working on non-blocking udp socket. The code that I had developed generates a Window Message whenever there is any data to be read over the socket. Below is the code snippet:

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;
   }
}



此代码工作正常,socket不必等待调用snedto()或recvfrom()。相反,只要数据准备好在套接字上读取或写入,就会生成一个窗口消息。



现在,我想找出一些通知数据的方法数据准备就绪而不是窗口消息。即只要有数据要在套接字上读取或写入,我就不希望生成任何窗口消息。



有没有其他方法可以实现上述功能,不使用Window Messages>请帮助我。



等待帮助:(


This code is working fine, and socket does not have to wait in call to snedto() or recvfrom(). Instead, A window Message is generated whenever data is ready to be read or written on the socket.

Now, I want to find out some other way of informing that data data is ready rather than a window message. i.e. I don''t want any window message to be generated whenever there is data to be read or written on the socket.

Is there any other way of implementing the above mentioned functionality without using Window Messages> Please help me.

Waiting for help :(

推荐答案

在自己的线程中做网络的东西使用PostThreadMessage与主线程进行通信。
Do the networking stuff in a own thread. Use PostThreadMessage to communicate with your main thread.


这篇关于使udp套接字无阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆