gpt4 book ai didi

C套接字Web服务器不在浏览器中显示html,仅发送HTTP响应

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

我对 C 语言相对较新,并尝试使用教授给我的一些代码,然后从那时起对其进行修改,以帮助我完成服务器项目。我不知道如何真正能够在 lynx 中看到 HTML,我只是得到 HTTP 响应。 (我可以 curl 它并查看我要发送的内容,但浏览器不会加载 HTML 正文)我一直在检查我能想到的所有内容,但此时我必须承认我对 HTTP 了解不够响应,并可以在正确的方向上迈出一步,以确定我在哪里走出了道路。

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define BUFFER_SIZE 9999
#define HTTP_METHOD "HTTP/1.1 "
#define HTTP__OK "200 OK\r\n"
#define HTTP__NOT_FOUND "404 Not Found\r\n"
#define SERVER_NAME "Server: ECE435\r\n"

/* Default port to listen on */
#define DEFAULT_PORT 8080 //modify port to listen on 8080

int main(int argc, char **argv) {

int socket_fd,new_socket_fd;
struct sockaddr_in server_addr, client_addr;
int port=DEFAULT_PORT;
int n;
socklen_t client_len;
char buffer[BUFFER_SIZE];

printf("Starting server on port %d\n",port);

/* Open a socket to listen on */
/* AF_INET means an IPv4 connection */
/* SOCK_STREAM means reliable two-way connection (TCP) */
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd<0) {
fprintf(stderr,"Error opening socket! %s\n",
strerror(errno));
exit(1);
}

/* Set up the server address to listen on */
/* The memset stes the address to 0.0.0.0 which means */
/* listen on any interface. */
memset(&server_addr,0,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;

/* Convert the port we want to network byte order */
server_addr.sin_port=htons(port);

/* Bind to the port */
if (bind(socket_fd, (struct sockaddr *) &server_addr,
sizeof(server_addr)) <0) {
fprintf(stderr,"Error binding! %s\n", strerror(errno));
fprintf(stderr,"Probably in time wait, have to wait 60s if you ^C to close\n");
exit(1);
}

/* Tell the server we want to listen on the port */
/* Second argument is backlog, how many pending connections can */
/* build up */
listen(socket_fd,5);


wait_for_connection:


/* Call accept to create a new file descriptor for an incoming */
/* connection. It takes the oldest one off the queue */
/* We're blocking so it waits here until a connection happens */
client_len=sizeof(client_addr);
new_socket_fd = accept(socket_fd,
(struct sockaddr *)&client_addr,&client_len);
if (new_socket_fd<0) {
fprintf(stderr,"Error accepting! %s\n",strerror(errno));
exit(1);
}

while(1){
/* Someone connected! Let's try to read BUFFER_SIZE-1 bytes */
memset( buffer, 0, BUFFER_SIZE );
n = read( new_socket_fd, buffer, ( BUFFER_SIZE-1 ) );
if (n==0){
fprintf( stderr, "Connection to client lost\n\n" );
break;
}
else if( n < 0 ){
fprintf(stderr,"Error reading from socket %s\n",
strerror(errno));
}

/* Print the message we received */
printf("Message received: %s\n" ,buffer);


const char *PATTERN1 = "GET /"; //first cut to make on buffer
const char *PATTERN2 = " HTTP"; //second cut to make on buffer
char *target = NULL; //variable to hold the slice we're taking
char *start, *end; //defining variables to hold start and end positions
if ( start = strstr( buffer, PATTERN1 ) ){ //code to grab a slice of buffer
start += strlen( PATTERN1 );

if ( end = strstr( start, PATTERN2 ) ){
target = ( char * )malloc( end - start + 1 );
memcpy( target, start, end - start );
target[end - start] = '\0';
}
}
if ( target ) printf( "Client requested: %s\n", target ); //code is working to this point. I can tell what file to get.

time_t rawtime;
struct tm info;
time( &rawtime );
struct tm * timeinfo;
char timestamp[100];
time_t now = time(0);
struct tm tm = *gmtime(&now);
strftime(timestamp, sizeof( timestamp ) , "%a, %d %b %Y %H:%M:%S %Z", &tm);
//printf("Time is: [%s]\n", timestamp);

struct stat file_info; //define statistics structure for file info
stat( target, &file_info ); //initiate file_info as the stat structure for target


char send_client[9999];


sprintf( send_client, "HTTP/1.0 %s%s\r\nServer: ECE435\r\nLast-Modified: Fri, 08 Sep 2017 04:31:47 GMT\r\nContent-Length: 85\r\nContent-Type: text/html\r\n\r\n", HTTP__OK, timestamp );

char file_path[256]; //limited to 256 char for now
sprintf( file_path, "./%s", target); //this is how you can combine char arrays: puts "./" + 'target' into 'file_path'

//int fd;

//printf( "%ld\r\n" , file_info.st_size ); //this should print the File Size


char source[BUFFER_SIZE + 1];
FILE *fp = fopen( file_path, "r");
if (fp != NULL) {
size_t newLen = fread(source, sizeof(char), BUFFER_SIZE, fp);
if (newLen == 0) {
fputs("Error reading file", stderr);
} else {
source[newLen] = '\0'; /* Just to be safe. */
}

fclose(fp);
}
strcat( send_client, source);


/* Send a response */
printf( "\r\n%s\r\n" , send_client ); //print response before sending
n = write( new_socket_fd, send_client , strlen(send_client) ) ;
if( n < 0 ){
fprintf( stderr, "Error writing. %s\n", strerror(errno));
}

}

close(new_socket_fd);

printf("Done connection, go back and wait for another\n\n");

goto wait_for_connection;

/* Try to avoid TIME_WAIT */
// sleep(1);

/* Close the sockets */
close(socket_fd);

return 0;
}

最佳答案

    memset( buffer, 0, BUFFER_SIZE );
n = read( new_socket_fd, buffer, ( BUFFER_SIZE-1 ) );
if (n==0){
fprintf( stderr, "Connection to client lost\n\n" );
break;
}
else if( n < 0 ){
fprintf(stderr,"Error reading from socket %s\n",
strerror(errno));
}

/* Print the message we received */
printf("Message received: %s\n" ,buffer);

当您在 TCP 连接上调用 read 时,您不会收到消息。如果您想接收 HTTP 消息,则必须编写代码来执行此操作。这只是坏了。

sprintf( send_client, "HTTP/1.0 %s%s\r\nServer: ECE435\r\n"
"Last-Modified: Fri, 08 Sep 2017 04:31:47 GMT\r\n"
"Content-Length: 85\r\nContent-Type: text/html\r\n\r\n", HTTP__OK, timestamp );

除非文件恰好包含 85 个字节,否则发送“Content-Length” header 并不是一个特别好的主意。

您有一个 while(1) 循环,它似乎试图通过单个连接接收多条消息。但是没有代码可以确定您何时实际收到 HTTP 请求,因此这肯定行不通。

HTTP 协议(protocol)很复杂,编写代码来正确实现它需要仔细阅读标准并实现标准所需的所有内容。这段代码并没有做到这一点,如果它确实有效,那主要是靠运气。

您可能只需修改“Content-Length” header 以获得正确的长度即可。但你仍然违反了很多规则。例如,您可能很容易最终向单个请求发送多个响应,因为您无法确保 read 返回整个 HTTP 请求。

关于C套接字Web服务器不在浏览器中显示html,仅发送HTTP响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53234273/

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