简体   繁体   English

为什么将TCP数据包发送到本地主机需要这么长时间?

[英]Why does it take so long to send TCP packets to localhost?

I wrote a simple C++ client/server pair. 我写了一个简单的C ++客户/服务器对。 The server just forks a process on socket accept and then waits for a packet from the client and then responds with another packet. 服务器只是在套接字接受上派生一个进程,然后等待来自客户端的数据包,然后以另一个数据包进行响应。 The client just sends a packet to server and then waits for a reply. 客户端只是向服务器发送一个数据包,然后等待答复。 I have timing code in the client right before the send and then after the receive. 在发送之前和接收之后,我在客户端中都有计时代码。

I am running both the server and client on my local box and connecting the client to local host. 我在本地计算机上同时运行服务器和客户端,并将客户端连接到本地主机。

In my timings the median latency seems to be around 2 milliseconds. 在我看来,延迟的中位数似乎是2毫秒左右。 Given that I am not really sending anything out on the network. 鉴于我实际上并没有在网络上发送任何内容。 The 2 millisecond latency seems awfully high to me. 2毫秒的延迟对我来说似乎太高了。

Can anyone explain why I see such a high latency or if this amount of time is realistic for the loopback address? 谁能解释为什么我看到这么高的延迟,或者对于环回地址来说,这样的时间是否现实?

I am on Linux Ubuntu 12.04. 我在Linux Ubuntu 12.04上。 I am directly using the TCP socket system calls rather than any wrapper (ie. accept, listen, send, receive). 我直接使用TCP套接字系统调用,而不是使用任何包装程序(即,接受,监听,发送,接收)。

Server body: 服务器主体:

while (1) 
{   
    ++msgNum;

    sin_size = sizeof their_addr; 
    new_fd = accept(sockfd, (struct sockaddr*) &their_addr, &sin_size);
    if (new_fd == -1) 
    {   
        perror("accept");
        continue; 
    }   

    inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr*) &their_addr), 
        s, sizeof s); 
    printf("server: got connection from %s\n", s); 

    if (!fork())
    {   
        close(sockfd); // child doesn't need the listener

        MyMsg msg;
        strcpy(msg.buf, "Hello world");

        for (int i = 1; i <= NUM_TEST_MSGS; ++i)
        {   
            msg.id = i;

            int numbytes = 0;
            int bytesRecv = 0;

            while (numbytes < MSG_LEN)
            {   
                int sendSize = MSG_LEN - numbytes;
                if ((bytesRecv = send(new_fd, ((char*) &msg) + numbytes, 
                        sendSize, 0)) == -1) 
                {   
                    perror("send");
                    exit(1);
                }   
                numbytes += bytesRecv;
            }   

            assert(numbytes == MSG_LEN);

            //printf("Server sent %d num bytes\n", numbytes);
        }   

        printf("Server finished sending msgs.\n");

        close(new_fd);
        exit(0);
    }   
    close(new_fd);
} 

Client body: 客户团体:

for (int i = 1; i <= NUM_TEST_MSGS; ++i)
{
    MyMsg msg;

    int numbytes = 0;
    int bytesRecv = 0;

    int start = rdTsc.Rdtsc();

    while (numbytes < MSG_LEN)
    {
        int recvSize = MSG_LEN - numbytes;
        if ((bytesRecv = recv(sockfd, ((char*) &msg) + numbytes, recvSize, 0)) == -1)
        {
            perror("recv");
            exit(1);
        }

        numbytes += bytesRecv;
    }

    int end = rdTsc.Rdtsc();

    perfCounter.Track(end - start);

    if (numbytes != MSG_LEN)
    {
        printf("COMP FAILED: %d %d\n", numbytes, MSG_LEN);
    }

    assert(numbytes == MSG_LEN);

    if (i != msg.id)
    {
        printf("Msg %d id %d \n", i, msg.id);
    }

    //if (numbytes != MSG_LEN) printf("GOT WEIRD SIZE %d\n", numbytes);
    assert(msg.id == i);

    //printf("client: received %d num bytes id %d body '%s'\n", numbytes, msg.id, msg.buf);

    if (i % 1000 == 0)
    {
        printf("Client: Received %d num msgs.\n", i);
    }
}

printf("Client: Finished successfully.\n");

close(sockfd);

2ms certainly sounds high for something that probably never even left the kernel buffers. 对于可能甚至从未离开内核缓冲区的内容,2ms当然听起来很高。 I suspect it might actually be inaccuracies in the function used for time-stamping. 我怀疑用于时间戳的功能实际上可能不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的有限状态机需要这么长时间才能执行? - Why does my finite state machine take so long to execute? 为什么在VCC 2003中编译需要这么长时间? - Why does this take so long to compile in VCC 2003? 为什么 C++ 编译需要这么长时间? - Why does C++ compilation take so long? 为什么用 g++ 编译这段代码需要这么长时间? - Why does this code take so long to compile with g++? 为什么我的简单cpp-netlib程序需要这么长时间才能编译? - Why does my simple cpp-netlib program take so long to compile? 如果以root身份运行它,为什么这个C ++程序需要很长时间才能完成? - Why does this C++ program take so long to finish if you run it as a root? 为什么阅读要花这么多时间? - Why does reading take so much time? 我正在从localhost向localhost发送UDP数据包,并且有时会丢弃数据包。 我如何阻止这种情况以及为什么会发生这种情况? - I am sending UDP packets from localhost to localhost and runs on packets sometimes drop. How do I stop this and why does it happen? 为什么加载符号会使 Visual Studio 需要很长时间才能启动? - Why loading symbols make Visual Studio take so long to start? 如何在C ++中发送2个TCP数据包 - How to send 2 TCP packets in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM