Yes, I'm testing clients with server on the same machine. My goal is: if the system works fine on my pc, I will have no problems on server machine. Is there a minimal example of recv no blocking?
mosine
Posts
-
recv no blocking -
recv no blockingI post my code:
DWORD WINAPI cliTh1( LPVOID lpData ){
struct CLIENT_INFO *pClientInfo;
char szClientMsg[250];
char packet[50];
HANDLE clientTxThread;pClientInfo = (struct CLIENT\_INFO \*)lpData ; char \*ip = inet\_ntoa(pClientInfo->clientAddr.sin\_addr); printf("SOCKET:%d - IP:%s - THREAD\_ID:%ld\\n", pClientInfo->hClientSocket, ip ,GetCurrentThreadId()); Q->pClient = pClientInfo; pClientInfo->primaConn = 0; pClientInfo->indirizzo = 0; pClientInfo->p = getDisconnect; while ( 1 ){ if(j>=MAXELEMENTS){j=0;}; if(WSAGetLastError()){ if(pClientInfo->primaConn == 1){ disconnectBuffer\[pClientInfo->indirizzo\] = 1; pClientInfo->primaConn=0; } if(disconnectBuffer\[pClientInfo->indirizzo\] == 1){ creaPackDisc(packet, pClientInfo->indirizzo); strcpy(bufferRx\[j\].packet, packet); Enqueue(Q, packet); j++; }else{ closesocket(pClientInfo->hClientSocket); ExitThread(pClientInfo->txThId); ExitThread(GetCurrentThreadId()); } Sleep(1000); }else{ if((pClientInfo->primaConn == 0) && (pClientInfo->indirizzo != 0)){ pClientInfo->connessione = setConnect(GetCurrentThreadId(), pClientInfo->indirizzo, ip); if(pClientInfo->connessione == 1){ pClientInfo->primaConn = 1; } //THREAD TX for each client clientTxThread = CreateThread(NULL,0,(LPTHREAD\_START\_ROUTINE)txThread, pClientInfo,0,&pClientInfo->txThId); if ( clientTxThread == NULL ){ printf("Unable to create client thread"); } else { CloseHandle( clientTxThread ) ; } } if(recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) > 0){ strcpy(bufferRx\[j\].packet, szClientMsg); memset(&szClientMsg\[0\], 0, sizeof(szClientMsg)); pClientInfo->indirizzo = calcolaHighLow(bufferRx\[j\].packet\[1\], bufferRx\[j\].packet\[2\], bufferRx\[j\].packet\[3\], bufferRx\[j\].packet\[4\]); Enqueue(Q, bufferRx\[j\].packet);
-
recv no blockingThanks for reply. The problem is, if my server must send data to client, it waits 5 seconds because recv function blocks thread. I try to launch another thread (for each client) for send operation, but cpu works 100%.
-
recv no blockingHi, I'm developing a server multithread (multiclient) and I have a problem with recv function (c language - SO: windows). My clients send info to server every 5 seconds and recv function blocks thread until info arrives. I try with ioctlsocket, but the number of byte read always is 0; I try with flag MSG_PEEK, but the data is copied into the buffer, but is not removed from the input queue and so I have many messages. Is there a way to perform recv function no blocking? THANKS
-
Multithread c WindowsThanks for your advices!!!! I will correct code source.
-
Multithread c WindowsI find my error!!!! thanks a lot. You advised me to control array size....
-
Multithread c WindowsThanks for reply. I declared:
HANDLE hClientThread[65000]
struct CLIENT_INFO clientInfo[10000]I launch 20 client process.
-
Multithread c WindowsHi, 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;};