使用TCP从客户端向服务器发送字节的问题

Adr*_*ian 1 c c++ sockets winsock

我的send()和recv()看起来像这样:

int Send(const char* buffer, int size)
{
    cout << "SIZE: " << size << endl;
    int offset;
    while(offset < size)
    {
        int n = ::send(getSocket(), buffer + offset, size - offset, 0);
        if(n == SOCKET_ERROR)
        {
            break;
        }
        offset += n;
        if(offset != size)
        {
            Sleep(1);
        }
    }
    return offset;
}

int Recv(char* buffer, int size)
{
    int n = ::recv(getSocket(), buffer, size, 0);
    if(n == SOCKET_ERROR)
    {
        cout << "Error receiving data" << endl;
    }
    if(n == 0)
    {
        cout << "Remote host closed connection" << endl;
    }
    return n;
}
Run Code Online (Sandbox Code Playgroud)

但是我的输出显示了许多发送的字节,这对我来说很奇怪:

Received from client: 669
Sent to web server: 3990336
Run Code Online (Sandbox Code Playgroud)

所以应该发送669个字节,所以从哪里得到3990336?这是某种错误还是?

谢谢.

Err*_*ata 5

你注意到int offset;没有初始化吗?