Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
M

mosine

@mosine
About
Posts
8
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • recv no blocking
    M mosine

    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?

    C / C++ / MFC sysadmin data-structures help question

  • recv no blocking
    M mosine

    I 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);
    
    C / C++ / MFC sysadmin data-structures help question

  • recv no blocking
    M mosine

    Thanks 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%.

    C / C++ / MFC sysadmin data-structures help question

  • recv no blocking
    M mosine

    Hi, 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

    C / C++ / MFC sysadmin data-structures help question

  • Multithread c Windows
    M mosine

    Thanks for your advices!!!! I will correct code source.

    C / C++ / MFC help sysadmin

  • Multithread c Windows
    M mosine

    I find my error!!!! thanks a lot. You advised me to control array size....

    C / C++ / MFC help sysadmin

  • Multithread c Windows
    M mosine

    Thanks for reply. I declared:

    HANDLE hClientThread[65000]
    struct CLIENT_INFO clientInfo[10000]

    I launch 20 client process.

    C / C++ / MFC help sysadmin

  • Multithread c Windows
    M mosine

    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;};
    
    C / C++ / MFC help sysadmin
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups