Click here to Skip to main content
15,041,393 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to make my Socket "Event Based". Following is what i tried:
VOID createServerSocket()
   WSADATA wsa; 

   //Initialise winsock//
   if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)

        //"WinSock Initialization FAILED",


   //Create a socket//

  SOCKET newSocketIdentifier;
  SOCKADDR_IN newSocket;

  if((newSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)

        //Socket Creation Failed

   //Socket Created//

   //Prepare the sockaddr_in structure//
  newSocket.sin_family = AF_INET;
  newSocket.sin_addr.s_addr = INADDR_ANY;
  newSocket.sin_port = htons(8888);

   if( bind(newSocketIdentifier ,(struct sockaddr *)&newSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
       //Bind Failed

   //Bind Done//

   WSAEVENT NewEvent = WSACreateEvent();
   WSAEventSelect(newSocketIdentifier, NewEvent, FD_READ | FD_WRITE); //made the socket "newSocketIdentifier" event based for events "FD_READ" and FD_WRITE


I don't know how to proceed further. How shall I check whether any of the desired events had occur? How can I continuously keep checking for these events? Will that be under some while(1) loop?

All the examples I found over the internet are for multiple sockets. However, in my case, I only have a single socket and I want to make it "Event Driven" for Read and Write. Please help me. I am stuck!
Why? Using blocking APIs with threads is much better, in most cases.
ayesha hassan 13-May-13 1:56am
I had been working on a Server Client aplication where Server is going to service(sendto + receivefrom) 'x' number of Clients at a time. For this purpose, I have created 'x' number of threads on Server side so that each thread is dadicated to one single client. Inside each thread there is a specific socket for just for its client. I was thinking to make these sockets non-blocking but now I think using blocking socket inside each thread is a better Idea. Blocking Socket continuously waits to receive data and when ever there is a need to send anything, sendto() is called. Is using blocking Socket in such a situation a good approach or should I use non-blocking sockets? :(
Waiting for help!!!
Help with what? I only say that blocking sockets are better. Think of it in this way: threading solves the problem of blocking, at the expense of the need in thread synchronization. But you learn thread synchronization just once and use this knowledge for lifetime, while non-blocking API is application-specific. I remember the time when most people (but not myself) could not use threading, some used blocking API and tolerated unresponsive applications, others used asynchronous API, both ways looked unsatisfactory to me...
ayesha hassan 7-May-13 2:33am
I shifted towards the idea of using non blocking sockets when I realized that my Server is blocking in continuous call to recvfrom() and is unable to send anything even if it wants, unless call to recvfrom() ends.
if((recv_len = recvfrom(AH_glb_ackSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &acksocket, &socketLength)) == SOCKET_ERROR)
//recvfrom() Failure.
if (sendto(socketIdentifier,sendBuffer,strlen(sendBuffer) , 0 , (struct sockaddr *) &AH_glb_connectedSocket, sizeof(AH_glb_connectedSocket)) == SOCKET_ERROR)
//sento() Failure
First of all, I did not receive a notification and did not know about your comment. If you address to me, you should use Reply, then I wold receive notification on your post.

Even though you can use async API, you idea is wrong. You need to create reasonable application level protocol. Why do you need to send something if you did not receive a previous message? What is the need to send something is driven by? Even if your sends and receives (to/from the same socket) should be asynchronous due to the semantic of your protocol, you can do it in two different threads. Actually, this is the same the async API would do, only threads are simpler, more universal and straightforward. I think async API was used when threads were not a commonplace as they are now.
ayesha hassan 8-May-13 0:56am
+1. Thank you for the explanation
You are welcome.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900