Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows programming socket 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

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

}
} 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 Frown | :(
Posted 3-May-13 23:17pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Do the networking stuff in a own thread. Use PostThreadMessage to communicate with your main thread.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 290
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100