简体   繁体   English

C / unix套接字:由于char指针(?)而导致分段错误(内核哑)

[英]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". 我想制作一个客户端和一个服务器程序(使用pthread的多个客户端),其中每个客户端向服务器发送N个数字(由用户输入),并且服务器在以下情况下通过“确定消息”返回这些数字的平均值平均值> 10,否则服务器返回“不正确的消息”。

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); 我一直在从客户端获取分段错误,并且我猜想指针在这里某个地方流氓了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. 您尚未为str分配任何值,因此它没有指向字符串。 So calling strlen makes no sense -- what string are you trying to get the length of? 因此,调用strlen没有任何意义-您要尝试使用什么字符串来获取长度?

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. 您可以查看现有协议(例如HTTP,SMTP或IRC)的文档以了解概念。 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. 它应该提供足够的详细信息,以便两个不同的人可以仅使用协议文档来实现服务器和客户端,并能够交换数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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