gpt4 book ai didi

C-SERVER : sizeof() measures top declared thing. 不是新的

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

上面:int main()是:

char response[] = "HTTP/1.1 200 OK\r\n\r\n";

我想在将其发送给客户端之前附加字符串“oooo”。

sprintf(response, "%s %s", response, "oooo");
write(client_fd, response, sizeof(response) - 1);

问题是:sizeof(response)正在使用sizeof顶部“response[]”

不是使用sprintf()新创建的响应的大小

因此,发送给客户端的所有内容都是原始响应,而不是附加有“oooo”的响应。

解决方案 = ?

C-服务器:

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

char response[] = "HTTP/1.1 200 OK\r\n\r\n";

int main()
{
int one = 1, client_fd;
struct sockaddr_in svr_addr, cli_addr;
socklen_t sin_len = sizeof(cli_addr);

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
err(1, "can't open socket");

setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

int port = 84;
svr_addr.sin_family = AF_INET;
svr_addr.sin_addr.s_addr = INADDR_ANY;
svr_addr.sin_port = htons(port);

if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
close(sock);
err(1, "Can't bind");
}

listen(sock, 5);
while (1) {
client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);

printf("----\n");

sprintf(response, "%s %s", response, "oooo");

write(client_fd, response, sizeof(response) - 1);
close(client_fd);
}
}

最佳答案

char response[] = "HTTP/1.1 200 OK\r\n\r\n";

好的,上面的行声明了一个 20 字节的字符数组(19 个 ASCII 字符加上隐式终止 NUL 字节)。

Problem is : sizeof(response) is using the sizeof the top "response[]"

sizeof(response) 返回数组的大小。在C中,数组是固定长度的;它们的大小不能改变。

sprintf(response, "%s %s", response, "oooo");

这是行不通的,原因是@EOF在他的评论中提到:首先,你试图将超过20个字符放入一个20个字符长的数组中;当 sprintf() 写入超过数组末尾时,这将导致未定义的行为。其次,您试图将 sprintf() 响应自身,这不是受支持的事情。

Solution = ?

您想要做的是将一个字符串连接到现有字符串的末尾;为此,您将需要一个更大的数组。如果您知道运行时涉及的字符串的长度,您可以这样做:

const char response[] = "HTTP/1.1 200 OK\r\n\r\n";
const char moreData[] = "oooo";
char biggerResponse[sizeof(response)+sizeof(moreData)-1]; // -1 because we don't need room for both NUL bytes
strcpy(biggerResponse, response);
strcat(biggerResponse, moreData);
write(client_fd, biggerResponse, sizeof(biggerResponse) - 1);

如果 moreData 是一个您在编译时不知道其长度的字符串,您可能必须诉诸动态分配来确保您的 BigResponse 数组足够大以容纳两个字符串:

const char response[] = "HTTP/1.1 200 OK\r\n\r\n";
const char * moreData = "some other string whose length we're not sure of";
const size_t biggerResponseSize = sizeof(response)+strlen(moreData);
char * biggerResponse = (char *) malloc(biggerResponseSize);
strcpy(biggerResponse, response);
strcat(biggerResponse, moreData);
write(client_fd, biggerResponse, biggerResponseSize - 1);
free(biggerResponse);

当然,在 C 中连接字符串很尴尬且容易出错,因此您可以通过分别 write() 来避免整个问题:

const char response[] = "HTTP/1.1 200 OK\r\n\r\n";
const char moreData[] = "oooo";
write(client_fd, response, sizeof(response) - 1);
write(client_fd, moreData, sizeof(moreData) - 1);

关于C-SERVER : sizeof() measures top declared thing. 不是新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42316143/

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