July 18th, 2012, 09:18 PM
My send() socket call iis taking its own sweet time
I have a TCP server that handles more than 100 clients.
I keep getting bottlenecks and i narrowed it down to the send() winsock function.
A send() winsock call sometimes takes 100~300ms to finish.
I i have no clue why but this block from other clients to be served.
Socket initialization code
int ret = WSAStartup(MAKEWORD(2,2), &wsaData);
if(ret != 0)
printf("Winsock failed to start.\n");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(52000);
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET)
if(bind(sock, (sockaddr*)&server, sizeof(server)) != 0)
if(listen(sock, 5) != 0)
Send function caller
int fromlen = sizeof(from);
SOCKET sTmpSocket = accept(ServerSock, (struct sockaddr*)&from, &fromlen);
I would like to know how i can fix the delay.
void CClient::SendPacket(BYTE* pPacket, DWORD Len)
DWORD ToSendLen = Len;
int iResult = send(this->ClientSocket, (char*)(pPacket + Len - ToSendLen), ToSendLen, 0);
if (iResult == SOCKET_ERROR)
ToSendLen -= iResult;
Thank you very much.
August 3rd, 2012, 06:33 PM
Re: My send() socket call iis taking its own sweet time
Your code suggests that your architecture uses blocking sockets.
If so, well, blocking sockets by their very nature will block. The block can be frustrating, but it allows you to write code with a very simple and serialized flow logic.
If you cannot tolerate the block, there are two options: re-write using non-blocking sockets, or maintain the use of blocking sockets but re-write into a multithreaded code, with one thread per client.
Both will introduce complications, and you are in the best position to know which one might be more appropriate. For example, if there is significnat coordination amongst the clients, then multithreading might be too difficult.
Without knowing more, with only around 100 clients, the multithreaded approach might be easier than non-blocking sockets. As you start to exceed a few hundred threads, the context switching among threads makes the multithreaded approach impractical, so this architecture would not be easily extensible if you suddenly find that you need to service 1000 clients simultaneously.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!