Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用recv()会导致分段错误_C++_Linux_Sockets_Tcp_Segmentation Fault - Fatal编程技术网

C++ 调用recv()会导致分段错误

C++ 调用recv()会导致分段错误,c++,linux,sockets,tcp,segmentation-fault,C++,Linux,Sockets,Tcp,Segmentation Fault,我正在我的linux机器上制作一个带有tcp连接的聊天程序。我有一个工作程序将文本发送到服务器并接收回数据,但是当我使用与recv完全相同的行时,我得到了一个分段错误。代码如下: #include <stdio.h> #include <string.h> // for strlen() #include <stdlib.h> // for exit() #include <sys/socket.h> // for send() a

我正在我的linux机器上制作一个带有tcp连接的聊天程序。我有一个工作程序将文本发送到服务器并接收回数据,但是当我使用与recv完全相同的行时,我得到了一个分段错误。代码如下:

#include <stdio.h>
#include <string.h>     // for strlen()
#include <stdlib.h>     // for exit()
#include <sys/socket.h> // for send() and recv()
#include <unistd.h>     // for sleep(), close()
#include <iostream>

#include "Auxiliary.h"
#include "CreateTCPClientSocket.h"

#define RCVBUFSIZE 32   /* Size of receive buffer */

int main (int argc, char *argv[])
{
    int         sock;                   /* Socket descriptor */
    char *      echoString;             /* String to send to echo server */
    char *      tempString;             /* String to save the cin */
    char        echoBuffer[RCVBUFSIZE + 1]; /* Buffer for received string */
    int         echoStringLen;          /* Length of string to echo */
    int         bytesRcvd;              /* Bytes read in single recv() */

    bool end = false;

    parse_args (argc, argv);

    sock = CreateTCPClientSocket (argv_ip, argv_port);

    while (!end)
    {
        bool messageGet = false;
        std::cout << "What's your message:" << std::endl;
        while(!messageGet)
        {
            std::cin >> tempString;
            if(tempString != "")
            {
                echoString = tempString;
                messageGet = true;
            }
        }

        echoStringLen = strlen(echoString);          /* Determine input length */
        echoString[echoStringLen] = '\0'; 
        echoStringLen += 2;
        delaying();

        send(sock, echoString, echoStringLen, 0);

        info_s("Sent string:", echoString);

        // TODO: add code to receive & display the converted string from the server
        //       use recv()
        bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE-1, 0);
        std::cout << echoBuffer << std::endl;
    }

    delaying ();

    close (sock);
    info ("close & exit");
    exit (0);
}

您有多确定SEGVULT在recv中

您对recv的调用看起来不错,但是,前面的一行正在写入未分配的内存,这将导致segfault:

std::cin >> tempString;
请尝试这样声明tempString:

#define INPUT_BUF_SIZE 100
char tempString[INPUT_BUF_SIZE + 1];
此外,该代码似乎不同寻常:

echoStringLen = strlen(echoString);          /* Determine input length */
echoString[echoStringLen] = '\0'; 
echoStringLen += 2;
echoString已经以null结尾,否则strlen将不会返回正确的结果。由于它已经以null结尾,因此添加\0将一无所获,将长度增加2是错误的。您可以将这三行替换为以下内容:

echoStringLen = strlen(echoString);

bytesRcvd应为ssize\u t类型。看到这个线程:我改变了它,没有什么区别,当我收到命令时仍然会出现分段错误。当我用recv函数注释行时,程序运行得很好,但是当我取消注释时,分段错误又回来了。你有没有试着分配内存来存储用户输入?顺便说一句,像这样的错误在程序的其他地方表现出来并不罕见。在我的机器上编译并运行您的代码cin>>tempString;,所以这绝对是个问题。