簡體   English   中英

未知段錯誤

[英]Unknown seg fault

在 main 甚至運行之前,我遇到了段錯誤。 我對 c 生疏了,找不到錯誤。 以前的搜索表明,如果結構太大,就會發生這種情況。 我正在使用 addrinfo,正如你所看到的,我已經嘗試對其進行 malloc'ing,但我仍然在 main 之前進行了 seg fault

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

您也不能修改httpGetRequest

上面的行也會導致此問題。

以某種方式為httpGetRequest分配內存,並且結合其他更改,您的代碼很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM