首页 > 编程语言 >实验四 Web服务器1-socket编程

实验四 Web服务器1-socket编程

时间:2023-12-08 11:57:31浏览次数:45  
标签:Web addr buffer 编程 server client include socket

实验四 Web服务器1-socket编程

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

  1. time服务器的客户端服务器,提交程序运行截图
  2. echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid 你的学号 姓名 echo :”返回给客户端
  3. 服务器部署到华为云服务器,客户端用Ubuntu虚拟机。
  4. 要用多线程或者多进程实现,至少连接两个客户端。
  5. 把服务器部署到试验箱。(加分项)

1。time服务器
服务器代码

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

#define PORT 8080
#define BUFFSIZE 1024

void handle_client(int client_socket) {
    char buffer[BUFFSIZE];
    time_t current_time;
    struct tm *time_info;

    time(&current_time);
    time_info = localtime(&current_time);
    snprintf(buffer, sizeof(buffer), "%s", asctime(time_info));

    send(client_socket, buffer, strlen(buffer), 0);

    close(client_socket);
}

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("Time 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_client(client_socket);
            exit(0);
        } else {  // parent process
            close(client_socket);  // parent doesn't need the client socket
        }
    }

    close(server_socket);

    return 0;
}

客户端代码

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

#define PORT 8080
#define BUFFSIZE 1024

int main() {
    int client_socket;
    struct sockaddr_in server_addr;
    char buffer[BUFFSIZE];

    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 = INADDR_ANY;

    if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("Error connecting to server");
        exit(EXIT_FAILURE);
    }

    recv(client_socket, buffer, sizeof(buffer), 0);
    printf("Server Time: %s\n", buffer);

    close(client_socket);

    return 0;
}


2.echo服务器

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

#define PORT 8081
#define BUFFSIZE 1024

void handle_client(int client_socket) {
    char buffer[BUFFSIZE];
    int bytes_received;

    bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
    if (bytes_received > 0) {
        // Process the received data (e.g., add server process ID, student ID, name, etc.)
        // and send it back to the client.
        snprintf(buffer, sizeof(buffer), "服务器进程pid %d 20211317lzt echo : %s", getpid(), buffer);
        send(client_socket, buffer, strlen(buffer), 0);
    }

    close(client_socket);
}

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("Echo 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_client(client_socket);
            exit(0);
        } else {  // parent process
            close(client_socket);  // parent doesn't need the client socket
        }
    }

    close(server_socket);

    return 0;
}

客户端代码

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

#define PORT 8081
#define BUFFSIZE 1024

int main() {
    int client_socket;
    struct sockaddr_in server_addr;
    char buffer[BUFFSIZE];

    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 = INADDR_ANY;

    if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("Error connecting to server");
        exit(EXIT_FAILURE);
    }

    // Send data to the server
    printf("Enter data to send to server: ");
    fgets(buffer, sizeof(buffer), stdin);
    send(client_socket, buffer, strlen(buffer), 0);

    // Receive and print the response from the server
    recv(client_socket, buffer, sizeof(buffer), 0);
    printf("Server Response: %s\n", buffer);

    close(client_socket);

    return 0;
}

标签:Web,addr,buffer,编程,server,client,include,socket
From: https://www.cnblogs.com/lizhuotong/p/17884845.html

相关文章

  • 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......
  • ubuntu 非 root用户 bind socket 80端口 失败
    在Ubuntu或其他类似的Linux系统中,非root用户在绑定低于1024的端口时会失败,这是出于安全考虑的一种行为。为了允许非root用户绑定低端口,您可以考虑以下几种方法:1.使用CAP_NET_BIND_SERVICE能力您可以使用setcap命令,为您的可执行文件分配CAP_NET_BIND_SERVICE能......
  • 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协议规范服务器部署到华为云服务器,浏览器用本机的把服务器部署到试验箱。(加分项)......
  • JavaWeb - Day03 - Ajax、前端工程化、Element
    01.Ajax-介绍Ajax概念:AsynchronousJavaScriptAndXML,异步的JavaScript和XML。作用:数据交换:通过Ajax可以给服务器发送请求,并获取服务器响应的数据。异步交互:可以在不重新加载整个页面的情况下,与服务器交换数据并更新部分网页的技术,如:搜索联想、用户名是否可用的校验等等......
  • 记录springboot的一次使用socketio的经历
    pom中加入依赖<dependency><groupId>com.corundumstudio.socketio</groupId><artifactId>netty-socketio</artifactId><version>2.0.6</version></dependency>......
  • 实验四 Web服务器2
    实验四Web服务器2基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服务器部署到华为云服务器,浏览器用本机的4.把服务器部署到试验箱。(加分项)web_server.c1#include......
  • 实验四 Web服务器2
    server.c#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#definePORT8080#defineBUFFER_SIZE1024intmain(){   int......