用C++帮我写一个程序,要求用client-server实现多个用户的群组交流
时间: 2024-05-01 19:21:59 浏览: 108
物品库存管理系统,一个用C++写的小程序
5星 · 资源好评率100%
好的,以下是一个简单的示例程序,实现了用 client-server 架构实现多个用户的群组交流:
1. 服务器端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8888
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int main() {
int server_fd, client_fds[MAX_CLIENTS], max_fd, activity, i, valread, sd;
struct sockaddr_in address;
char buffer[BUFFER_SIZE];
fd_set readfds;
// 初始化客户端文件描述符数组
for (i = 0; i < MAX_CLIENTS; i++) {
client_fds[i] = 0;
}
// 创建服务器套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器套接字属性
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
// 绑定服务器套接字到指定端口
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d...\n", PORT);
// 等待连接请求并处理
while (1) {
// 将所有客户端文件描述符添加到 readfds 集合中
FD_ZERO(&readfds);
FD_SET(server_fd, &readfds);
max_fd = server_fd;
for (i = 0; i < MAX_CLIENTS; i++) {
sd = client_fds[i];
if (sd > 0) {
FD_SET(sd, &readfds);
}
if (sd > max_fd) {
max_fd = sd;
}
}
// 等待 I/O 事件发生
activity = select(max_fd + 1, &readfds, NULL, NULL, NULL);
if (activity < 0) {
perror("select failed");
exit(EXIT_FAILURE);
}
// 处理连接请求
if (FD_ISSET(server_fd, &readfds)) {
int new_socket;
struct sockaddr_in client_address;
socklen_t client_size = sizeof(client_address);
if ((new_socket = accept(server_fd, (struct sockaddr *)&client_address, &client_size)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("New connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
// 将新创建的套接字添加到客户端文件描述符数组中
for (i = 0; i < MAX_CLIENTS; i++) {
if (client_fds[i] == 0) {
client_fds[i] = new_socket;
break;
}
}
}
// 处理客户端消息
for (i = 0; i < MAX_CLIENTS; i++) {
sd = client_fds[i];
if (FD_ISSET(sd, &readfds)) {
if ((valread = read(sd, buffer, BUFFER_SIZE)) == 0) {
// 客户端断开连接
printf("Client disconnected\n");
close(sd);
client_fds[i] = 0;
} else {
// 广播消息给所有客户端
for (i = 0; i < MAX_CLIENTS; i++) {
if (client_fds[i] != 0 && client_fds[i] != sd) {
send(client_fds[i], buffer, valread, 0);
}
}
}
}
}
}
return 0;
}
```
2. 客户端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define SERVER_ADDRESS "127.0.0.1"
#define PORT 8888
#define BUFFER_SIZE 1024
int main() {
int sockfd;
struct sockaddr_in server_address;
char buffer[BUFFER_SIZE];
// 创建客户端套接字
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_ADDRESS, &server_address.sin_addr) <= 0) {
perror("inet_pton failed");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
printf("Connected to server\n");
// 发送消息给服务器
while (1) {
printf("> ");
fgets(buffer, BUFFER_SIZE, stdin);
send(sockfd, buffer, strlen(buffer), 0);
}
return 0;
}
```
以上是一个简单的示例程序,仅供参考。实际应用中,还需要考虑如何处理异常情况,如连接断开、客户端超时等。
阅读全文