Thread: question about asynchronous sockets

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    question about asynchronous sockets

    do you have to use the WSAAsyncSelect() in the winprocedure in order to create an asynchronous socket?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    No. I assume by "async socket" you mean "socket that receives its event notifications asynchronously" - perhaps "overlapped socket" is more MSDN-speak.

    WSAAsyncSelect is one way, it says to Winsock: "I want this socket to be async and I want to register for these events to be sent to this window via this window message when said events happen"

    You can make a socket async other ways however - WSAEventSelect, which is similar to AsyncSelect, but you specify an Event object to wait on for network events to occur.

    Calling CreateIoCompletionPort with the socket handle also makes it a socket overlapped implicitly, and enables you to receive completions via GetQueuedCompletionStatus.

    Finally you can make the socket overlapped at creation time with WSASocket - this isn't usually needed because the functions above make it overlapped anyway, but if you want to use completion routines (nb not same as completion ports), you need to do this I think.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    but do all of these go in the winproc()?

    what do i do in case of listen() and bind()
    are'nt these calls blocking?

    if i do it like this then will it be ok?:

    Code:
    #define MYMESSAGE   WM_USER + 100	
    
    winproc();
    {
    // do stuff needen for winsock to function 
    
    bind();
    listen();
    
    WSAAsyncSelect(MYMESSAGE);  //in the msg parameter
    
    switch(msg)
       {
    
    case MYMESSAGE:
    switch(WSAGETSELECTEVENT(lParam))
    {
     case FD_ACCEPT:
     accept();
    return 0;
    
    case FD_WRITE:
    send();
    return 0;
     
    }
    
      }
    
    
    }
    maybe bind just binds to a socket, but how about listen()
    i cant put these in a case like accept() can i?

    thx!
    Last edited by pode; 12-22-2004 at 09:35 AM.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    No. You don't even need a windows procedure unless you're using AsyncSelect, since that uses windows messages.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Don't call bind(), listen(), or WSAAsyncSelect() from your WindowProc. At least, not every time it's called!

    You'd typically call bind(), listen() and WSAAS() in your initialisation, or whenever your application "goes online".

    They only need to be called once - bind to bind the socket to a port, and listen to start the socket listening for connections. You'd also call WSAAsyncSelect then.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    ok i think i got it now but how do i do something like
    if the user pressed a key only then will the message be sent i tried making a boolean true when user hits key enter(also in winproc) then in
    Code:
    case FD_WRITE:
    if(isok==TRUE)
    send();
    return 0;
    this does not work, how do i do this?

  7. #7
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Do you mean that you want stuff to only be sent after you press the enter key in your own application?

    Incidentally, FD_WRITE(at least from what I read) simply alerts you to the fact that you can write to the socket. You can simply add a keylistener which first checks that it can write (i.e. set a boolean in FD_WRITE so we know its ok) and then after someone hits enter, then do a send.

    The sending part probably should not be in the FD_WRITE section at all because unless you simply want to send data every time the socket is ok to write to, you cannot do too much. Well not exactly, but for simplicity's sake I can't think of anything which would need that offhand..but, anyway, you would want to handle that in a key message somewhere else, so you can accumulate whatever you need then when the user hits enter or the like you can just do a send.

    Hopefully that helps and makes sense to you.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Incidentally, FD_WRITE(at least from what I read) simply alerts you to the fact that you can write to the socket. You can simply add a keylistener which first checks that it can write (i.e. set a boolean in FD_WRITE so we know its ok) and then after someone hits enter, then do a send.
    Note to Pode: You still have to handle (WSA)EWOULDBLOCK even if you've received an FD_WRITE, because you're not guaranteed to have buffer space at some later time after FD_WRITE, and additionally your data might be too large for Winsock to accept at that time.

    Of coruse if you do receive a WOULDBLOCK then Winsock will give you another FD_WRITE when there's space again.

  9. #9
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Hehe I knew I would forget something thanks aztechd, it is important to always check for WSAEWOULDBLOCK as I forgot to mention. Otherwise everything else should be ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about forking with sockets listening
    By Phoenix_Rebirth in forum C Programming
    Replies: 2
    Last Post: 11-10-2008, 04:05 AM
  2. Sockets: IPv4 vs IPv6
    By ninboy in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-15-2008, 01:02 AM
  3. Hi all!, Another question about socket's & NetBios
    By Pandora in forum Windows Programming
    Replies: 9
    Last Post: 03-19-2003, 08:10 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Sockets Question
    By SyntaxBubble in forum Windows Programming
    Replies: 6
    Last Post: 12-30-2001, 07:50 AM