Thread: single Socket for listening and sending/receiving, Simultaneously?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Unhappy single Socket for listening and sending/receiving, Simultaneously?

    Hi,

    Is it possible to use the same network Socket simultaneously, for listening (for incoming connections) and communicating with a non-connected client (using the sendto() and recvfrom() functions)? Or must I have two separate sockets, and if so must the separate sockets also be bound to separate network ports? Just for the info, I am using WinSock 2 API functions.

    High thanks in advance
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If using accept() then you'll need two sockets. And yes they can both use the same port.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Yes I know... but is it possible to use the same listning socket for communication with a non-connected client simultaneously? In a multithread app for ex:

    * socket S is created.
    * bind S to network address with specific port.
    * Create thread T.
    * set S to listen for incoming connections.
    * meanwhile on thread T, socket S is sending and receiving messages.

    Is the above situation possible?
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't think so.

    However I think one of socket gurus should come answer it for sure.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How would you tell the difference between incoming data and a new connection request?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb I think you need threads for this ...

    I think threads would be the right solution. I made a two-person chat application not too long ago, that I haven't finished yet, and I had to use threads, to control both sending and receiving data. Try going to http://msdn.microsoft.com/library and searching for _beginthread() or _beginthreadex(). I just used a function called Server(), declared as 'void Server()', that accepted connections and listened for incoming data. For multi-connection support is jus a little more complicated, but not by means of threads. You just have to create an array of listening sockets. For example:

    Code:
    // These are global variables
    SOCKET listening[10];       // Where ten is the maximum number of sockets that can be connected at once
    I don't know if this is EXACTLY what you need, but I think it should help you.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Originally posted by Hammer
    How would you tell the difference between incoming data and a new connection request?
    If it where possible, then the listening function would return when a connection is requested by another comp. And the incoming data would be returned from the receiving function. Although I don't know how it is handled on a low-level, it is probably up to the protocol used.


    Thanks for the info SyntaxBubble, it's not really what I was looking for but still useful. May I ask if you used threads that always listened for connections or message-based notification?
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I think this may be possible, but is strongly discouraged as you can run into all sorts of problems. It would be much better to use a completely seperate socket for listening whilst doing other i/o, and would make your code more readable & understandable.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    For multi-connection support is jus a little more complicated, but not by means of threads. You just have to create an array of listening sockets.
    Depending on what you mean, I may or may not disagree.

    Code:
    //Global variables
    SOCKET listening;
    (do something to bind it to a port and get it to listen)
    std::vector<SOCKET> clients;
    
    //In your thread procedure
    while(!quit)
    {
        clients.push_back(accept(listening, ...));
    }
    If you mean that in order to get more than 1 client to connect you need to create more listening sockets, I disagree. One listening socket can accept connections from any number of clients < the system's max number of sockets. If you mean that to get clients connecting on more than 1 port you need more listening sockets, I agree.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    If you mean that to get clients connecting on more than 1 port you need more listening sockets, I agree.
    hello,

    I'm a newbie in c++. Anyway, do you have any idea on connecting clients thru different ports?

    I appreciate any help from you guys out there. thanks

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by rinin_farina
    hello,

    I'm a newbie in c++. Anyway, do you have any idea on connecting clients thru different ports?

    I appreciate any help from you guys out there. thanks
    Read through some of the tutorials mentioned in the "good links" thread.

    To get a client connecting on different ports, simply create more sockets and call connect on them. Obviously a server will need to be listening on each port you call to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed