用c语言写一个socket的多对多程序server端
时间: 2023-11-17 21:04:10 浏览: 78
好的,以下是一个简单的多对多服务器端程序的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#define BUF_SIZE 100
#define NAME_SIZE 20
#define MAX_CLIENTS 10
void error_handling(char *message);
void send_message(char *message, int len, int sender);
void remove_client(int client);
int clients[MAX_CLIENTS];
char name_arr[MAX_CLIENTS][NAME_SIZE];
char message[BUF_SIZE];
fd_set reads, copy_reads;
int main(int argc, char *argv[]) {
int serv_sock, client_sock;
struct sockaddr_in serv_addr, client_addr;
socklen_t client_addr_size;
int str_len, i, j, max_fd;
char name[NAME_SIZE];
struct timeval timeout;
int fd_num, fd;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == -1) {
error_handling("socket() error");
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if (bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
error_handling("bind() error");
}
if (listen(serv_sock, 5) == -1) {
error_handling("listen() error");
}
FD_ZERO(&reads);
FD_SET(serv_sock, &reads);
max_fd = serv_sock;
while (1) {
copy_reads = reads;
timeout.tv_sec = 5;
timeout.tv_usec = 5000;
fd_num = select(max_fd + 1, ©_reads, NULL, NULL, &timeout);
if (fd_num == -1) {
break;
}
if (fd_num == 0) {
continue;
}
for (fd = 0; fd < max_fd + 1; fd++) {
if (FD_ISSET(fd, ©_reads)) {
if (fd == serv_sock) {
client_addr_size = sizeof(client_addr);
client_sock = accept(serv_sock, (struct sockaddr *)&client_addr, &client_addr_size);
FD_SET(client_sock, &reads);
if (client_sock > max_fd) {
max_fd = client_sock;
}
printf("Connected client: %d\n", client_sock);
sprintf(name, "[%d]", client_sock);
strcpy(name_arr[client_sock], name);
clients[client_sock] = 1;
} else {
str_len = read(fd, message, BUF_SIZE);
if (str_len == 0) {
remove_client(fd);
FD_CLR(fd, &reads);
} else {
send_message(message, str_len, fd);
}
}
}
}
}
close(serv_sock);
return 0;
}
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
void send_message(char *message, int len, int sender) {
int i;
for (i = 0; i < max_fd + 1; i++) {
if (FD_ISSET(i, &reads)) {
if (i != sender && i != serv_sock) {
write(i, name_arr[sender], strlen(name_arr[sender]));
write(i, " ", 1);
write(i, message, len);
}
}
}
}
void remove_client(int client) {
int i;
close(client);
clients[client] = 0;
for (i = 0; i < MAX_CLIENTS; i++) {
if (clients[i] == 1) {
FD_SET(i, &reads);
}
}
printf("Removed client: %d\n", client);
}
```
这个程序使用`socket()`函数创建一个TCP套接字,然后使用`bind()`函数将其绑定到指定的端口上。然后使用`listen()`函数开始监听客户端连接请求。当有客户端连接时,使用`accept()`函数接受连接请求并将客户端套接字加入到`fd_set`集合中。使用`select()`函数监听所有连接的套接字,如果有套接字可读,使用`read()`函数读取客户端的消息并广播给其他客户端。如果客户端关闭连接,将其从`fd_set`集合中移除并关闭套接字。
请注意,这个程序只是一个简单的示例,没有进行错误处理和线程安全处理。如果需要在实际应用中使用,请进行必要的改进。
阅读全文