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

实验四 Web服务器2

时间:2023-12-08 12:34:21浏览次数:31  
标签:Web socket request server client 实验 address 服务器 include

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

#define PORT 8080
#define MAXLINE 1024

void handle_client(int client_socket);

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_address, client_address;
    socklen_t client_address_len = sizeof(client_address);

    // Create socket
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket < 0) {
        perror("Error creating socket");
        exit(EXIT_FAILURE);
    }

    // Set up server address
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(PORT);

    // Bind the socket
    if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
        perror("Error binding socket");
        exit(EXIT_FAILURE);
    }

    // Listen for incoming connections
    if (listen(server_socket, 10) < 0) {
        perror("Error listening for connections");
        exit(EXIT_FAILURE);
    }

    printf("Server listening on port %d...\n", PORT);

    while (1) {
        // Accept a connection
        client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_len);
        if (client_socket < 0) {
            perror("Error accepting connection");
            exit(EXIT_FAILURE);
        }

        // Handle the client request in a separate function
        handle_client(client_socket);
    }

    close(server_socket);
    return 0;
}

void handle_client(int client_socket) {
    char buffer[MAXLINE];
    ssize_t n;

    // Receive the request from the client
    n = recv(client_socket, buffer, sizeof(buffer), 0);
    if (n < 0) {
        perror("Error reading from socket");
        exit(EXIT_FAILURE);
    }

    // Print the received request
    printf("Received request:\n%s\n", buffer);

    // Simple response for GET request
    const char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>";
    
    // Send the response back to the client
    n = send(client_socket, response, strlen(response), 0);
    if (n < 0) {
        perror("Error writing to socket");
        exit(EXIT_FAILURE);
    }

    // Close the client socket
    close(client_socket);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

#define PORT 8080
#define MAXLINE 1024

int main() {
    int client_socket;
    struct sockaddr_in server_address;
    char request[MAXLINE];
    ssize_t n;

    // Create socket
    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket < 0) {
        perror("Error creating socket");
        exit(EXIT_FAILURE);
    }

    // Set up server address
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(PORT);
    inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr);

    // Connect to the server
    if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
        perror("Error connecting to server");
        exit(EXIT_FAILURE);
    }

    // Create a simple GET request
    const char *get_request = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n";

    // Send the GET request to the server
    n = send(client_socket, get_request, strlen(get_request), 0);
    if (n < 0) {
        perror("Error writing to socket");
        exit(EXIT_FAILURE);
    }

    // Receive and print the response from the server
    while ((n = recv(client_socket, request, sizeof(request), 0)) > 0) {
        write(STDOUT_FILENO, request, n);
    }

    // Close the socket
    close(client_socket);

    return 0;
}

 

标签:Web,socket,request,server,client,实验,address,服务器,include
From: https://www.cnblogs.com/liTCabcAbc/p/17884904.html

相关文章

  • 实验四 Web服务器2
    一、任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务器部署到华为云服务器,浏览器用本机的把服务器部署到试验箱。(加分项)二、实验过程1.运行截图:......
  • 20211314王艺达 实验四 2
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务器部署到华为云服务器,浏览器用本机的把服务器部署到试验箱。(加分项)具体实现代码:copy.c:/*copy.c:**Copyright......
  • 2023-2024-1 20211327 实验四 Web服务器2
    实验四Web服务器2Web服务器的客户端服务器web_server.c#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<arpa/inet.h>#definePORT8080#defineBUFFER_SIZE1024voidhandle_client(intclient_socket){......
  • web服务器-socket编程
    客户端#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<netinet/ip.h>#include<arpa/inet.h>#include<unistd.h>#inc......
  • ctfshow-web入门-爆破wp
    Web21:​ 进入主页为登录框,随便输入信息用burp抓包,发现Authorization认证使用Base64加密传输,解密发现为刚才输入的信息右键发送至Intruder进行爆破,使用题目给出的字典进行爆破并添加变量添加前缀使用户名固定并用Base64加密传输,记得取消勾选url-encode,不然会转义Base64的"="......
  • 实验四 Web服务器1-socket编程
    一、代码#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.h>#include<stdio.h>#de......
  • 实验四 Web服务器2
    实验四Web服务器2基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务器部署到华为云服务器,浏览器用本机的把服务器部署到试验箱。(加分项)1.webserver代码//web_server.c#inc......
  • 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......