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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multithread c Windows

Multithread c Windows

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadmin
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mosine
    wrote on last edited by
    #1

    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;};
    
    J 1 Reply Last reply
    0
    • 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;};
      
      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      You are accepting an unlimited number of connections but did not check if cont reaches the limit of your clientInfo and hClientThread array sizes. This results in segmentation faults when cont is equal to the number of array items.

      M 1 Reply Last reply
      0
      • J Jochen Arndt

        You are accepting an unlimited number of connections but did not check if cont reaches the limit of your clientInfo and hClientThread array sizes. This results in segmentation faults when cont is equal to the number of array items.

        M Offline
        M Offline
        mosine
        wrote on last edited by
        #3

        Thanks for reply. I declared:

        HANDLE hClientThread[65000]
        struct CLIENT_INFO clientInfo[10000]

        I launch 20 client process.

        M J 2 Replies Last reply
        0
        • M mosine

          Thanks for reply. I declared:

          HANDLE hClientThread[65000]
          struct CLIENT_INFO clientInfo[10000]

          I launch 20 client process.

          M Offline
          M Offline
          mosine
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • M mosine

            Thanks for reply. I declared:

            HANDLE hClientThread[65000]
            struct CLIENT_INFO clientInfo[10000]

            I launch 20 client process.

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            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 or sizeof() when using a fixed size.

            M 1 Reply Last reply
            0
            • J Jochen Arndt

              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 or sizeof() when using a fixed size.

              M Offline
              M Offline
              mosine
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

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