4

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Jason E.
  • 43
  • 1
  • 3
  • 1
    You need to post code for `closesocket` logic and `accept` thread to get a proper answer. – Steve Townsend Oct 29 '10 at 17:44
  • 1
    Which platform. It does on Windows. The answer to this similar question might help: http://stackoverflow.com/questions/2486335/wake-up-thread-blocked-on-accept-call – Len Holgate Oct 29 '10 at 18:41
  • Steve, thank you for your input but the code I'm talking about is embedded in a larger application so I would have to write an example. I'll see if I can over the weekend. – Jason E. Oct 29 '10 at 22:07
  • 1
    Len, Thank you for your response. I've looked at the source you gave me... I'm still trying to wrap my head around it. – Jason E. Oct 29 '10 at 22:09

3 Answers3

4

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

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • 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. Oct 29 '10 at 22:03
  • 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 Oct 30 '10 at 10:41
  • Won't it be the same with select blocking indefinitely instead of accept ? – thodg Jun 23 '17 at 18:22
  • `select` has timeout parameter, so you call it in a loop with timeout that you're comfortable with. – Dialecticus Jun 23 '17 at 18:29
0

I have seen a similar issue where calls to WSAStartup and WSACleanup weren't balanced.

The program was calling WSACleanup before invoking the closesocket call. Both the thread trying to close the socket as the one accepting would block. When I removed the 'extra' WSACleanup things improved.

Elsewhere it says that calling WSACleanup would release the call to accept with an error but I'm not really sure that's true... And who knows what else goes wrong if you fail to match those startup and cleanup functions.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
E. van Putten
  • 615
  • 7
  • 17
0

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
  • 8,757
  • 2
  • 32
  • 36
  • 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. Oct 29 '10 at 18:13