我是套接字编程新手。我正在尝试制作一个可以处理多个连接的 TCP 监听器。
我找到了this看起来非常有用的例子。
此代码的问题在于,它仅在收到数据时才将数据发送到连接的客户端。我想异步发送数据到连接的客户端。
我看到 select()
函数永远阻塞代码,直到事件到达套接字。
我正在考虑在 select()
函数中添加一个延迟(而不是 NULL
),以便它每隔几微秒超时一次,并且程序将能够发送数据,如果有的话。在 if (buffer[0] > 0)
问题是:有没有更好的方法来做我想做的事?我可以用其他方式强制 select()
超时吗?
char buffer[1025];
是一个从另一个线程填充的全局数组。下面的 while(TRUE)
正在另一个线程上运行。
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
struct timeval timeout;
timeout.tv_usec = 10000;
activity = select( max_sd + 1 , &readfds , NULL , NULL , &timeout);
if ((activity < 0) && (errno!=EINTR))
{
cout << "select error" << endl;
}
//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;
}
}
}
if (buffer[0] > 0)
{
sd = client_socket[0];
send(sd , buffer , strlen(buffer) , 0 );
}
//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 );
}
}
}
}
最佳答案
The problem with this code is that it sends data to the connected clients only when data received.
这是因为原始代码实现了请求/响应服务器 - 在本例中,它具体是一个回显服务器。
I want to send data to the connected clients asynchonously.
您可能需要首先枚举服务器开始异步发送数据的条件/触发器。我即兴编造的一个例子是多回波服务器。一旦这样的服务器从客户端接收到一个字符串,它就会回显 3 次,每次回显间隔(比如说)1 秒。
在这种情况下,可以使用选择超时来中断等待,并且可以修改“if buffer[0] > 0”检查以发送任何挂起的回显。
The question is: Is there any better way to do what I want? Can I force select() to timeout with other way?
man-page表示还有第三种方法可以打破选择,即安排信号的传递。但是,我认为,这应该用于处理传递到服务器程序的信号,并且仅用于处理异步 IO 的选择超时。更准确地说,我见过生产级服务器在异步循环中使用 epoll/kqueue(更好的选择替代方案)超时。所以,你的做法看起来很合理。
关于c++ - 如何在 TCP 监听器中处理异步发送和接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018933/