gpt4 book ai didi

c - 在 strcat() 中编程段错误

转载 作者:行者123 更新时间:2023-11-30 19:54:46 25 4
gpt4 key购买 nike

我正在尝试用 C 语言编写一个 Web 服务器,但我的代码目前出现段错误,我不知道为什么。这似乎与我的 strcats 有关,但这就是我所能得到的。我已经发布了代码和 gdb 输出。任何帮助是极大的赞赏。

***代码<强>***

    /*  web-server.c    */

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFLEN 1500
#define BACKLOG 10

static int server_socket(int port) {
int fd;
struct sockaddr_in servaddr;

if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("Unable to create socket");
return -1;
}
printf("created socket\n");
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);

if (bind(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
perror("Unable to bind to port");
return -1;
}
printf("binding completed\n");
if (listen(fd, BACKLOG) == -1){
perror("Unable to listen for connections");
return -1;
}
printf("socket setup\n");
return fd;

}

static char *html_request(char *request) {
char request_code[3], url[BUFLEN], http_code[BUFLEN];
printf(request);
printf("\n");
sscanf(request, "%3s %s %s", request_code, url, http_code);
return url;
}


int main() {
int connfd, servfd, rlen, i, eof;
int content_length = 0;
char buf[BUFLEN];
struct sockaddr_in saddr;
char *response;
char *read_buffer = malloc(1);
char *response200 = "HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close\nConnection-Length: ";
char *response404 = "HTTP/1.1 404 Not Found\nContent-Type: text/html\nConnection: close\nConnection-Length: ";
FILE *in;
char buffer[1500], content_buffer[5];
char *headers = malloc(BUFLEN);
char *output = malloc(BUFLEN);
socklen_t saddr_len = sizeof(saddr);
servfd = server_socket(8080);
printf("before connection\n");
if ((connfd = accept(servfd, (struct sockaddr *)&saddr, &saddr_len)) == -1) {
perror("Unable to accept connection");
return -1;
}
else {
printf("Accept connection\n");

}
if ((rlen = read(connfd, buf, BUFLEN)) > 0) {
realloc(read_buffer,rlen);
for (i = 0; i < rlen; i++){
printf("%c", buf[i]);
read_buffer[i] = buf[i];
}
printf("\n");

}
printf("IMMA BEAST!\n");
response = html_request(read_buffer);
printf(response);

if (strcmp(response, "/index.html") == 0){
printf("\nMatches!");
in = fopen("index.txt", "r");
if (in == NULL){
printf("Read fail!");
}
while ((eof = fread(buffer, 1, 296, in )) > 0){
content_length = content_length + 1;
}
snprintf(content_buffer, 5, "%d" ,content_length);
headers = strcat(response200, content_buffer);
}
else{
printf("\nFails!");
in = fopen("index-error.txt", "r");
if (in == NULL){
printf("Read fail!");
}
while ((eof = fread(buffer, 1, 300, in )) > 0){
content_length = content_length + 1;
}
snprintf(content_buffer, 5, "%d" ,content_length);
headers = strcat(response404, content_buffer);
}
fclose(in);
output = strcat(headers, buffer);
write(connfd, output, BUFLEN);
close(connfd);
free(read_buffer);
free(headers);
free(output);

return 0;

}

* GDB 输出 *

Program received signal SIGSEGV, Segmentation fault.
0x0000003d4647eff0 in strcat () from /lib64/libc.so.6
(gdb) where
#0 0x0000003d4647eff0 in strcat () from /lib64/libc.so.6
#1 0x0000000000400ecd in main ()
(gdb) n
Single stepping until exit from function strcat,
which has no line number information.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

最佳答案

这根本不对:

strcat("HTTP/1.1 404 Not Found\nContent-Type: text/html\nConnection: close\nConnection-Length: ", content_buffer);

strcat()将第二个参数附加到第一个参数。第一个参数是一个文字字符串,绝不能修改。 C 允许编译的唯一原因是一个不幸的事实,即旧代码依赖于 char[] 类型的文字字符串。而不是const char[] .

关于c - 在 strcat() 中编程段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15089804/

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