使用linux C,写一个可接收多客户端通讯的TCP服务器代码,使用select仕贤
时间: 2023-06-13 11:03:02 浏览: 62
好的,我明白了。以下是可以接收多个客户端连接的TCP服务器的示例代码,使用select函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.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 30
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, activity, client_socket[MAX_CLIENTS];
int max_sd, sd, max_clients = MAX_CLIENTS, opt = 1, i, valread;
struct sockaddr_in address;
fd_set read_fds;
char buffer[BUFFER_SIZE];
bool client_connected[max_clients];
// create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// set socket options
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
// setup the address struct
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// bind socket to address
if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// listen for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
// initialize all client sockets to 0
for (i = 0; i < max_clients; i++) {
client_socket[i] = 0;
client_connected[i] = false;
}
printf("Server listening on port %d \n", PORT);
while (true) {
// clear the socket set
FD_ZERO(&read_fds);
// add master socket to set
FD_SET(server_fd, &read_fds);
max_sd = server_fd;
// add child sockets to set
for (i = 0; i < max_clients; i++) {
// socket descriptor
sd = client_socket[i];
// if valid socket descriptor then add to read list
if (sd > 0) {
FD_SET(sd, &read_fds);
}
// highest file descriptor number, needed for select function
if (sd > max_sd) {
max_sd = sd;
}
}
// wait for an activity on one of the sockets
activity = select(max_sd + 1, &read_fds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
perror("select error");
}
// if something happened on the master socket, then it's an incoming connection
if (FD_ISSET(server_fd, &read_fds)) {
if ((new_socket = accept(server_fd, (struct sockaddr *) &address, (socklen_t *) &address)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("New connection: fd=%d, IP=%s, Port=%d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
// add new socket to array of sockets
for (i = 0; i < max_clients; i++) {
// if position is empty
if (client_socket[i] == 0) {
client_socket[i] = new_socket;
client_connected[i] = true;
printf("Added new socket to array of sockets: fd=%d, index=%d \n", new_socket, i);
break;
}
}
}
// else it's some IO operation on some other socket
for (i = 0; i < max_clients; i++) {
// socket descriptor
sd = client_socket[i];
if (FD_ISSET(sd, &read_fds)) {
// check if it was for closing, and also read the incoming message
memset(buffer, 0, BUFFER_SIZE);
valread = read(sd, buffer, BUFFER_SIZE);
if (valread <= 0) {
// some client disconnected
getpeername(sd, (struct sockaddr *) &address, (socklen_t *) &address);
printf("Client disconnected: fd=%d, IP=%s, Port=%d \n", sd, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
close(sd);
client_socket[i] = 0;
client_connected[i] = false;
} else {
// send message to everyone else
for (int j = 0; j < max_clients; j++) {
if (client_connected[j] && client_socket[j] != sd) {
send(client_socket[j], buffer, strlen(buffer), 0);
}
}
}
}
}
}
return 0;
}
```
这段代码中,我们创建了一个套接字,将其绑定到一个端口,然后开始监听传入的连接。我们使用一个无限循环来等待事件,在每个迭代中,我们使用select函数来检查可读的套接字。我们首先将主套接字放到读取集合中,将客户端套接字逐一添加到读取集合中。如果主套接字有可读的数据,说明有新的连接请求;我们通过accept函数接受连接,并为新客户端套接字分配一个数组位置。如果已连接的客户端套接字有可读的数据,我们读取数据并将其转发给所有其他已连接的客户端套接字。如果我们读到的数据长度为0,说明该客户端已断开连接,我们将其从数组中删除。对于这段代码,你可以自由地进行修改和定制,以满足你自己的需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)