c语言

C语言socket怎样实现消息队列

小樊
83
2024-10-09 00:29:33
栏目: 编程语言

在C语言中,使用socket实现消息队列需要涉及到服务器端和客户端的交互。这里给出一个简单的基于TCP协议的消息队列实现示例:

  1. 服务器端代码(server.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 8080
#define BUFFER_SIZE 1024
#define MAX_CLIENTS 5

int clients[MAX_CLIENTS];
int client_count = 0;

void handle_client(int client_socket);
void send_message(int client_socket, const char *message);

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_size;

    // 创建socket
    if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    // 配置服务器地址
    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);

    // 绑定socket
    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("bind");
        exit(1);
    }

    // 监听连接
    if (listen(server_socket, 5) == -1) {
        perror("listen");
        exit(1);
    }

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

    while (1) {
        // 接受客户端连接
        client_addr_size = sizeof(client_addr);
        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_size);

        // 处理客户端连接
        if (client_socket == -1) {
            perror("accept");
            continue;
        }

        // 将客户端socket添加到客户端数组
        for (int i = 0; i < client_count; i++) {
            if (clients[i] == client_socket) {
                printf("Client %d already connected\n", client_socket);
                close(client_socket);
                break;
            }
        }

        if (client_count < MAX_CLIENTS) {
            clients[client_count++] = client_socket;
            printf("Client %d connected\n", client_socket);
            handle_client(client_socket);
        } else {
            printf("Server is full, cannot accept new client %d\n", client_socket);
            close(client_socket);
        }
    }

    close(server_socket);
    return 0;
}

void handle_client(int client_socket) {
    char buffer[BUFFER_SIZE];
    int message_length;

    while (1) {
        // 接收客户端发送的消息
        message_length = recv(client_socket, buffer, BUFFER_SIZE, 0);

        if (message_length == -1) {
            perror("recv");
            break;
        } else if (message_length == 0) {
            printf("Client %d disconnected\n", client_socket);
            remove_client(client_socket);
            break;
        }

        // 打印接收到的消息
        buffer[message_length] = '\0';
        printf("Received from client %d: %s\n", client_socket, buffer);

        // 将消息发送给所有其他客户端
        send_message(client_socket, buffer);
    }

    close(client_socket);
}

void send_message(int sender_socket, const char *message) {
    for (int i = 0; i < client_count; i++) {
        if (i != sender_socket) {
            int result = send(clients[i], message, strlen(message), 0);
            if (result == -1) {
                perror("send");
            }
        }
    }
}

void remove_client(int client_socket) {
    for (int i = 0; i < client_count; i++) {
        if (clients[i] == client_socket) {
            clients[i] = clients[--client_count];
            break;
        }
    }
}
  1. 客户端代码(client.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define BUFFER_SIZE 1024

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

    // 创建socket
    if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    // 配置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_addr.sin_port = htons(PORT);

    // 连接服务器
    if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("connect");
        exit(1);
    }

    char message[BUFFER_SIZE];
    while (1) {
        // 从用户输入获取消息
        fgets(message, BUFFER_SIZE, stdin);
        message[strcspn(message, "\n")] = 0;

        // 发送消息给服务器
        if (strlen(message) > 0) {
            send(client_socket, message, strlen(message), 0);
        }
    }

    close(client_socket);
    return 0;
}

这个示例中,服务器端使用一个数组clients来存储所有已连接的客户端socket。当客户端发送消息时,服务器会将消息发送给所有其他客户端。客户端代码中,用户可以输入消息并发送给服务器,然后服务器会将消息广播给所有连接的客户端。

0
看了该问题的人还看了