【问题标题】:Unknown seg fault未知的段错误
【发布时间】:2020-01-09 16:51:42
【问题描述】:

在 main 运行之前,我遇到了段错误。我对 c 生疏了,找不到错误。以前的搜索表明,如果结构太大,可能会发生这种情况。我正在使用 addrinfo,正如你所看到的,我已经尝试对其进行 malloc'ing,但我仍然在 main 之前出现段错误

#include <stdio.h>
/* for printf() and fprintf() */
#include <sys/socket.h> 
/* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h>   
/* for sockaddr_in and inet_addr() */
#include <stdlib.h>        
/* for atoi() and exit() */
#include <string.h>       
/* for memset() */
#include <unistd.h>      
/* for close() */ 
#include <sys/types.h>

#include <netinet/in.h>

#include <netdb.h>

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

void DieWithError(char *errorMessage){};  /* Error handling function */ 

int main(int argc, char *argv[])
{
    int sock;                         /* Socket descriptor */
    struct addrinfo hints, *servInfo; /*holds the result of getaddrinfo */
    int rttOption;      /* Echo server port */
    char *servIP;                     /* Server IP address (dotted quad) */
    char *portNumber;                 /* String to send to echo server */
    char echoBuffer[RCVBUFSIZE];      /* Buffer for echo string */
    unsigned int echoStringLen;       /* Length of string to echo */
    int bytesRcvd, totalBytesRcvd;    /* Bytes read in single recv() and total bytes read */
    int status;

    servInfo = (struct addrinfo *) malloc (sizeof(struct addrinfo));


    char *httpGetRequest = "GET /";
    char *partOfRequest = "HTTP/1.0\r\nConnection: keep-alive\r\n\r\n";
    strcpy(httpGetRequest, servIP);
    strcpy(httpGetRequest, partOfRequest);

    echoStringLen = strlen(httpGetRequest);
    if ((argc < 3) || (argc > 4))    /* Test for correct number of arguments */
        {
            fprintf(stderr, "Usage: %s <Server URL/IP> <Port Number> [<Option -p>]\n",
                argv[0]);
            exit(1);
        }
    printf("Check after parsing");

    servIP = argv[1];         /* First arg: server IP address (dotted quad) */
    portNumber = argv[2];     /* Second arg: string to echo */
    if (argc == 4 && *argv[3] == 'p')
    {
        rttOption = 1; //print out the rtt
    }

    /* Construct the server address structure */
    memset(&hints, 0, sizeof(hints));     /* Zero out structure */     
    hints.ai_family      = AF_UNSPEC;             /* Internet address family */
    hints.ai_socktype    = SOCK_STREAM;

    //getting the linked list?
    if(status = getaddrinfo(servIP, portNumber, &hints, &servInfo) < 0)
        {
            DieWithError("getaddrinfo() failed");
        }


    /* Create a reliable, stream socket using TCP */
    if ((sock = socket(servInfo->ai_family, servInfo->ai_socktype, servInfo->ai_protocol)) < 0)
    {
         DieWithError("socket() failed");
    }


    /* Establish the connection to the echo server */
    if (connect(sock, servInfo->ai_addr, servInfo->ai_addrlen) < 0)
    {
       DieWithError("connect() failed");
    }
    printf("Check");
    /* Send the string to the server */
    if (send (sock, httpGetRequest, echoStringLen, 0) != echoStringLen)
    {
        DieWithError("send() sent a different number of bytes than expected");
    }
    /* Receive the same string back from the server */
    totalBytesRcvd = 0;
    printf("Received: ");   /* Setup to print the echoed string */


    while (totalBytesRcvd < echoStringLen)
    {
        /* Receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender */
        if ((bytesRcvd = recv(sock, httpGetRequest, RCVBUFSIZE -1, 0)) <= 0)     
        {       
            DieWithError("recv() failed or connection closed prematurely"); 
        }
            totalBytesRcvd += bytesRcvd;   /* Keep tally of total bytes */ 
            echoBuffer[bytesRcvd] = '\0';  /* Terminate the string! */  
            printf("%s", httpGetRequest);      /* Print the echo buffer */
    }
    printf("\n");    /* Print a final linefeed */
    close(sock);
    exit(0);
    return 0;
}

我希望至少会遇到命令行输入的错误捕获。但相反,我们在到达那里之前是核心转储。

【问题讨论】:

  • 在使用之前不要初始化servIP。此行导致问题:strcpy(httpGetRequest, servIP);
  • 您正试图覆盖只读字符串。

标签: c http tcp segmentation-fault client


【解决方案1】:

在使用servIP之前不要初始化它。

此行导致问题:

strcpy(httpGetRequest, servIP);

你也不能修改httpGetRequest

上面的行也会导致这个问题。

以某种方式为httpGetRequest 分配内存,结合其他更改,您的代码就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2021-01-12
    • 1970-01-01
    • 2019-05-15
    • 2022-01-24
    相关资源
    最近更新 更多