Loading

Linux下TCP/IP编程--TCP实战

之前尝试过windows下的简单TCP客户端服务器编写,这次尝试下一下Linux环境下的TCP

客户端代码

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

struct CustomMessage {
    char university[4]; // "AHU\0"
    char major[32]; // 专业
    char data[256]; // 消息数据
};

int main() {
    int client_socket;
    struct sockaddr_in server_address;

    // 创建套接字
    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }

    // 设置服务器地址
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(8888);
    if (inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr) <= 0) {
        perror("Invalid IP address");
        close(client_socket);
        exit(EXIT_FAILURE);
    }

    // 连接服务器
    if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
        perror("Connection failed");
        close(client_socket);
        exit(EXIT_FAILURE);
    }

    while (1) {
        // 获取用户输入的消息
        printf("请输入要发送的消息 (输入 exit 结束): ");
        struct CustomMessage message_to_send;
        fgets(message_to_send.data, sizeof(message_to_send.data), stdin);
        message_to_send.data[strcspn(message_to_send.data, "\n")] = '\0'; // 移除换行符
        strcpy(message_to_send.university, "AHU"); // 设置学校为"AHU"
        strcpy(message_to_send.major, "CS"); // 设置专业为"CS"

        // 发送自定义消息
        int bytes_sent = send(client_socket, (const char*)&message_to_send, sizeof(struct CustomMessage), 0);
        if (bytes_sent < 0) {
            perror("Send failed");
            close(client_socket);
            exit(EXIT_FAILURE);
        }

        // 如果用户输入 "exit",结束循环
        if (strcmp(message_to_send.data, "exit") == 0) {
            break;
        }
    }

    // 关闭客户端套接字
    close(client_socket);

    return 0;
}

服务器端代码

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

#define PORT 8888
#define MAX_PENDING_CONNECTIONS 5

struct CustomMessage {
    char university[4];
    char major[32];
    char data[256];
};

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

    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = INADDR_ANY;
    server_address.sin_port = htons(PORT);

    if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
        perror("Bind failed");
        close(server_socket);
        exit(EXIT_FAILURE);
    }

    if (listen(server_socket, MAX_PENDING_CONNECTIONS) < 0) {
        perror("Listen failed");
        close(server_socket);
        exit(EXIT_FAILURE);
    }

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

    while (1) {
        client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_length);
        if (client_socket < 0) {
            perror("Accept failed");
            close(server_socket);
            exit(EXIT_FAILURE);
        }

        while (1) {
            struct CustomMessage received_message;
            ssize_t bytes_received = recv(client_socket, (char*)&received_message, sizeof(struct CustomMessage), 0);
            if (bytes_received > 0) {
                printf("Received Message:\n");
                printf("University: %s\n", received_message.university);
                printf("Major: %s\n", received_message.major);
                printf("Data: %s\n", received_message.data);

                if (strcmp(received_message.data, "exit") == 0) {
                    close(client_socket);
                    break;
                }
            }
            else if (bytes_received == 0) {
                printf("Client disconnected\n");
                close(client_socket);
                break;
            }
            else {
                perror("Receive failed");
                close(client_socket);
                break;
            }
        }
    }

    close(server_socket);

    return 0;
}

运行结果

image

posted @ 2024-03-29 10:20  go__Ahead  阅读(78)  评论(0编辑  收藏  举报