简体   繁体   English

在C ++套接字连接中发送的奇怪行为

[英]Strange behavior of send in c++ socket connection

I'm experimenting with the tcp socket connection under c++. 我正在尝试使用c ++下的tcp套接字连接。 I would like to send the serialized (JSON) form of an std::map object over the network. 我想通过网络发送std :: map对象的序列化(JSON)形式。 I found out that after a certain amount of packets (this depends on the packet size) have been sent, sending is just not possible anymore. 我发现在发送了一定数量的数据包(取决于数据包大小)之后,就无法再发送了。 I have tried both the curl lib and the standard POSIX API for the socket connection: 我已经尝试了curl lib和用于套接字连接的标准POSIX API:

  • send in POSIX API: it starts to block after ~100 packets have been sent 在POSIX API中发送:发送约100个数据包后开始阻塞
  • curl: curl_easy_send seems to be executed but the server does not receive a new packet curl:curl_easy_send似乎已执行,但服务器未收到新数据包

During the communication the same socket connection is used.I guess the whole thing is related to the buffer size, but (1) I have increased it and did not see any significant effect (2) I would expect that sending would be possible again after a given amount of time if the buffer is full currently. 在通信过程中,使用了相同的套接字连接。我想整个事情都与缓冲区大小有关,但是(1)我增加了缓冲区大小,但没有看到任何明显的影响(2)我希望在以后可以再次发送给定的时间(如果缓冲区当前已满)。 Maybe I'm missing something important but silly option or info in this situation. 在这种情况下,也许我错过了一些重要但愚蠢的选择或信息。 So my question is: is the configuration of the server wrong or do I not use the APIs in a correct way? 所以我的问题是:服务器的配置是否错误或我是否以正确的方式使用API​​?

Receiver side: 接收方:

ListenerThread::ListenerThread() {
    curl = curl_easy_init();

    if (curl != 0) {
        curl_easy_setopt(curl, CURLOPT_URL, "...");
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
        res = curl_easy_perform(curl);
        res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);

        if (res != 0) {
            printf("Error: %s\n", curl_easy_strerror(res));
        }
    }
}
...
void ListenerThread::run() {
    while (true) {
        char buf[2048];

        wait_on_socket(sockfd, 1, 60000L);
        res = curl_easy_recv(curl, buf, 2048, &iolen);

        if (CURLE_OK != res) {
            break;
        }

        nread = (curl_off_t) iolen;

        printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread);
        printf("Buffer: %s\n", buf);
    }
}

It could happen if the peer is not reading the data your are sending. 如果对等方未读取您正在发送的数据,则可能会发生这种情况。

The packets get into the receive buffer of the socket on the other end and the ACKs are sent back to you, so some amount of data gets confirmed and is discarded from your socket send buffer. 数据包进入另一端的套接字的接收缓冲区,并将ACK发送回给您,因此一些数据被确认并从套接字的发送缓冲区中丢弃。 However, if the peer is not extracting the data from its receive buffer, it eventually gets full and you cannot send any data until some place is freed there. 但是,如果对等方未从其接收缓冲区中提取数据,则它最终会变满,并且您无法发送任何数据,除非该位置被释放。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM