如果我没有收到来自客户端的消息,如何每 20 秒打印一次日期时间

How to print datetime every 20sec if I didn't get message from client

我刚刚通过多线程创建了客户端-服务器通信。 我的问题是我想在没有收到消息时每 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] = '[=11=]';

        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;
}

我想在这里得到与 Select 相同的结果:

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;
    }

有什么建议吗?非常感谢您的帮助!

您需要将套接字选项设置为SO_RCVTIMEO。有关此套接字选项的更多信息,请参阅以下 link。

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*/

您的读取方法将在 20 秒的指定时间后解除阻塞,并将 errno 设置为 EAGAIN EWOULDBLOCK。您可以在循环条件中处理此错误号以获得所需的功能。