首页 > 其他分享 >实验四 Web服务器2

实验四 Web服务器2

时间:2023-12-08 12:00:46浏览次数:42  
标签:Web addr server client 实验 服务器 sizeof include socket

实验四 Web服务器2

基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现:

  1. Web服务器的客户端服务器,提交程序运行截图
  2. 实现GET即可,请求,响应要符合HTTP协议规范
  3. 服务器部署到华为云服务器,浏览器用本机的
  4. 把服务器部署到试验箱。(加分项)

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

相关文章

  • HTTP长连接和Websocket的区别
    一、HTTP和WebSocket都是基于TCP协议TCP建立每个连接都需要三次握手。二、HTTP短连接HTTP1.0(短链接)就是浏览器和服务器每进行一次HTTP操作,就建立一次TCP连接,数据传输完成后,TCP连接就随之关闭,即:客户端与服务端的连接均必须被切断。三、HTTP长连接HTTP1.1(长连接)中使用......
  • 实验四 Web服务器1-socket编程
    实验四Web服务器1-socket编程基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:time服务器的客户端服务器,提交程序运行截图echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid你的学号姓名echo:”返回给客户端服务器部......
  • 2023-2024-1 20211327 实验四 Web服务器1-socket编程
    实验四Web服务器1-socket编程time服务器的客户端服务器time_server.c#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/socket.h>#include<sys/types.h>#include<s......
  • 实验四-2 Web
    代码#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<netdb.h>#include<arpa/inet.h>#includ......
  • Web服务器-socket编程
    代码#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<netdb.h>#include<arpa/inet.h>#includ......
  • 第4章 带你快速入门WebRTC技术
    4-3屏幕共享项目-用golang搭建静态资源服务这段代码是一个使用Go语言编写的简单的HTTP和HTTPS服务器。下面是对代码中涉及的知识点和语法格式的详细解释packagemainimport( "fmt" "net/http")packagemain:这行代码指定了当前文件所属的包是main,表示这是一个可执行程......
  • 实验四 Web服务器2
    一、任务基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务器部署到华为云服务器,浏览器用本机的把服务器部署到试验箱。(加分项)......
  • 20211314王艺达 实验四 1
    1.time服务器的客户端服务器,提交程序运行截图查看ipcsapp.c:#include<netinet/in.h>#include<arpa/inet.h>#include<netdb.h>#include<sys/types.h>#include<sys/socket.h>#include<stdlib.h>#include<string.h>#include<errno.......
  • Windows服务器,通过Nginx部署VUE+Django前后端分离项目
    目录基本说明安装Nginx部署VUE前端部署Django后端Djangoadmin静态文件(CSS,JS等)丢失的问题1.基本说明本文介绍了在windows服务器下,通过Nginx部署VUE+Django前后端分离项目。本项目前端运行在80端口,服务器端运行在8000端口。因此本项目使用Django的......
  • 服务器节点建立互信
    1、节点之间建立互信可以免密登录和访问执行ssh-keygen-b1024-trsa cd/root/.ssh ls#可以看到刚生成的公钥和私钥若对应节点下无/root/.ssh目录执行ssh-keygen-b1024-trs即可 将公钥修改名称后传输至对应节点下的/root/.ssh目录下放置覆盖原有的公......