Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 未知seg故障_C_Http_Tcp_Segmentation Fault_Client - Fatal编程技术网

C 未知seg故障

C 未知seg故障,c,http,tcp,segmentation-fault,client,C,Http,Tcp,Segmentation Fault,Client,在主电路运行之前,我遇到了seg故障。我对c很生疏,找不到错误。以前的搜索表明,如果结构太大,可能会发生这种情况。我使用的是addrinfo,正如你们所看到的,我已经尝试了malloc'ing它,但我仍然在main之前划分故障 #include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), connect(), send(), and recv()

在主电路运行之前,我遇到了seg故障。我对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;
}
#包括
/*对于printf()和fprintf())*/
#包括
/*对于socket()、connect()、send()和recv()*/
#包括
/*对于sockaddr_in和inet_addr()*/
#包括
/*对于atoi()和exit()*/
#包括
/*对于memset()*/
#包括
/*对于close()*/
#包括
#包括
#包括
#定义RCVBUFSIZE 32
/*接收缓冲区的大小*/
void diewerstror(char*errorMessage){};/*错误处理函数*/
int main(int argc,char*argv[])
{
int sock;/*套接字描述符*/
结构addrinfo提示,*servInfo;/*保存getaddrinfo的结果*/
int rttOption;/*回显服务器端口*/
char*servIP;/*服务器IP地址(虚线四元)*/
char*portNumber;/*要发送到echo服务器的字符串*/
char echoBuffer[RCVBUFSIZE];/*用于回显字符串的缓冲区*/
无符号int-echoStringLen;/*要回显的字符串长度*/
int bytesRcvd,totalBytesRcvd;/*单个recv()中读取的字节数和读取的总字节数*/
智力状态;
servInfo=(struct addrinfo*)malloc(sizeof(struct addrinfo));
char*httpGetRequest=“GET/”;
char*partOfRequest=“HTTP/1.0\r\n连接:保持活动\r\n\r\n”;
strcpy(httpGetRequest,servIP);
strcpy(httpGetRequest,请求的一部分);
echoStringLen=strlen(httpGetRequest);
if((argc<3)| |(argc>4))/*测试参数的正确数量*/
{
fprintf(标准,“用法:%s[]\n”,
argv[0]);
出口(1);
}
printf(“解析后检查”);
servIP=argv[1];/*第一个arg:服务器IP地址(虚线四元)*/
portNumber=argv[2];/*第二个arg:要回显的字符串*/
如果(argc==4&&*argv[3]==p')
{
rttOption=1;//打印出rtt
}
/*构造服务器地址结构*/
memset(&hints,0,sizeof(hints));/*零输出结构*/
hits.ai_family=AF_unsec;/*互联网地址系列*/
hits.ai_socktype=SOCK_流;
//获取链接列表?
if(status=getaddrinfo(servIP、portNumber、提示和servInfo)<0)
{
DiewarError(“getaddrinfo()失败”);
}
/*使用TCP创建可靠的流套接字*/
如果((sock=socket(servInfo->ai_系列,servInfo->ai_socktype,servInfo->ai_协议))<0)
{
DIEWERROR(“套接字()失败”);
}
/*建立与echo服务器的连接*/
如果(连接(sock,servInfo->ai_addr,servInfo->ai_addrlen)<0)
{
DIEWERROR(“连接()失败”);
}
printf(“支票”);
/*将字符串发送到服务器*/
if(发送(sock,httpGetRequest,echoStringLen,0)!=echoStringLen)
{
DIEWERROR(“send()发送的字节数与预期的不同”);
}
/*从服务器接收相同的字符串*/
totalBytesRcvd=0;
printf(“已接收:”;/*设置以打印回显字符串*/
而(totalBytesRcvd如果((bytesRcvd=recv(sock,httpGetRequest,RCVBUFSIZE-1,0))您在使用它之前不初始化
servIP

这一行导致了以下问题:

strcpy(httpGetRequest, servIP);
您也不能修改
httpGetRequest

上述行也会导致此问题


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

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