Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows Win32 programming socket , +
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);
 
   //Bind//
   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!
Posted 6-May-13 21:24pm
Comments
Sergey Alexandrovich Kryukov at 7-May-13 2:26am
   
Why? Using blocking APIs with threads is much better, in most cases.
—SA
ayesha hassan at 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!!!
Sergey Alexandrovich Kryukov at 13-May-13 2:16am
   
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...
—SA
ayesha hassan at 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.
e.g:
while(1)
{
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
}
}
Sergey Alexandrovich Kryukov at 7-May-13 10:15am
   
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.
—SA
ayesha hassan at 8-May-13 0:56am
   
+1. Thank you for the explanation
Sergey Alexandrovich Kryukov at 8-May-13 1:02am
   
You are welcome.
—SA

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2014
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