本教程假设你具有套接字编程的基本知识,即你熟悉基本的服务器和客户端模型。在基本模型中,服务器一次只处理一个客户端,如果你想开发任何可扩展的服务器模型,这是一个很大的假设,特别是本文讨论的C/C++处理服务器上的多个客户端。
C/C++中的套接字编程处理多个客户端的简单方法是为每个连接到服务器的新客户端生成新线程。强烈不推荐这种方法,因为有各种缺点,即:
- 线程很难编码、调试,有时它们会产生不可预测的结果。
- 上下文的开销切换
- 对于大量客户端不可扩展
- 可能会发生死锁
Select()
处理多个客户端的更好方法是使用select() linux 命令。
- Select 命令允许监视多个文件描述符,等待直到其中一个文件描述符变为活动状态。
- 例如,如果要在其中一个套接字上读取一些数据,select 将提供该信息。
- Select 的工作方式类似于中断处理程序,一旦任何文件描述符发送任何数据,它就会被激活。
用于选择的数据结构: fd_set
它包含要监视某些活动的文件描述符列表。
有四个与 fd_set 相关的函数:
fd_set readfds;
// Clear an fd_set
FD_ZERO(&readfds);
// Add a descriptor to an fd_set
FD_SET(master_sock, &readfds);
// Remove a descriptor from an fd_set
FD_CLR(master_sock, &readfds);
//If something happened on the master socket , then its an incoming connection
FD_ISSET(master_sock, &readfds);
激活选择:请阅读select的手册页以检查 select 命令的所有参数。
activity = select( max_fd + 1 , &readfds , NULL , NULL , NULL);
C/C++中的套接字编程实现示例:
//Example code: A simple server side code, which echos back the received message.
//Handle multiple socket connections with select and fd_set on Linux
#include <stdio.h>
#include <string.h> //strlen
#include <stdlib.h>
#include <errno.h>
#include <unistd.h> //close
#include <arpa/inet.h> //close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
#define TRUE 1
#define FALSE 0
#define PORT 8888
int main(int argc , char *argv[])
{
int opt = TRUE;
int master_socket , addrlen , new_socket , client_socket[30] ,
max_clients = 30 , activity, i , valread , sd;
int max_sd;
struct sockaddr_in address;
char buffer[1025]; //data buffer of 1K
//set of socket descriptors
fd_set readfds;
//a message
char *message = "ECHO Daemon v1.0 \r\n";
//initialise all client_socket[] to 0 so not checked
for (i = 0; i < max_clients; i++)
{
client_socket[i] = 0;
}
//create a master socket
if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
//set master socket to allow multiple connections ,
//this is just a good habit, it will work without this
if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
//type of socket created
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
//bind the socket to localhost port 8888
if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", PORT);
//try to specify maximum of 3 pending connections for the master socket
if (listen(master_socket, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
//accept the incoming connection
addrlen = sizeof(address);
puts("Waiting for connections ...");
while(TRUE)
{
//clear the socket set
FD_ZERO(&readfds);
//add master socket to set
FD_SET(master_socket, &readfds);
max_sd = master_socket;
//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 , &readfds);
//highest file descriptor number, need it for the select function
if(sd > max_sd)
max_sd = sd;
}
//wait for an activity on one of the sockets , timeout is NULL ,
//so wait indefinitely
activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);
if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
//If something happened on the master socket ,
//then its an incoming connection
if (FD_ISSET(master_socket, &readfds))
{
if ((new_socket = accept(master_socket,
(struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d
\n" , new_socket , inet_ntoa(address.sin_addr) , ntohs
(address.sin_port));
//send new connection greeting message
if( send(new_socket, message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
puts("Welcome message sent successfully");
//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;
printf("Adding to list of sockets as %d\n" , i);
break;
}
}
}
//else its some IO operation on some other socket
for (i = 0; i < max_clients; i++)
{
sd = client_socket[i];
if (FD_ISSET( sd , &readfds))
{
//Check if it was for closing , and also read the
//incoming message
if ((valread = read( sd , buffer, 1024)) == 0)
{
//Somebody disconnected , get his details and print
getpeername(sd , (struct sockaddr*)&address , \
(socklen_t*)&addrlen);
printf("Host disconnected , ip %s , port %d \n" ,
inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
close( sd );
client_socket[i] = 0;
}
//Echo back the message that came in
else
{
//set the string terminating NULL byte on the end
//of the data read
buffer[valread] = '\0';
send(sd , buffer , strlen(buffer) , 0 );
}
}
}
}
return 0;
}
C/C++处理服务器上的多个客户端 - 编译文件并运行服务器。
使用 telnet 作为客户端连接服务器。
尝试使用以下命令在不同的机器上运行:
telnet localhost 8888
C/C++中的套接字编程代码说明:
- 我们创建了一个 fd_set 变量 readfds,它将监视客户端的所有活动文件描述符以及主服务器侦听套接字的活动文件描述符。
- 每当有新客户端连接时,master_socket 将被激活,并为该客户端打开一个新的 fd。我们将其 fd 存储在我们的 client_list 中,并在下一次迭代中将其添加到 readfds 以监视来自该客户端的活动。
- 同样,如果旧客户端发送了一些数据,则 readfds 将被激活,我们将从现有客户端列表中查看哪个客户端发送了数据。替代方案:
还有其他函数可以执行类似于 select 的任务。pselect , 轮询 , ppoll