我在主程序运行之前就遇到了段错误。我对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);
您也不能修改
httpGetRequest
。上面的行也导致此问题。
以某种方式为
httpGetRequest
分配内存,再结合其他更改,您的代码就可以了。