C/unix套接字:由于char指针(?)而导致分段错误(内核哑) [英] C/unix Sockets: Segmentation fault (core dumbed) due to char pointer(?)

查看:79
本文介绍了C/unix套接字:由于char指针(?)而导致分段错误(内核哑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(也许是这个问题的副本,但我发现它相当很难自己找出问题所在,因此,如果此处的答案与其他问题几乎相同,我将尝试删除该问题)

(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)

我想制作一个客户端和一个服务器程序(有多个使用pthread的客户端),其中每个客户端向服务器发送N个数字(由用户输入),并且服务器返回这些数字的平均值,并带有确定"消息",如果平均值> 10,否则服务器返回不正常的消息".

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:

客户:

#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;
    }

我不断从客户端获取分段错误,我猜想指针在这里sequencecheck=recv(s, str, strlen(str), 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);     

您尚未为str分配任何值,因此它没有指向字符串.因此,调用strlen没有意义-您要尝试使用什么字符串来获取长度?

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.

您可以查看有关现有协议(例如HTTP,SMTP或IRC)的文档,以了解概念.显然,您不必这么详细,但是它应该指定服务器和客户端如何启动连接,确定消息的开始和结束位置以及断开连接.

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.

这篇关于C/unix套接字:由于char指针(?)而导致分段错误(内核哑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆