我设法创建了一个接受来自Web浏览器的连接的C服务器,但是,我从套接字收到的HTTP请求是随机字节,我似乎无法转换为ASCII值,如下所示: / p>
\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;
}
我知道这只会在客户端提前关闭连接时打印,但是当我来到它时我可以越过那座桥:)
答案 0 :(得分:1)
三件事:
connfd
,而不是fd
来读取 - fd
是原始的侦听套接字。rcount
查看已获得的字节数。rcount > 0
。