I have written a Server using WINAPI with CreateThread() Method. The first connection request always dies. All the following requests/threads are working as expected. I have no idea why so i hope someone could tell me. Here is a working example illustrating the issue.
DWORD WINAPI process_thread(LPVOID lpParam) {
SOCKET current_client = (SOCKET)lpParam;
char buf[1024];
int res;
while(1) {
res = recv(current_client, buf, strlen(buf), 0);
if(res>0) {
buf[res] = '\0';
send(current_client, buf, strlen(buf), 0);
}
}
}
int main() {
SOCKET sock;
DWORD thread;
WSADATA wsaData;
SOCKADDR_IN server;
WSAStartup(0x102,&wsaData);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(123);
sock=socket(AF_INET,SOCK_STREAM,0);
bind(sock,(SOCKADDR*)&server,sizeof(server));
listen(sock,5);
SOCKET client;
SOCKADDR_IN from;
int fromlen = sizeof(from);
while(1) {
client = accept(sock,(struct SOCKADDR*)&from,&fromlen);
CreateThread(NULL, 0,process_thread,(LPVOID)client, 0, &thread);
}
closesocket(sock);
WSACleanup();
return 0;
}
You are misusing
strlen()
inside your thread code.When calling
recv()
, you need to specify the full size of your buffer.strlen()
is not the right way to get that value. Usesizeof()
instead.Then, when
recv()
exits, its return value tells you exactly know how many bytes in the buffer are valid. Again,strlen()
is not the right way to get that value.Also, you don't need to null-terminate the buffer just to pass it to
send()
. Since you are told how many bytes are in the buffer, just send that many bytes.Also, your threads are not terminating, or closing their sockets, when clients disconnect from the server.
Also, your
main()
is leaking thread handles, and not doing any kind of error handling at all.Try something more like this instead: