在C語言中,使用socket實現消息隊列需要涉及到服務器端和客戶端的交互。這里給出一個簡單的基于TCP協議的消息隊列實現示例:
#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;
}
}
}
#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。當客戶端發送消息時,服務器會將消息發送給所有其他客戶端。客戶端代碼中,用戶可以輸入消息并發送給服務器,然后服務器會將消息廣播給所有連接的客戶端。