Thread: Socket Programming getting segfault

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Socket Programming getting segfault

    Hello,
    I am new here and hopefully I can get some help. I am trying to send a packet across a client/server communication. I am getting a seg fault (while running the program(It compiles fine)) when I try to read the neighbor file, You should be able to see this below the comment /***** Read neighbor file***/ :
    Please forgive any mistakes and thanks for any help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h> 
    
    
    typedef struct {
      int dist;
      //int  Port;
      char source[21];
      char dest[21];
      //int  Min, Max;
    } PACKET;
    
    
    
    
    /*struct packet {
        int dist;
        char source[21];
        char dest[21];
    };*/
    
    
    void error(const char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
    
    
        char buffer[256];
        if (argc < 3) {
            fprintf(stderr,"usage: IPaddress hostname\n");
            exit(1);
        }
        
        FILE *pFile;
        char virtual[21];
        int port;
        char addr[21];
        char line[21];
        
        PACKET *pkt;
        //struct packet *pkt;
        FILE *neighbor;
        char s[21];
        char d[21]; 
         
        if (argc < 2) {
            fprintf(stderr,"ERROR: Enter an IP Addr\n");
            exit(1);
        }
         
        pFile = fopen("node.config", "r");
         
        if (pFile != NULL)
        {
             
             while (!feof(pFile))
             {
                 fscanf(pFile, "%s %d %s", virtual, &port, addr);
                 if(strcmp(virtual, argv[1]) == 0)
                     break;
                 //printf("%s %d %s\n", virtual, port, addr);
               }
    
    
             printf("%d\n", port);
             fclose(pFile);
        }
        else
        {
            printf("Could not open file.\n");
        }
        
        /******** Read the neighbor file *********/
        
        neighbor = fopen("neighbor.config", "r");
        
        if (neighbor != NULL)
        {
             
             while (!feof(neighbor))
             {
                 printf("here1\n");
                 //fscanf(neighbor, "%s %s %d", pkt->source, pkt->dest, &pkt->dist);
                 //fscanf(neighbor, "%s %s %d", s, d, &pkt->dist);
                 //if(strcmp(virtual, argv[1]) == 0)
                     //break;
                 printf("here\n");
                 printf("%s %s %d\n", pkt->source, pkt->dest, pkt->dist);
               }
    
    
             printf("%d\n", port);
             fclose(neighbor);
        }
        else
        {
            printf("Could not open file.\n");
        }
        
        //portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[2]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(port);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR connecting");
        printf("Please enter the message: ");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        
        //n = write(sockfd,buffer,strlen(buffer));
        //n = send(sockfd, buffer, strlen(buffer), 0);
        n = send(sockfd, pkt, sizeof(pkt), 0);
        
        if (n < 0) 
             error("ERROR writing to socket");
        bzero(buffer,256);
        //n = read(sockfd,buffer,255);
        n = recv(sockfd, buffer, 255, 0);
        if (n < 0) 
             error("ERROR reading from socket");
        printf("%s\n",buffer);
        close(sockfd);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    PACKET *pkt;
    isn't enough. It will point to a random (and likely invalid) address. You need to allocate memory for the structure.

    Code:
    while (!feof(pFile))
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Thanks a bunch Andreas. I was able to solve the problem by allocating space for PACKET
    pkt = (PACKET *)malloc(sizeof(PACKET));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  3. socket programming
    By sunil in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-28-2008, 07:12 AM
  4. Other programming questions: segfault in free
    By Neeharika in forum C Programming
    Replies: 2
    Last Post: 02-21-2006, 06:35 AM
  5. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM

Tags for this Thread