Thread: What exactly does listen() do from winsock 1.1

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    What exactly does listen() do from winsock 1.1

    Hi, im designing a login server for a game, and every time the server receives a connection i intend to a launch a new thread. I was playing with the code from another server i made which was only capable of connecting to one client, and basically it does this.

    Code:
    //define the listening socket above
    listen(listenSock, 10)
    //define a server socket
    serverSock = accept(listenSock, NULL, NULL);
    //terminate server thread, as this is just a test
    what i was expecting was the server thread to block at listen(), and then continue after the client tried to connect. The client, ofcourse, timed out. however, i then tried running two clients, and both of them found the server (im not sure if they both got "accepted") and then timed out.

    So what does listen() actually. Im really confused, as i was intending to make a loop using listen to block until a client tried to connect and then launch a thread. But it would appear as though listen did more than just block. Could someone please explain whats going on there. Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    listen() puts the socket in listening mode. It doesn't wait for an actual connection, though. That's accept()'s job.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    listen() goes in the main thread, then you pass the accept()'ed socket into a new thread to deal with that connection. The main thread loops round for another listen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    ok, so accept() will block and should be in the loop, and theres some sort of magical queue created in the background. I hate magic, so confusing.

    Thx, im pretty sure i can work from there.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There's no queue. Accept will block if the socket is blocking and won't if it's not. Where is this queue you are speaking of?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This queue is internal to the network driver and holds client connections until accept fetches them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ok there's no queue from a software standpoint.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (C++) Winsock connect and listen program. Need Help!
    By azjherben in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-05-2009, 07:44 PM
  2. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  3. Winsock: Cutting A listen() Call Short
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 10-05-2006, 12:02 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. Non blocking listen() with winsock
    By manwhoonlyeats in forum C Programming
    Replies: 1
    Last Post: 12-08-2002, 07:00 PM