gpt4 book ai didi

c - C 套接字中从浏览器接收到的非 ASCII 字节

转载 作者:行者123 更新时间:2023-11-30 15:38:15 24 4
gpt4 key购买 nike

我已经成功创建了一个接受来自 Web 浏览器的连接的 C 服务器,但是,我从套接字收到的 HTTP 请求是随机字节,我似乎无法将其转换为 ASCII 值,如下所示:

\00\00\00\00\00\00\00\00\A9Ag_\BD\00\00\00\00\00\00\00\00\00\00\BD\88_\BD\00\00\C0\B4\88_\BD\00\00<\A7f_\BD\00\00\F0,+_\BD\00\00\A4f_\BD\00\00\00\00\00\00\00\00\00\9D\00\00\00\00\00\00\00\00\00\00\00\000b0\D8\FF\00\00\90a0\D8\FF\00\00\BD\88_\BD\00\00\C0\E5\88_\BD\00\00\F8و_\BD\00\00\00\00\00\00\00\00\00\A9Ag_\BD\00\00\00\00\00\00\00\00\00\00\BD\88_\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\FF\00\00\F8و_\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F8\E7\88_\BD\00\00(`\00\00\00\00\00\00\00\00\00\00\00\00\00\C0\E5\88_\BD\00\00\F0`0\D8\FF\00\00\E0`0\D8\FF\00\00\87\F0\96|\00\00\00\00\00\00\80\C0\00\00\00\00\00\90-Ͼz\FF<\A7f_\BD\00\00\FF\FF\FF\FF\00\00\00\00\00 *_\BD\00\00\D0++_\BD\00\00\C0\B4\88_\BD\00\00 <\00\00\00\00\00\C0\B4\88_\BD\00\00\B0b0\D8\FF\00\00\FD\A6=_\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\C0\E1?\D8\FF\00\00\00\00\00\00 \00\00\00|6g_\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00c0\D8\FF\00\00\00\00\00\00\00\00\00&\B0be\00\00\00\00*?g_\BD\00\00\00\00\00\00\00\00\00\00&\00\00\00\00\00\00\00\C0\8A\95\00\00\00\00`\E1?\D8\FF\00\00\00\00\00\BD\00\00\00\00\00\00\00\00\00\00`\E1?\D8\FF\00\00|6g_\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00pc0\D8\FF\00\00\00\00\00\00\00\00\00.N=\F6\00\00\00\00\80>g_\BD\00\00\00\00\00\00\00\00\00\00.\00\00\00\00\00\00\008\F5\D8\00\00\00\00]*_\BD\00\00[*_\BD\00\00\00\00\00\00\00\00\00]*_\BD\00\00\C8c0\D8\FF\00\00\A0c0\D8\FF\00\00 !+_\BD\00\00\80\EB\88_\BD\00\00\F8\E7\88_\BD\00\00\00\00\00\00\00\00\00\00\BA\88_\BD\00\00\C0\B4\88_\BD\00\00\D4@\00\00\00\00\00\F0,+_\BD\00\00\80@\00\00\00\00\00\00\00\00\00\00\00\00+\00\00\00\00\00h\E2\88_\BD\00\008d0\D8\FF\00\00d0\D8\FF\00\00\BA\88_\BD\00\00\C0\E5\88_\BD\00\00h\E2\88_\BD\00\00\00\00\00\00\00\00\00\A9Ag_\BD\00\00\00\00\00\00\00\00\00\00\BA\88_\BD\00\00\00\00\00\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00h\E2\88_\BD\00\00\D8\E1?\D8\FF\00\00\F8\E7\88_\BD\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\C0\E5\88_\BD\00\00pc0\D8\FF\00\00`c0\D8\FF\00\00.N=\F6\00\00\00\00v\00\00\00\00\00\00\C2\00\00\00\00\00\00\00\D4@\00\00\00\00\00\F

公平地说,在终端中,它似乎确实“类似于”HTTP 请求,但是,我不知道失败的原因:(

感谢任何帮助...

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

#define BUFLEN 1024
#define PORT 8080

/*
* Server.c
*
* Accept connections from a client
*
*/

int main() {
/* Useful Declarations */
int fd, backlog, connfd;
char buf[BUFLEN];
struct sockaddr_in addr, cliaddr;
socklen_t cliaddrlen;
ssize_t rcount, i;

/* IPv4 Declarations */
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);

/* Create socket or return error */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fputs("Unable to create a socket\n", stderr);
return -1;
}

/* Bind socket to port or return error */
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
fputs("Unable to bind socket to port\n", stderr);
return -1;
}

backlog = 1;

/* Start listening to port or return error */
if (listen(fd, backlog) == -1) {
fputs("Unable to listen to port\n", stderr);
close(fd);
return -1;
}

cliaddrlen = sizeof(cliaddr);

/* Await for connection, or return error */
if ((connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen)) == -1) {
fputs("Unable to accept connection\n", stderr);
close(fd);
return -1;
}

/* Read up to 1500 bytes from socket or error*/
if ((rcount = read(fd, buf, BUFLEN)) == -1) {
fputs("Error occurred while reading from socket\n", stderr);
printf("Error Number: %d\n", errno);

for (i = 0; i < BUFLEN; i++)
printf("%c", buf[i]);

return -1;
}

close(fd);
fputs("Socket closed\n", stdout);
return 0;
}

我知道只有当客户端过早关闭连接时才会打印此内容,但稍后当我到达它时我可以跨过那座桥:)

最佳答案

三件事:

  • 您应该使用 connfd,而不是 fd 进行读取 - fd 是原始监听套接字。
  • 打印缓冲区时,使用 rcount 查看获得的字节数。
  • 这也意味着只有在 rcount > 0 时打印缓冲区才有意义。

关于c - C 套接字中从浏览器接收到的非 ASCII 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21861224/

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