【问题标题】:How to count time in socket如何计算套接字中的时间
【发布时间】:2015-11-28 01:44:38
【问题描述】:

我有一个简单的服务器-客户端程序,它根据从客户端接收到的数据进行操作。

当接收到的数据是字符串start 时,我希望服务器以秒为单位计算时间,直到它收到来自客户端的stop 命令,并且当服务器接收到stop 时,服务器将计数的秒数发送回客户端。

我是 C 编程新手,不知道如何使用 time.h 库。我尝试使用一些命令,但几乎没有错误。

/* tcpserver.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/sysinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "address.h"
int main() {
    int sock, connected, bytes_recieved , true = 1, number;  
    char send_data [1024] , recv_data[1024];       
    struct sockaddr_in server_addr,client_addr; 
    struct sysinfo info;   
    int sin_size;
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }
    if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
        perror("Setsockopt");
        exit(1);
    }
    server_addr.sin_family = AF_INET;         
    server_addr.sin_port = htons(1237);     
    server_addr.sin_addr.s_addr = INADDR_ANY; 
    bzero(&(server_addr.sin_zero),8); 
    if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))
                                                               == -1) {
        perror("Unable to bind");
        exit(1);
    }
    if (listen(sock, 5) == -1) {
        perror("Listen");
        exit(1);
    }
    printf ("\nTCPServer Waiting for client on port 1237");
    fflush(stdout);
    number = 0;
    while(1) {  
        sin_size = sizeof(struct sockaddr_in);
        connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
    printf("\n I got a connection from (%s , %d)",
    inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
    send(connected, "Hello, this is simple program.", 100, 1);
    while (1) {
        printf("\n SEND (q or Q to quit) : ");
        fgets(send_data, sizeof(send_data), stdin);
        bytes_recieved = recv(connected,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';
        if (strcmp(favoriteDairyProduct, "start") == 0 || strcmp(favoriteDairyProduct, "START") == 0) {
        // count the time until stop is recieved


        }
        printf("\n RECIEVED DATA = %s " , recv_data);
        fflush(stdout);   
        send(connected, send_data,strlen(send_data), 0);  
      }       
    }
    close(sock);
    return 0;
}

这是客户端代码。我想做的另一件事是更改程序以接收多个客户。但现在计时器更重要。

/* tcpclient.c */
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/sysinfo.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "address.h"
#include <time.h>

int main()
{
    int sock, bytes_recieved, number;  
    char send_data[1024],recv_data[1024];
    struct hostent *host;
    struct sockaddr_in server_addr;  
    struct sysinfo info;   
    host = gethostbyname("127.0.0.1");
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }
    server_addr.sin_family = AF_INET;     
    server_addr.sin_port = htons(1237);   
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server_addr.sin_zero),8); 
    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("Connect");
        exit(1);
    }
    while(1) {
        bytes_recieved = recv(sock,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';
        printf("\nRecieved data = %s " , recv_data);
        printf("\nSEND (q or Q to quit) : ");
        fgets(send_data, sizeof(send_data), stdin);        
        send(sock,send_data, sizeof(send_data), 0);
    }   
    close(sock);         
    return 0;
}

【问题讨论】:

    标签: c sockets timer client-server time.h


    【解决方案1】:

    根据您所在的平台,您可以在收到开始和停止消息时使用 gettimeofday()。

    #include <sys/time.h>
    
    struct timeval t1, t2;
    if (<received start command>) {
        gettimeofday(&t1, NULL);
    }
    if (<received stop command>) {
        gettimeofday(&t2, NULL);
    }
    

    每个都以秒和微秒为单位为您提供当前时间。你必须找到两者之间的区别。

    time_t diff_sec = t2.tv_sec - t1.tv_sec;
    suseconds_t diff_usec = t2.tv_usec - t1.tv_usec;
    

    当然,您必须考虑到 t2.tv_usec 可能小于 t1.tv_usec。

    您还应该验证您是否在停止之前收到了开始,否则结果将没有意义。

    我将把剩下的作为练习留给读者,因为这看起来像家庭作业。

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多