实验四 Web服务器2
基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现:
- Web服务器的客户端服务器,提交程序运行截图
- 实现GET即可,请求,响应要符合HTTP协议规范
- 服务器部署到华为云服务器,浏览器用本机的
- 把服务器部署到试验箱。(加分项)
1.webserver代码
// web_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFSIZE 1024
void handle_request(int client_socket) {
FILE *file = fopen("index.html", "r");
if (file == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char buffer[BUFFSIZE];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
send(client_socket, buffer, bytes_read, 0);
}
fclose(file);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
if (listen(server_socket, 5) == -1) {
perror("Error listening on socket");
exit(EXIT_FAILURE);
}
printf("Web server is running...\n");
while (1) {
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
if (fork() == 0) { // child process
close(server_socket); // child doesn't need the listener
handle_request(client_socket);
exit(0);
} else { // parent process
close(client_socket); // parent doesn't need the client socket
}
}
close(server_socket);
return 0;
}
2.webclient代码
// web_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFSIZE 1024
void send_request(int client_socket) {
char request[] = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n";
send(client_socket, request, sizeof(request), 0);
}
void receive_response(int client_socket) {
char buffer[BUFFSIZE];
int bytes_received;
while ((bytes_received = recv(client_socket, buffer, sizeof(buffer), 0)) > 0) {
printf("%.*s", bytes_received, buffer);
}
}
int main() {
int client_socket;
struct sockaddr_in server_addr;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
exit(EXIT_FAILURE);
}
send_request(client_socket);
receive_response(client_socket);
close(client_socket);
return 0;
}
3.index.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Web Page</title>
</head>
<body>
<h1>Hello, 20211317lzt!</h1>
<p>This is a simple web page.</p>
</body>
</html>
标签:Web,addr,server,client,实验,服务器,sizeof,include,socket
From: https://www.cnblogs.com/lizhuotong/p/17884875.html