Multithread c Windows
-
Hi, I'm developing a client-server C language in Windows. The client connects to server and every 5 seconds send a message. The communication is OK and server receives messages. I try to develop multi-client (.bat file starts client.exe) - server but I have problem: A segmentation fault appears after n messages. This is my code: main
//INIT WINSOCK
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
//CREATE SOCKET
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET){
printf("Could not create socket : %d" , WSAGetLastError());
}
//sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//BIND
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR){
printf("Bind failed with error code : %d" , WSAGetLastError());
}
//LISTEN
listen(s , 10000);
//WAIT CONNECTION
puts("Waiting for incoming connections...");
while( TRUE ) {
new_socket = accept(s , (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET){
printf("accept failed with error code : %d" , WSAGetLastError());
}else{
clientInfo[cont].clientAddr = client ;
clientInfo[cont].hClientSocket = new_socket;
hClientThread[cont] = CreateThread(NULL,0,( LPTHREAD_START_ROUTINE ) cliTh1,( LPVOID ) &clientInfo[cont],0,&dwThreadId ) ;if ( hClientThread\[cont\] == NULL ){ printf("Unable to create client thread"); } else { printf("thread OK\\n"); CloseHandle( hClientThread\[cont\] ) ; cont++; } } }
Thread function (cliTh1)
DWORD WINAPI cliTh1( LPVOID lpData ){
struct CLIENT_INFO *pClientInfo;
char szClientMsg[250];
static int j = 0;
int i = 0;
char *message;pClientInfo = (struct CLIENT\_INFO \*)lpData ; printf("SOCKET:%d - THREAD\_ID:%ld\\n", pClientInfo->hClientSocket, GetCurrentThreadId()); while ( 1 ){ if(WSAGetLastError()){ printf("CURRENT:%ld\\n", GetCurrentThreadId()); closesocket(pClientInfo->hClientSocket); ExitThread(GetCurrentThreadId()); } if(recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) > 0){ if(j>5000){j=0;};
-
Hi, I'm developing a client-server C language in Windows. The client connects to server and every 5 seconds send a message. The communication is OK and server receives messages. I try to develop multi-client (.bat file starts client.exe) - server but I have problem: A segmentation fault appears after n messages. This is my code: main
//INIT WINSOCK
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
//CREATE SOCKET
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET){
printf("Could not create socket : %d" , WSAGetLastError());
}
//sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//BIND
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR){
printf("Bind failed with error code : %d" , WSAGetLastError());
}
//LISTEN
listen(s , 10000);
//WAIT CONNECTION
puts("Waiting for incoming connections...");
while( TRUE ) {
new_socket = accept(s , (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET){
printf("accept failed with error code : %d" , WSAGetLastError());
}else{
clientInfo[cont].clientAddr = client ;
clientInfo[cont].hClientSocket = new_socket;
hClientThread[cont] = CreateThread(NULL,0,( LPTHREAD_START_ROUTINE ) cliTh1,( LPVOID ) &clientInfo[cont],0,&dwThreadId ) ;if ( hClientThread\[cont\] == NULL ){ printf("Unable to create client thread"); } else { printf("thread OK\\n"); CloseHandle( hClientThread\[cont\] ) ; cont++; } } }
Thread function (cliTh1)
DWORD WINAPI cliTh1( LPVOID lpData ){
struct CLIENT_INFO *pClientInfo;
char szClientMsg[250];
static int j = 0;
int i = 0;
char *message;pClientInfo = (struct CLIENT\_INFO \*)lpData ; printf("SOCKET:%d - THREAD\_ID:%ld\\n", pClientInfo->hClientSocket, GetCurrentThreadId()); while ( 1 ){ if(WSAGetLastError()){ printf("CURRENT:%ld\\n", GetCurrentThreadId()); closesocket(pClientInfo->hClientSocket); ExitThread(GetCurrentThreadId()); } if(recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) > 0){ if(j>5000){j=0;};
You are accepting an unlimited number of connections but did not check if
cont
reaches the limit of yourclientInfo
andhClientThread
array sizes. This results in segmentation faults whencont
is equal to the number of array items. -
You are accepting an unlimited number of connections but did not check if
cont
reaches the limit of yourclientInfo
andhClientThread
array sizes. This results in segmentation faults whencont
is equal to the number of array items. -
Thanks for reply. I declared:
HANDLE hClientThread[65000]
struct CLIENT_INFO clientInfo[10000]I launch 20 client process.
-
Thanks for reply. I declared:
HANDLE hClientThread[65000]
struct CLIENT_INFO clientInfo[10000]I launch 20 client process.
So that is not the error source. But why you are using different sizes? Another possible source is in these lines in the thread function:
if(j>5000){j=0;};
strcpy(bufferRx[j].packet, szClientMsg);This requires that the packet contains a null terminated string. You are also using a constant value for the
bufferRx
size without showing the definition or allocation. It would be better to use a variable when it is allocated orsizeof()
when using a fixed size. -
So that is not the error source. But why you are using different sizes? Another possible source is in these lines in the thread function:
if(j>5000){j=0;};
strcpy(bufferRx[j].packet, szClientMsg);This requires that the packet contains a null terminated string. You are also using a constant value for the
bufferRx
size without showing the definition or allocation. It would be better to use a variable when it is allocated orsizeof()
when using a fixed size.