gpt4 book ai didi

c - 段错误,调试

转载 作者:行者123 更新时间:2023-11-30 17:35:40 25 4
gpt4 key购买 nike

我的程序代码在几个小时前还在工作,我似乎可以想到我更改了什么以使其出现段错误。我很难找到这个讨厌的小错误,如果有人能告诉我调试此类问题的最佳方法,那就太棒了。

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>


/* VSIE client program uses TCP protocol to connect to the remote http server.
The program will take 2 input arguments:
1) command option, get (receive) or head (send)
2) http URL address
*/

#define MAX 80
#define MAX2 1024
#define http "HTTP/1.1"
#define TRUE 1
#define FALSE 0
#define HEADERSTOP "\n\n"
main(int argc, char *argv[])
{
char *temp;
unsigned char *e;
char *line;
char command[MAX];
char server[MAX];
char path[MAX];
char filename[MAX]= "";
char httpString[MAX];
int i, x, f, n, length = 0;
int numBytes = 0;
int getData = TRUE;
int getFlag = FALSE;
int flag = FALSE;
int headFlag = FALSE;
FILE *in;

int sk;
unsigned char buf[MAX2];
struct sockaddr_in remote;
struct hostent *hp;
struct servent *sp;
short port = 0;


// parse input arguments
sscanf(argv[2],"%[^'/']%s",server,path);

if (strcmp(argv[1],"-get") == 0)
{
sprintf(command, "GET");
getFlag = TRUE;
}
else if (strcmp(argv[1],"-head") == 0)
{
sprintf(command, "HEAD");
}

//build http 1.1 GET or HEAD message
sprintf(httpString,"%s %s %s\nHost: %s\n\n", command, path,http,server);

printf("command = %s, server = %s, path = %s\n", command, server, path);
printf("httpString = %s\n",httpString);

//parse filename from path
length = strlen(path);
x=0;
f=0;

for(i = 0; i < length; i++)
{
//printf("path[%d] = %c \n",i,path[i]);
if ((flag == TRUE) & (f == 2))
{
filename[x] = path[i];
x++;
}

if (path[i] == '/')
{
flag = TRUE;
f++;
}

}

printf("filename = %s\n", filename);

//if command = get, open filename
//if(command == "-get")
if (getFlag == TRUE)
{
if((in = fopen (filename,"w")) == NULL)
{
//printf("FAILURE: opening input file %s\n",filename);
perror("FAILURE: opening input file");
exit(1);
}
printf("file opened successfully\n");
}

//get internet address of host & port number of http service
hp = gethostbyname(server);
if (hp == NULL)
{
printf("Can't find host name. %s\n", server);
exit (1);
}

//copy the h_addr (source) to s_add (destination) for n bytes specified by length
bcopy(hp->h_addr,&remote.sin_addr.s_addr,hp->h_length);

/* get the port number */
sp = getservbyname("http", "tcp");
if (sp == NULL)
{
printf("can't find port # %d\n",sp->s_port);
exit (1);
}
port = sp->s_port;
remote.sin_port = sp->s_port;
printf("port = %d, port = %d \n", port, remote.sin_port);

//create socket for http server - socket type: Sock_Stream, protocol: TCP
sk = socket(AF_INET,SOCK_STREAM,0);
if (sk < 0)
{
perror("error opening socket");
exit(1);
}
remote.sin_family = AF_INET;

//initiate connection to the server address w/ TCP socket
if (connect(sk, (struct sockaddr *) &remote, sizeof(remote)) < 0)
{
printf("connect fails!\n");
exit(1);
}
printf("connection successful\n");

//send http message
printf("send message:%s\n", httpString);
//send(sk,httpString,strlen(httpString)+1,0);
if(send(sk,httpString,sizeof(httpString),0) < 0)
{
printf("send() failed");
//exit(1);
}

n = 1;
//Loop until all data received
while(getData == TRUE)
{
//wait for and print the return message
numBytes = recv(sk,buf,sizeof(buf),0);
if (numBytes < 0)
{
perror("error reading from socket");
break;
}
else if (numBytes < MAX2)
{
getData = FALSE;
printf("***end while loop****\n");
}
if (headFlag == FALSE){

e = memchr(buf, '\n', sizeof(buf));

while (*(e+1) != '\r'){
e = memchr(e+1, '\n', sizeof(buf));
}
sprintf(temp, "%s", e);
headFlag = TRUE;
}



printf("\n****number of bytes received %d****\n",numBytes);
line = strtok(buf, "\n");
while(line != NULL) {
if (strstr(line, "Content-Length:")!= NULL){
int dataSize;
sscanf(line, "Content-Length: %d", &dataSize);
printf("The Data Size: %d\n", dataSize);
}
line = strtok(NULL, "\n");


//saved the retrieved content into the file (input argument)

if (getFlag == TRUE)
{
//printf("write output\n");

if(e != NULL){
printf("%.*s\n", (int)(numBytes-sizeof(temp)), buf);
fwrite(temp, sizeof(temp), 1, in);
e = NULL;
}else{
fwrite(buf, numBytes, 1, in);
}

}
n++;
} // end while()

//close socket & file
close(sk);

if(fclose(in) !=0)
{
perror("FAILURE: Closing input file");
exit(1);
}

return 0;
} //end main()

最佳答案

问题位于

    sprintf(temp, "%s", e);

 if(fclose(in) !=0)

验证这两个地方的代码。

注释掉这两部分后,就没有段错误了。

关于c - 段错误,调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22968142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com