我刚刚通过多线程创建了客户端 - 服务器通信。 我的问题是我想在没有收到任何消息的情况下每20秒显示一条消息。
这是我的代码:
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , address;
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
struct timeval timeout;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
// if (setsockopt (socket_desc, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
// perror("setsockopt failed\n");
// if (setsockopt (socket_desc, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
// perror("setsockopt failed\n");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( PORT );
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
listen(socket_desc , 3);
puts("Oczekiwanie na połączenia ...");
c = sizeof(struct sockaddr_in);
pthread_t thread_id;
while( (new_socket = accept(socket_desc, (struct sockaddr *)&address, (socklen_t*)&c)) )
{
printf("Połączono z nowym klientem, socket fd: %d , ip: %s , port: %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
if( pthread_create( &thread_id , NULL , connection_handler , (void*) &new_socket) < 0)
{
perror("ERROR: nie można utworzyć wątku");
exit(EXIT_FAILURE);
}
}
if (new_socket < 0)
{
perror("accept failed");
exit(EXIT_FAILURE);
}
return 0;
}
和处理函数:
void *connection_handler(void *socket_desc)
{
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
message = "Nazwiązano połączenie z serwerem \r\n";
write(sock , message , strlen(message));
//przetwarzanie wiadomosci
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
client_message[read_size] = '\0';
printf("%s", client_message);
memset(client_message, 0, 2000);
}
if(read_size == 0)
{
printf("Połączenie z klientem zostało przerwane\n");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
我想得到与选择此处相同的结果:
struct timeval tv;
tv.tv_sec = 20;
tv.tv_usec = 0;
activity = select( max_sd + 1 , &readfds , NULL , NULL , &tv);
if (activity == -1)
perror("select() failed");
else if (activity){
tv.tv_sec = delay;
}
else{
time_t t = time(0);
cout << "DELAY MESSAGE: " << currentDateTime() << endl;
tv.tv_sec += delay;
}
有什么建议吗?非常感谢您的帮助!
答案 0 :(得分:0)
您需要将套接字选项设置为SO_RCVTIMEO。有关此套接字选项的更多信息,请参阅以下链接。
http://linux.die.net/man/3/setsockopt
您在connection_handler中的代码应设置此选项,如下所示
struct timeval tv;
tv.tv_sec = 20; /* 20 Secs Timeout */
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
/*Now you should read*/
在errno设置为EAGAIN EWOULDBLOCK的指定时间20秒后,您的读取方法将解除阻塞。您可以在循环条件中处理此错误以获得所需的功能。