首页 > 其他分享 >socket测试

socket测试

时间:2023-11-29 10:47:08浏览次数:32  
标签:addr buffer server client 测试 include socket

echo_server

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

#define PORT 7406

void handle_client(int client_socket, char *student_id) {
    char buffer[1024];
    ssize_t bytes_received;

    // Receive data from the client
    bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
    if (bytes_received < 0) {
        perror("Error receiving data from client");
        close(client_socket);
        exit(EXIT_FAILURE);
    }

    buffer[bytes_received] = '\0';

    // Check if the received data follows the specified format
    if (sscanf(buffer, "%*[^:]:%*[^:]:%*d:%*d:%s", student_id) != 1) {
        fprintf(stderr, "Error: Invalid data format received from client.\n");
        close(client_socket);
        exit(EXIT_FAILURE);
    }

    // Reverse the received data
    int len = strlen(buffer);
    for (int i = 0; i < len / 2; i++) {
        char temp = buffer[i];
        buffer[i] = buffer[len - i - 1];
        buffer[len - i - 1] = temp;
    }

    // Send the reversed data back to the client
    ssize_t bytes_sent = send(client_socket, buffer, strlen(buffer), 0);
    if (bytes_sent < 0) {
        perror("Error sending data to client");
    }

    // Close the client socket
    close(client_socket);
    exit(EXIT_SUCCESS);
}

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    char student_id[9];

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

    // Configure server address
    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);

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

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

    while (1) {
        // Accept connection from client
        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
        if (client_socket < 0) {
            perror("Error accepting connection");
            continue;
        }

        // Fork a new process to handle the client
        pid_t pid = fork();
        if (pid < 0) {
            perror("Error forking process");
            close(client_socket);
            continue;
        } else if (pid == 0) {
            // Child process handles the client
            close(server_socket);
            handle_client(client_socket, student_id);
        } else {
            // Parent process continues to accept new connections
            close(client_socket);
        }
    }

    // Close the server socket
    close(server_socket);

    return 0;
}

echo_client



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

#define PORT 7406

int main() {
    int client_socket;
    struct sockaddr_in server_addr;
    char student_id[9] = "YOUR_ID"; // Replace with your 8-digit student ID

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

    // Configure server address
    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"); // Assuming server is running locally

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

    // Send data to the server
    char message[1024];
    snprintf(message, sizeof(message), "客户端实现者%s:消息", student_id);

    ssize_t bytes_sent = send(client_socket, message, strlen(message), 0);
    if (bytes_sent < 0) {
        perror("Error sending data to server");
    }

    // Receive and print the response from the server
    char buffer[1024];
    ssize_t bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
    if (bytes_received < 0) {
        perror("Error receiving data from server");
    } else {
        buffer[bytes_received] = '\0';
        printf("Server Response:\n%s\n", buffer);
    }

    // Close the client socket
    close(client_socket);

    return 0;
}


标签:addr,buffer,server,client,测试,include,socket
From: https://www.cnblogs.com/zsy1748774883/p/17863978.html

相关文章

  • socket测试
    client.c:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/socket.h>#include<arpa/inet.h>#defineSERVER_IP"127.0.0.1"#definePORT7312#defineMAX_MSG_SIZE102......
  • 2023-2024-1 20211327 myxxd(课上测试)
    myxxd(课上测试)学***d的使用,提交至少3个应用截图xxd的主要功能是什么?需要使用什么系统调用来实现?写出你的推导过程,命令xxd主要用于查看文件的十六进制表示,并提供了一些额外的功能,如生成C语言风格的数组表示。它的主要功能包括:查看文件的十六进制表示:显示文件内容的十......
  • 课上测试2 myxxd
    1.学***d的使用,提交至少3个应用截图2.xxd的主要功能是什么?需要使用什么系统调用来实现?写出你的推导过程,命令xxd的主要功能是以十六进制格式显示文件的内容,并提供将文件转换为十六进制格式或从十六进制格式转换回二进制格式的能力。它还可以用于编辑文件的十六进制内容。在推导......
  • 基于FPGA的图像sobel锐化实现,包括tb测试文件和MATLAB辅助验证
    1.算法运行效果图预览   将FPGA的仿真结果导入到matlab显示图像效果   2.算法运行软件版本MATLAB2022a,vivado2019.2 3.算法理论概述       基于FPGA的图像Sobel锐化实现,是一种通过使用Sobel算子对图像进行边缘检测,从而提高图像清晰度的方法。Sobel......
  • springboot 自定义响应体大小测试接口
    @ResponseBody@RequestMapping("/def/response/body/service")publicStringBuilderdefResponseBodyService(@RequestParam(name="count")Integercount,HttpServletRequestHttpRequest)throwsInterruptedException{  StringbaseStr="0......
  • 关键字 开发-06 封装yaml文件直接生成测试用例
    前言前面几个章节,我们主要是如何通过yaml文件的数据自动转换成测试用例,并按照关键字去执行,如下是之前我们通过参数化的形式手动执行用例。fromutils.runimportRunByKey#获取文件路径file_path=Path(__file__).parent.joinpath('data','login.yml')@pytest.mark.para......
  • 【Python进阶】第7篇:TCP和socket客户端程序开发。总结md文档集合(已分享,附代码)
    本文从14大模块展示了python高级用的应用。分别有Linux命令,多任务编程、网络编程、Http协议和静态Web编程、html+css、JavaScript、jQuery、MySql数据库的各种用法、python的闭包和装饰器、mini-web框架、正则表达式等相关文章的详细讲述。全套笔记和代码自取地址:请移步这里感......
  • Realtek蓝牙Android10.0移植结束后的基本测试和常见问题分析
    基本测试主要包括配置检查和BT测试两大部分配置检查:为了进一步确保porting没有问题,在测试之前先确认fw以及config文件是否存在。adbshell到测试平台的根目录,检查测试平台的vendor/firmware/目录中rtlxxxx_fw以及rtlxxxx_config文件是否存在(xxxx为BTChip型号)......
  • 海外时区测试注意事项
    测试有时会遇到时区测试,如设备或系统在不同时区进行测试,或跨时区访问设备,检索或者时间显示等等问题,这之间需要一个时区转换测试,实际情况下,针对不同设备或者系统的需求,也需要根据设计进行测试,不能一概而论。这里记录一下时区遇到的一些不同概念,测试点及相关功能后续想到了再更新。......
  • 软件测试/人工智能|基于录制的接口测试用例自动生成技术探究
    导言在现代软件开发中,接口测试是确保系统功能和性能的关键步骤。然而,手动编写接口测试用例往往耗费大量时间和资源。基于录制的接口测试用例自动生成技术通过简化这个流程,显著提高了测试效率和准确性。录制接口测试用例自动生成技术简介录制接口测试用例自动生成技术允许开发......