Thread: Segmentation fault on TCP server.

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    9

    Segmentation fault on TCP server.

    I have developed a server in C ++ and a client that connects to it.

    Both are running on a PC with linux.

    When we run the server and a client is connected to it, a
    Segmentation fault on the server, specifically, on the line:
    sClient = ((struct ThreadArgs *) threadArgs) -> sClient;

    Attached is the source code of the server.
    Attached Files Attached Files

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    593
    You have a warning in your code. It may not actually be a problem, but it is usually best to fix all warnings.
    Code:
    ServerTCP.cpp: In member function ‘void ServerTCP::waitConnections()’:
    ServerTCP.cpp:116:70: warning: converting from ‘void* (ServerTCP::*)(void*)’ to ‘THREAD_FUNC_PTR {aka void* (*)(void*)}’ [-Wpedantic]
         if (pthread_create(&threadID, NULL, (THREAD_FUNC_PTR)&ServerTCP::ThreadMain, (void *)threadArgs
    Making ThreadMain static silences the warning.
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,034
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    The TCP server that I am developing as a C ++ class is based on a C client-server application that works perfectly.

    The C ++ class of the server is intended to be a replica of the server written in C (and it works fine).

    I attach the code of the client and the server written in C.

    When I test the server in C ++ I also use the client written in C.

    If the C server works fine, but the C++ server gets a segmentation fault, what am I doing wrong when I migrate the server code from C to C ++?
    Last edited by jstechg; 02-04-2019 at 03:31 PM.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    client.c : (It works fine)
    ----------
    Code:
        #include <stdio.h>
        #include <sys/socket.h>
        #include <arpa/inet.h>
        #include <netdb.h>
        #include <stdlib.h>
        #include <string.h>
        #include <unistd.h>
    
    
        #define MAX_FRAME_LENGTH    65000
    
    
        int main()
        {
          struct hostent *host;
    
          char data_out[MAX_FRAME_LENGTH];
          int ret, aux;
    
          char serverAddress[64];
          int iPort;
    
          int s;
          struct sockaddr_in addr;
    
          char keys[50];
    
          strcpy (serverAddress, "localhost");
          iPort = 5772;
    
          s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
          if(s < 0)
          {
            printf("Error: Call to socket has failed.\n");
            exit(1);
          }
    
          addr.sin_family = AF_INET;
          addr.sin_port = htons(iPort);
          addr.sin_addr.s_addr = inet_addr(serverAddress);
    
          if(addr.sin_addr.s_addr == INADDR_NONE)
          {
            host = NULL;
            host = gethostbyname(serverAddress);
    
            if(host == NULL)
            {
              printf("Error\nUnknown host: %s\n", serverAddress);
              exit(1);
            }
            memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
          }
    
          ret = connect(s, (struct sockaddr *) &addr, sizeof(addr));
          if (ret < 0)
          {
            printf("Error: Call to connect(s, (SOCKADDR) addr, sizeof(addr)); has failed.\n");
            exit(1);
          }
    
          printf("Client has connected to %s:%d\n",serverAddress, iPort);
          printf ("Please, press any key to send data to the Server. Press 'e' to finish.\n\n");
    
          do
          {
            for (aux=0; aux<20; aux++)
              data_out[aux] = aux;
    
            ret = send(s, data_out, 20, 0);
            if (ret < 0)
            {
              printf("Error: Call to send has failed.");
              exit(1);
            }
            else if (ret != 20)
              printf ("ERROR sending data: Wrong length of data.\n");
    
            aux = scanf ("%s", keys);
          }
          while ((strcmp (keys, "e") != 0) && (strcmp(keys, "E") != 0));
    
          close(s);
          return 0;
        }

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    serverTCP.c: (C version. It works fine)
    ------------
    Code:
        #include <stdio.h>
        #include <sys/socket.h>
        #include <arpa/inet.h>
        #include <stdlib.h>
        #include <string.h>
        #include <unistd.h>
        #include <pthread.h>
        
        
        
        #define MAX_DATA_LENGTH    65000
        
        
        
        struct ThreadArgs
           {
           int sClient;
           };
        
        void *ThreadMain(void *arg);    /* Main program of a thread */
        
        
        
        int main()
        {
          int ret;
          unsigned int iPort, iAddrLen;
          int sListen, sClient;
          struct sockaddr_in addr, remote_addr;
          pthread_t threadID;
          struct ThreadArgs *threadArgs;
        
          iPort = 5772;
        
          sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
          if (sListen < 0)
          {
            printf("Error: Call to socket(AF_INET, SOCK_STREAM, IPPROTO_IP); has failed.\n");
            exit(1);
          }
        
          addr.sin_family = AF_INET;
          addr.sin_port = htons(iPort);
          addr.sin_addr.s_addr = htonl(INADDR_ANY);
        
          ret = bind(sListen, (struct sockaddr *) &addr, sizeof(addr));
          if(ret < 0)
          {
            printf("Error: Call to bind(sListen, (struct sockaddr *) &addr, sizeof(addr)); has failed.\n");
            exit(1);
          }
        
          ret = listen(sListen, 10);
          if(ret < 0)
          {
            printf("Error: Call to listen(sListen, 10); has failed.\n");
            exit(1);
          }
        
          for(;;)
          {
            printf("Waiting for connection from a client...\n");
        
            iAddrLen = sizeof(remote_addr);
        
            sClient = accept(sListen, (struct sockaddr *) &remote_addr, &iAddrLen);
            if(sClient < 0)
            {
              printf("Error: Call to accept(sListen, (struct sockaddr *) remote_addr, sizeof(remote_addr)); has failed.\n");
              exit(1);
            }
        
            printf("%s has connected.\n\n", inet_ntoa(remote_addr.sin_addr));
               
            if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) == NULL)
            {
              printf("Error: malloc() for threadArgs has failed.\n");
              exit(1);
            }
        
            threadArgs -> sClient = sClient;
        
            if (pthread_create(&threadID, NULL, ThreadMain, (void *)threadArgs) != 0)
            {
              printf("Error: pthread_create() has failed.\n");
              exit(1);
            }
          }
        }
        
        
        
        void *ThreadMain(void *threadArgs)
        {
          int sClient;
          int ret;
          char data_in[MAX_DATA_LENGTH];
        
          pthread_detach(pthread_self() ) ;
        
          sClient = ((struct ThreadArgs *) threadArgs) -> sClient;
          free(threadArgs);
        
          ret = 1;
          while (ret != 0)
          {
            printf("Waiting to receive client data.\n");
        
            ret = recv(sClient, data_in, MAX_DATA_LENGTH, 0);
            if (ret < 0)
            {
              printf("Error: Call to recv(sClient, frame_in, sizeof(frame_in), 0); has failed.\n");
              exit(1);
            }
        
            printf("Received data from client %d\n\n", sClient);
          }
        
          close(sClient);
          return NULL;
        }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,034
    > what am I doing wrong when I migrate the server code from C to C ++?
    Do as john.c said, make your class member thread function static.

    pthread_create knows nothing of 'this' pointers.

    Perhaps you should use std::thread to hide that detail from you.
    std::thread - cppreference.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    Thanks, I am going to try this. But I want to have a multithread server. So, I think that defining the thread function as static is not a good idea.

    Colud be that I define it as friend instead?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,528
    A static member function is very different from a static local variable, if that's why you're objecting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation Fault Help
    By DuckCowMooQuack in forum C Programming
    Replies: 11
    Last Post: 02-08-2011, 08:03 PM
  3. simple server & client getting Segmentation fault error
    By blondieoubre in forum Linux Programming
    Replies: 2
    Last Post: 12-11-2007, 06:26 AM
  4. client-server program: segmentation fault
    By ankurcse in forum Linux Programming
    Replies: 6
    Last Post: 07-17-2006, 07:54 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread