Segmentation Fault

This is a discussion on Segmentation Fault within the Networking/Device Communication forums, part of the General Programming Boards category; Hello. I am getting a segmentation fault on my socket program. First I coded this program without sockets using printf ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    175

    Segmentation Fault

    Hello.
    I am getting a segmentation fault on my socket program. First I coded this program without sockets using printf statements and the program worked exactly as intended. Now when I "combined" the two, I get a segmentation fault at a certain point in the program I get it only in "case 3" of my code. I am hoping someone can spot what I might be missing. I am posting what I believe to be relevant code as it is a pretty long program.
    classes.h:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #define BINFILE "classes.db"
    #define MAX_RECORD 10
    #define BUF 2048
    
    static int      sockfd;
    
    struct sockaddr_in      self, client_addr;
    
    typedef enum {MW, TR} days;
    
    typedef struct {
        int hour, min;
    } Time;
    
    typedef struct {
        char Dept[5];
        int course, sect;
        days meet_days;
        Time start, end;
        char instr[20];
    } sched_record;
    
    int sortFunction(const void *p, const void *q);
    Main portion of the sockets. (client side)
    Code:
     int switchInput;
        int i = 0;
        int connected;
        int sock;
        int bytes_received;
        int sin_size;
        int true = 1;
        int tempCourse = 0;
        char send_data[BUF];
        char recv_data[BUF];
        char tempDept[5];
        char tempDay[2];
        char tempTime[2];
        FILE *filePointer;
        sched_record data[MAX_RECORD];
        filePointer = fopen (BINFILE, "rb");
    
        if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
            perror ("Socket");
            exit(1);
            }
    
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)) == -1) {
            perror ("Setsocketopt");
            exit(1);
            }
    
        self.sin_family = AF_INET;
        self.sin_port = htons(nunya);
        bzero(&(self.sin_zero), 8);
        self.sin_addr.s_addr = inet_addr ("127.0.0.0");
    
        if (bind (sock, (struct sockaddr *)(&self), sizeof (self)) < 0) {
            perror ("Unable to bind");
            exit(1);
            }
         if (listen(sock, 5) == -1) {
            perror("Listen");
            exit(1);
        }
    
        if (filePointer == NULL) {
                perror("**Can't open file**");
                exit(1);
            }
    
        printf("\nTCPServer waiting for client on port 13018");
            fflush(stdout);
    
        while(1) {
    
            sin_size = sizeof(struct sockaddr_in);
            connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
    
            printf("\n I got a connection from (%s, %d)",
                   inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
            printf("\n");
    
            char send_data[] = "Server is Ready";
            send(connected, send_data, strlen(send_data), 0);
    
            while(1) {
    
                bytes_received = recv(connected, recv_data, BUF, 0);
                recv_data[bytes_received] = '\0';
                switchInput = atoi (recv_data);
    
                switch(switchInput) {
    Menu for case 3
    Code:
     case 3:
                    fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                    fclose(filePointer);
                    char send_data_3[] = "Enter Department Name or A for all: ";
                    send(connected, send_data_3, strlen(send_data_3), 0);
                    bytes_received = recv(connected, recv_data, BUF, 0);
                    recv_data[bytes_received] = '\0';
                    strcpy(tempDept, recv_data);
    
                    char send_data_4[] = "Enter Course Number of 0 for any course in department: ";
                    send(connected, send_data_4, strlen(send_data_4), 0);
                    bytes_received = recv(connected, recv_data, BUF, 0);
                    recv_data[bytes_received] = '\0';
                    tempCourse = atoi (recv_data);
    
                    char send_data_5[] = "Enter Days of Class, M for MW, T for TTH or D for any: ";
                    send(connected, send_data_5, strlen(send_data_5), 0);
                    bytes_received = recv(connected, recv_data, BUF, 0);
                    recv_data[bytes_received] = '\0';
                    strcpy(tempDay, recv_data);
    
                    char send_data_6[] = "Enter A for mornings, P for afternoons of D for any: ";
                    send(connected, send_data_6, strlen(send_data_6), 0);
                    bytes_received = recv(connected, recv_data, BUF, 0);
                    recv_data[bytes_received] = '\0';
                    strcpy(tempTime, recv_data);
    And the tested problem area:
    Code:
     else if ((strcmp(tempDept, data[i].Dept)==0) && tempCourse == 0 && (strcmp(tempDay, "D")==0) && (strcmp(tempTime, "D")==0)) {
                                     sprintf("%s %d %d %2s %02d%02d %02d%02d %s\n", data[i].Dept, data[i].course, data[i].sect, data[i].meet_days == MW ? "MW" : "TR",
                                           data[i].start.hour, data[i].start.min, data[i].end.hour, data[i].end.min, data[i].instr);
                                           send(connected, send_data, strlen(send_data), 0);
                        }
    Thank you for looking this over. I really appreciate it!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Every socket related function call must be checked for errors. In particular, you are not checking any of your calls to send. Also, you need to make sure that you are not overwriting yout tempDept, tempDay or tempTime arrays with strcpy.

    Then, you need to turn up the warnings on your compiler, and read the documentation for sprintf. It prints the formatted string and data into a string/buffer.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Quote Originally Posted by anduril462 View Post
    Every socket related function call must be checked for errors. In particular, you are not checking any of your calls to send. Also, you need to make sure that you are not overwriting yout tempDept, tempDay or tempTime arrays with strcpy.

    Then, you need to turn up the warnings on your compiler, and read the documentation for sprintf. It prints the formatted string and data into a string/buffer.
    The problem was the sprintf statement had the wrong format. My compiler warnins are -Wall and -w.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    The -Wall and -w are opposites, -Wall is generate all messages, the -w is inhibit all warnings ( no warnings). You really should use -Wall -Wextra at minimum.


    Jim

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you're expecting response strings of some exact size then you should check the return values for that size, since otherwise it's an error.

    And the name switchInput simply says that the variable is used as input to a switch, which is obvious from the syntax of the code itself. It's better for a variable name to contain semantic information. What are you switching over? If it's menu options, then how about menuOption?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By gkoenig in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 01:14 PM
  2. segmentation fault!!
    By Necromancer in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 07:03 AM
  3. Segmentation Fault
    By jat421 in forum C Programming
    Replies: 6
    Last Post: 04-03-2005, 02:26 PM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21