简体   繁体   中英

C/unix Sockets: Segmentation fault (core dumbed) due to char pointer(?)

(maybe a duplicate of this question, but I find it quite hard to pinpoint my problem myself, so if the answer here is even close to identical to the other question, I'll try to delete the question)

I want to make a client and a server program (with multiple clients using pthreads), where each client sends N numbers (input by the user) to the server, and the server returns the average of these numbers with an "ok message" if average>10, else the server returns a "not ok message".

After the client prints the results of the server, the user is asked if he wants to enter a new number sequence for its average.

Here is where I got so far:

Client:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCK_PATH "avg_socket"

    int main(void)
    {
        int i, s, t, len, done;
        int numofintegers;
        int yesorno;
        int average, avg;
        char *sequencecheck;

        struct sockaddr_un remote;
        char str[100];

        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        printf("Trying to connect...\n");

        remote.sun_family = AF_UNIX;
        strcpy(remote.sun_path, SOCK_PATH);
        len = strlen(remote.sun_path) + sizeof(remote.sun_family);
        if (connect(s, (struct sockaddr *)&remote, len) == -1) {
            perror("connect");
            exit(1);
        }

        printf("Connected.\n");

        done = 0;
        do {
            printf("Give the number of integers:");
           fscanf(stdin, "%d", numofintegers);

            send(s, &numofintegers, sizeof(numofintegers), 0);

            int ar[numofintegers];

            for(i=0;i<numofintegers;i++)
            {
                printf("Give the number %d \n", i+1);
                printf("> ");
                fscanf(stdin, "%d", ar[i]);
            }

            send(s, &ar, numofintegers*sizeof(int), 0);

            sequencecheck=recv(s, str, 100, 0);     

            if(strcmp(sequencecheck, "Sequence Ok"))
            {
                average=recv(s, avg, sizeof(avg), 0);
                printf("Average of numbers: %d", avg);
            }


           send(s, &yesorno, sizeof(yesorno), 0);

           if(!yesorno)
       {
                done=1;
        }

         } while (!done);

        close(s);

        return 0;
    }

I keep getting segmentation faults from the client side and I guess a pointer went rogue somewhere here sequencecheck=recv(s, str, strlen(str), 0); .

But as I said above, I have a hard time finding the actual problem.

Which are the problems in my code?

        sequencecheck=recv(s, str, strlen(str), 0);     

You haven't assigned any value to str yet, so it doesn't point to a string. So calling strlen makes no sense -- what string are you trying to get the length of?

I strongly urge you to stop coding and start documenting the network protocol you plan to use. Document it at the byte level. Define what a "message" is and specify how they're delineated.

You can look at documentation for existing protocols (like HTTP, SMTP, or IRC) to get an idea. Obviously, yours doesn't have to be that detailed, but it should specify how the server and client start a connection, decide where messages begin and end, and tear down the connection.

It should provide enough details so that two different people could implement the server and client using just the protocol documentation and be able to exchange data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM