Click here to Skip to main content
13,141,576 members (35,590 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm having a problem with accept function in Windows.
It takes 2-4 seconds to accept a socket from another 3rd party application.

Here is my code:

DWORD dwStart = GetTickCount();
AcceptSocket = accept(ListenSocket, NULL, NULL);
DWORD dwEnd = GetTickCount();
DWORD time = dwEnd - dwStart;
printf("Accept took: %u",time);

Please help me with this issue :)

Posted 1-Dec-10 3:00am
Updated 1-Dec-10 3:01am
Henry Minute225.8K
Lloyd Atkinson 1-Dec-10 11:01am
Perhaps it's simply because it blocks, and then waits for a connection which could take anything from a few milliseconds to several seconds depending on how fast/reliable the network is.
Alexeirob 2-Dec-10 2:37am
But how can it be? The connect is coming 0.5 seconds before the accept. And the connection is through the loopback. Can it be related to blocking/non-blocking socket?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Have a look at the remarks on accept:

The accept function can block the caller until a connection is present if no pending connections are present on the queue, and the socket is marked as blocking. If the socket is marked as nonblocking and no pending connections are present on the queue, accept returns an error as described in the following. After the successful completion of accept returns a new socket handle, the accepted socket cannot be used to accept more connections. The original socket remains open and listens for new connection requests.[^]

You might want to improve response time by using a thread to prevent blocking. You could notify the main thread using an event but be sure to invoke this properly to avoid problems in that area.[^]

Good luck!
Dalek Dave 1-Dec-10 10:00am
Good call.
Alexeirob 2-Dec-10 3:22am
Thanks E.F. Nijboer.
I've looked at the accept remarks, and the connect() function (in the client side) called before the accept, so that there are pending connections.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 1 Dec 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100