636363

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define BUFFER_SIZE 1024

#define EXIT_MESSAGE "exit"

void error(const char *message) {

    perror(message);

    exit(1);

}

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

    if (argc != 2) {

        fprintf(stderr, "Usage: %s <port>\n", argv[0]);

        exit(1);

    }

    int server_socket, client_socket;

    char buffer[BUFFER_SIZE];

    struct sockaddr_in server_addr, client_addr;

    socklen_t client_addr_len;

    // 创建服务器套接字

    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    if (server_socket < 0) {

        error("Error opening socket");

    }

    // 设置服务器地址结构

    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(atoi(argv[1]));

    // 绑定服务器套接字到指定端口

    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {

        error("Error binding socket");

    }

    // 监听连接请求

    if (listen(server_socket, 1) < 0) {

        error("Error listening");

    }

    printf("Waiting for a connection...\n");

    // 接受客户端连接

    client_addr_len = sizeof(client_addr);

    client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);

    if (client_socket < 0) {

        error("Error accepting connection");

    }

    printf("Client connected\n");

    // 接收和发送消息,直到收到退出消息

    while (1) {

        // 接收客户端消息

        memset(buffer, 0, sizeof(buffer));

        if (recv(client_socket, buffer, sizeof(buffer), 0) < 0) {

            error("Error receiving data");

        }

        printf("Client: %s\n", buffer);

        // 去除换行符

        buffer[strcspn(buffer, "\n")] = '\0';

        // 检查退出消息

        if (strcmp(buffer, EXIT_MESSAGE) == 0) {

            printf("Client exited\n");

            break;

        }

        // 发送消息给客户端

        printf("Server: ");

        fgets(buffer, sizeof(buffer), stdin);

        // 去除换行符

        buffer[strcspn(buffer, "\n")] = '\0';

        // 检查退出消息

        if (strcmp(buffer, EXIT_MESSAGE) == 0) {

            if (send(client_socket, buffer, strlen(buffer), 0) < 0) {

                error("Error sending data");

            }

            printf("You exited\n");

            break;

        }

        if (send(client_socket, buffer, strlen(buffer), 0) < 0) {

            error("Error sending data");

        }

    }

    // 关闭套接字

    close(client_socket);

    close(server_socket);

    return 0;

}

优化下面代码.bg { width: 100%; height: 100vh; background-image: url('../../assets/img/info-bg.png'); background-size: 100% 100%; background-repeat: no-repeat; position: relative; font-family: AlibabaPuHuiTiR; .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #ffffff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #ffffff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #ffffff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background-image: url('../../assets/img/howo.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-zk-img { width: 56%; height: 90%; background-image: url('../../assets/img/heavyT.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-gj-img { width: 82%; height: 90%; background-image: url('../../assets/img/transit.png'); background-size: 100% 100%; background-repeat: no-repeat; } .car-online { margin-bottom: 50px; } } .day-data { display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #ffffff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; }
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值