recv()返回SOCKET_ERROR

时间:2019-04-24 20:41:30

标签: c++ winsock

现在,我目前正在尝试学习winsock,为此,我正在尝试通过套接字发送文件。我已经编写了所有代码,并使其部分工作,仅发送图像的顶部,但现在它已完全停止工作。我不知道是什么导致send和recv返回SOCKET_ERROR。

说实话,我不知道该怎么办。套接字只是关闭便便本身,我不知道为什么。

这是我用来接收文件大小的代码,也是文件本身。

cout << "Fetching file from server" << endl;

        int nBytes = 4096, nLeft, idx; // I have no idea what this does, ctrl c + ctrl v always works though, maybe this is the problem?
        nLeft = nBytes;
        idx = 0;


        while (nLeft > 0)

        {
            ret = recv(listening, sizef[idx], nLeft, 0);
            if (ret == SOCKET_ERROR)
            {
                WSACleanup();
                return 912;
            }
            nLeft -= ret;
            idx += ret;
        }

这是我用来发送文件大小和文件的代码。

int nBytes = 4096, nLeft, idx;
    nLeft = nBytes;
    idx = 0;
    while (nLeft > 0)
    {
        ret = send(clientSocket, &cstr[idx], nLeft, 0);

        if (ret == SOCKET_ERROR)

        {
            cerr << "Oops, failed to send, the programmer shit his pants, or the client did";
            return 912;
        }
        nLeft -= ret;
        idx += ret;
    }

现在,此功能的预期结果是仅接收文件,但对于我尝试的每个文件,它始终返回SOCKET_ERROR。

0 个答案:

没有答案