views:

79

answers:

2

I have a server application that opens a socket and listen for a connection. In the application, I have a separate thread that creates a socket, binds it and calls the listen and accept functions on it.

When the application closes I call closesocket on the socket that was created, then wait for the socket thread to close. However, if the thread is waiting on the accept function the thread never completes.

I thought that the accept function would return after the cloasesocket was called. Is this a correct thought? If so, why does the accept function not return? Is there another way to cause the accept function to return?

+2  A: 

Don't call accept unless select says it's OK. In that case accept will never block.

Dialecticus
Used the select function with a timer but for some reason when the application closes the select function never times out. Any ideas why?
Jason E.
The correct way to go about this is not to close the application immediately, but to somehow instruct all threads to finish, and wait them finish, and then close.
Dialecticus
A: 

Check the man page... http://linux.die.net/man/2/accept

accept() blocks on socket A, and when a new connection comes in, returns a new socket B to connect the client. This is frequently in a tight loop with a fork() and exec() to send the new connection off to a child process to handle the connection, while the parent returns into accept() to wait for another connection.

Are you trying to say that another thread in your program closes socket A out from under the accept() call?

Rob K
The Application thread starts the socket server thread and the application thread stops the socket server thread. If the socket server thread is in accept function the thread will not end. So Socket A is closed by the application thread. I thought by doing this the accept function would return an error, thus allowing the socket server thread to end. Incorrect assumption? Thanks for you comment.
Jason E.