CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Network Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Network Programming Chat about all aspects of network programming, including raw sockets, Winsock API, BSD sockets, MFC-based CSocket, CAsyncSocket, and other network-related topics.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 7th, 2009, 01:16 PM
    MasterDucky MasterDucky is offline
    Member
     
    Join Date: Dec 2007
    Posts: 110
    MasterDucky is an unknown quantity at this point (<10)
    Non-blocking socket question

    Im sorry it might not be the brightest question but when we talking about asynchronous or non-blocking (i think its the same) sockets,
    we only talking about the server side, right?

    I mean we can connect to it with the same client that we connected to blocking sockets?
    Reply With Quote
      #2    
    Old November 8th, 2009, 09:13 AM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,189
    hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)
    Re: Non-blocking socket question

    It doesn't matter. You can use a non-blocking socket on either side.

    Asynchronous (as defined by MS) is technically different in that uses message triggers and callbacks to handle data asynchronously rather than using the TCP/IP stack mechanisms.
    Reply With Quote
      #3    
    Old November 8th, 2009, 01:17 PM
    MasterDucky MasterDucky is offline
    Member
     
    Join Date: Dec 2007
    Posts: 110
    MasterDucky is an unknown quantity at this point (<10)
    Re: Non-blocking socket question

    Thanks a lot!
    Reply With Quote
      #4    
    Old November 11th, 2009, 08:51 PM
    MikeAThon MikeAThon is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2002
    Location: California
    Posts: 4,123
    MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)
    Re: Non-blocking socket question

    Just to emphasize what hoxsiew has said ...

    Asynchronous and non-blocking refer to two entirely different concepts. Non-blocking refers to the notion that a call to a socket function is guaranteed to return immendiately, even if there is no current activity on the socket. In contrast, for a blocking socket, a call to a socket function will block indefinitely, until there is some appropriate form of activity on the socket.

    Asynchronous refers to the manner in which your sockets notify your application that there has been some activity. By way of explanation, suppose you have a blocking socket and you call recv(). The call to recv() will block until there is data in the buffer, so when the call to recv() returns, your application is notified (implicitly) that there has been activity. In contrast, for a non-blocking socket, the call will return immediately, even if there has not been any activity at all. In this scenario, the only way for your application to know that there has been activity is to poll for it (i.e., by calling recv() in a loop until it succeeds) or by using the select() function. Both of these activities are "synchronous" since it is your own application that determines for itself whether there has been activity on the socket, and since because it is the application that makes these determinations, the timing of these determinations is known precisely.

    (Incidentally, polling for activity is a very bad idea and should never be done in an actual program. It's given here only to make the contrast with "asynchronous" more clear.)

    In contrast, with asynchronous notifications, it's the OS that makes the determination that there has been activity on the socket, and it's the OS that sends some sort of trigger to your application that notifies your application of this activity. These notifications can arrive at any time, regardless of the current state of execution of your application. For this reason, the notifications are called "asynchronous".

    The precise nature of the trigger depends on context. Usually, when speaking of asynchronous sockets, programmers are referring to GUI-based applications that run a message loop, and that use the WSAAsyncSelect() function so that the OS sends ordinary Windows messages to your app to notify it of socket activity. The content of the message identifies the socket for which there has been activity, as well as the nature of the activity.

    Mike
    Reply With Quote
      #5    
    Old November 12th, 2009, 04:04 PM
    Payne747 Payne747 is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 20
    Payne747 is an unknown quantity at this point (<10)
    Smile Re: Non-blocking socket question

    Thanks MikeAThon for the clarification, I've also been struggling to get to grips with non-blocking\Asynchronous sockets, though I do have a question about the following statement...

    Quote:
    Originally Posted by MikeAThon View Post
    In this scenario, the only way for your application to know that there has been activity is to poll for it (i.e., by calling recv() in a loop until it succeeds) or by using the select() function.
    I understand why polling is a bad idea, so when using non-blocking sockets, looping is the only way forward right? I assume you loop on recv() until it returns EWOULDBLOCK (suggesting there's nothing in the TCP buffer. Does this sound right?

    Therefore, select() shouldn't be used (polling), what's the equilevent call on Linux systems for Asynchronous checks (WSAAsyncSelect())?

    Thank you,

    Payne747
    Reply With Quote
      #6    
    Old November 12th, 2009, 04:20 PM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,189
    hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)
    Re: Non-blocking socket question

    You've got it backwards. Looping is polling. select() blocks, but only for a specified time. If nothing happens during the timeout period, control returns to the program and it can do other things.

    Since linux has no native messaging API like windows, you pretty much have to use select, or implement your own asynchronous system with threads.
    Reply With Quote
      #7    
    Old November 13th, 2009, 10:31 AM
    Payne747 Payne747 is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 20
    Payne747 is an unknown quantity at this point (<10)
    Re: Non-blocking socket question

    got it, so select() isn't really a bad thing, it's just not as good as an asynchronous solution, whatever that may be.
    Reply With Quote
      #8    
    Old November 14th, 2009, 07:12 AM
    MasterDucky MasterDucky is offline
    Member
     
    Join Date: Dec 2007
    Posts: 110
    MasterDucky is an unknown quantity at this point (<10)
    Re: Non-blocking socket question

    Thank you very much Mike, thats exactly what i was wanting to know.

    So for a Windows GUI application even select() will block the program, we should use asynchronous connection.
    Reply With Quote
      #9    
    Old November 15th, 2009, 09:34 PM
    Red Squirrel Red Squirrel is offline
    Member
     
    Join Date: Jul 2007
    Posts: 476
    Red Squirrel is an unknown quantity at this point (<10)
    Re: Non-blocking socket question

    It took me a long time to figure out how to code non blocking sockets but I managed to figure it out and write a class for it. As long as you used a reasonable delay between the polls, it will be efficient. In fact, I was able to handle about 4000 connections streaming random data, in a single thread! Non blocking sockets are a real dream once you get a basic engine going. You can go a step further and add a couple threads to help take the load, to take advantage of multi cpu and make high performance server apps. That gets a bit more complicated though and you CAN run into performance issues if not done right.
    __________________
    http://www.uovalor.com :: Free UO Server
    Reply With Quote
      #10    
    Old November 18th, 2009, 06:17 PM
    Payne747 Payne747 is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 20
    Payne747 is an unknown quantity at this point (<10)
    Re: Non-blocking socket question

    Quote:
    Originally Posted by Red Squirrel View Post
    It took me a long time to figure out how to code non blocking sockets but I managed to figure it out and write a class for it.
    I don't suppose you'd be willing to share the code for this class, or at least point me in the right direction to using the would-be blocking functions within?
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Network Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:48 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009