Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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:

C++
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 :)

Thanks,
AR
Posted
Updated 1-Dec-10 3:01am
v2
Comments
LloydA111 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

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.




http://msdn.microsoft.com/en-us/library/ms737526%28VS.85%29.aspx[^]

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.

http://members.cox.net/doug_web/threads.htm[^]

Good luck!
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900