C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 12-10-2007, 03:05 AM   #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.
*DEAD* is offline   Reply With Quote
Old 12-10-2007, 03:26 AM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline   Reply With Quote
Old 12-10-2007, 03:33 AM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 12-10-2007, 04:03 AM   #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.
*DEAD* is offline   Reply With Quote
Old 12-13-2007, 10:17 PM   #5
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,472
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?
Bubba is offline   Reply With Quote
Old 12-14-2007, 06:35 AM   #6
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline   Reply With Quote
Old 12-14-2007, 06:48 PM   #7
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,472
Ok there's no queue from a software standpoint.
Bubba is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:23 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22