C ++-使用TCP / IP仿真太慢

时间:2019-07-01 15:11:00

标签: c++ performance sockets optimization tcp

我在两台生成数据的计算机上运行了两个模拟。 为了连接这两个模拟,我运行了一个块(IPTunnel),该块将数据从一个模拟传输到另一模拟(在另一台计算机上)以保持其运行。 问题是TCP连接的优化。

在一台计算机上进行两次模拟大约需要3分钟,而在2台计算机上进行两次模拟则需要大约1小时。

我该如何提高TCP连接的速度?

谢谢!

这是发送数据的代码:

const Nightmare = require('nightmare')
const nightmare = Nightmare({ show: true })

console.log('before')
nightmare
    .goto('https://duckduckgo.com')
    .type('#search_form_input_homepage', 'github nightmare')
    .click('#search_button_homepage')
    .wait('#r1-0 a.result__a')
    .evaluate(() => document.querySelector('#r1-0 a.result__a').href)
    .end()
    .then(function (result) {
        console.log('Result ->', result)
    })
    .catch(error => {
        console.error('Search failed:', error)
    })
console.log('after')

这是该块的通用代码:

void IPTunnel::ipTunnelSendInt(int space) {
    int data = space;
    char* tosend = (char*)& data;
    int remaining = sizeof(data);
    int result = 0;
    int sent = 0;
    while (remaining > 0) {
        result = send(clientSocket, tosend + sent, remaining, 0);
        if (result > 0) {
            remaining -= result;
            sent += remaining;
        }
        else if (result < 0) {
            //printf("ERROR sending int!\n");
            // probably a good idea to close socket
            break;
        }
    }
}

int IPTunnel::ipTunnelRecvInt() {
    int value = 0;
    char* recv_buffer = (char*)& value;
    int remaining = sizeof(int);
    int received = 0;
    int result = 0;
    while (remaining > 0) {
        result = recv(clientSocket, recv_buffer + received, remaining, 0);
        if (result > 0) {
            remaining -= result;
            received += result;
        }
        else if (result == 0) {
            printf("Remote side closed his end of the connection\n");
            // probably a good idea to close socket
            break;
        }
        else if (result < 0) {
            //printf("ERROR receiving int!\n");
            // probably a good idea to close socket
            break;
        }
    }
    return value;
}

(....)

case signal_value_type::t_message:
        for (int k = 0; k < process; k++) {
            t_message message;
            inputSignals[0]->bufferGet(&message);

            t_message_type valueMType = MessageProcessors::getMessageType(message);
            t_message_data_length valueMDataLength = MessageProcessors::getMessageDataLength(message);

            ipTunnelPut(valueMType);
            ipTunnelSendInt(valueMDataLength);
            //////////////////////////////////////////////
            // Point of sending msg length

            string data = message.messageData;

            ipTunnelSendInt((int)data.size());
            int numberOfMessages = (int)ceil(data.size() / 511.0);
            string tmpMsg;
            int msgSent = 0;
            int msgToSend = (int)min(data.size(), 511);

            for (int it = 0; it < numberOfMessages; ++it) {
                char msg[512];


                tmpMsg.assign(data.begin() + msgSent, data.begin() + msgSent + msgToSend);
                msgSent = msgSent + msgToSend;
                msgToSend = (int)min(data.size() - msgSent, 511);
                //acrescentar if se o size for maior que msg
//              if (valueMDataLength > 512)
//                  printf("TAMANHO É MAIOR QUE 512");
                strncpy_s(msg, tmpMsg.c_str(), sizeof(msg));

                //                  msg[sizeof(msg) - 1] = 0;

                int strSz = (int)strlen(msg);


                char* tosend = (char*)& msg;
                int remaining = strSz;
                int result = 0;
                int sent = 0;
                while (remaining > 0) {
                    result = send(clientSocket, tosend + sent, remaining, 0);
                    if (result > 0) {
                        remaining -= result;
                        sent += remaining;
                    }
                    else if (result < 0) {
                        printf("ERROR!\n");
                        // probably a good idea to close socket
                        break;
                    }
                }
            }
        }
        break;

0 个答案:

没有答案