使用C语言写一个select函数的监听socket 接受demo
时间: 2023-08-02 16:06:20 浏览: 159
socket_select demo
下面是一个简单的使用select函数监听socket并接受数据的示例代码,其中使用了TCP协议:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int server_fd, max_fd, activity, i, valread, new_socket, sd;
struct sockaddr_in server_addr, client_addr;
char buffer[BUFFER_SIZE];
fd_set readfds;
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Set socket options
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
// Bind socket to address
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
// Accept incoming connections and add them to the set of file descriptors to monitor
int addrlen = sizeof(client_addr);
int client_sockets[MAX_CLIENTS] = {0};
for (i = 0; i < MAX_CLIENTS; ++i) {
new_socket = accept(server_fd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen);
if (new_socket < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("New connection, socket fd is %d, IP is : %s, port : %d\n", new_socket, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Add new socket to set of file descriptors to monitor
client_sockets[i] = new_socket;
}
// Main loop to monitor file descriptors for activity
while (1) {
// Clear the set of file descriptors to monitor
FD_ZERO(&readfds);
// Add server socket and client sockets to the set of file descriptors to monitor
FD_SET(server_fd, &readfds);
max_fd = server_fd;
for (i = 0; i < MAX_CLIENTS; ++i) {
sd = client_sockets[i];
if (sd > 0) {
FD_SET(sd, &readfds);
}
if (sd > max_fd) {
max_fd = sd;
}
}
// Wait for activity on any of the monitored file descriptors
activity = select(max_fd + 1, &readfds, NULL, NULL, NULL);
if (activity < 0) {
perror("select error");
exit(EXIT_FAILURE);
}
// If there is activity on the server socket, it means there is a new incoming connection
if (FD_ISSET(server_fd, &readfds)) {
new_socket = accept(server_fd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen);
if (new_socket < 0) {
perror("accept error");
exit(EXIT_FAILURE);
}
printf("New connection, socket fd is %d, IP is : %s, port : %d\n", new_socket, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Add new socket to set of file descriptors to monitor
for (i = 0; i < MAX_CLIENTS; ++i) {
if (client_sockets[i] == 0) {
client_sockets[i] = new_socket;
break;
}
}
}
// Check each client socket for activity
for (i = 0; i < MAX_CLIENTS; ++i) {
sd = client_sockets[i];
if (FD_ISSET(sd, &readfds)) {
// Receive data from client
valread = read(sd, buffer, BUFFER_SIZE);
if (valread == 0) {
// Client disconnected
printf("Client disconnected, socket fd is %d\n", sd);
close(sd);
client_sockets[i] = 0;
} else {
// Display received data and send response back to client
printf("Received data from client %d: %s\n", sd, buffer);
send(sd, buffer, strlen(buffer), 0);
}
}
}
}
return 0;
}
```
在上述代码中,我们首先创建了一个服务器套接字,然后将它绑定到指定的端口并开始监听。接着,在一个循环中,我们使用select函数来等待任何一个文件描述符上的活动,当有活动时,我们就处理该活动。如果是服务器套接字上的活动,说明有新的客户端连接,我们接受该连接并将其添加到要监听的文件描述符集合中。如果是客户端套接字上的活动,说明客户端已经发送数据,我们读取该数据并向客户端发送回复。如果客户端已经断开连接,我们将其从要监听的文件描述符集合中移除,并关闭该套接字。
阅读全文