Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从套接字c+发送和接收文件时出现问题+;_C++_Sockets_Mfc - Fatal编程技术网

C++ 从套接字c+发送和接收文件时出现问题+;

C++ 从套接字c+发送和接收文件时出现问题+;,c++,sockets,mfc,C++,Sockets,Mfc,我正在尝试将文件从服务器发送到客户端 我在我的服务器上有这个功能: UINT CServerDlg::sendFile(WPARAM pParam) { if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { return FALSE; } else { if (AfxSocketInit() == FALSE) {

我正在尝试将文件从服务器发送到客户端 我在我的服务器上有这个功能:

UINT CServerDlg::sendFile(WPARAM pParam)
{
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        return FALSE;
    }
    else {
        if (AfxSocketInit() == FALSE)
        {
            return FALSE;
        }

        CSocket ServerSocket;
        if (ServerSocket.Create(Dport, SOCK_STREAM, NULL) == 0)
        {
            return FALSE;
        }
        else
        {
            if (ServerSocket.Listen(32) == FALSE)
            {
                ServerSocket.Close();
                return FALSE;
            }

        }


        CSocket Connector;
        if (ServerSocket.Accept(Connector))
        {
            char* buffer = NULL;

            FILE* fi = fopen(file_name.c_str(), "rb");
            if (!fi)
                return 0;

            //get the file length
            fseek(fi, 0, SEEK_END);
            long file_size = ftell(fi);
            fseek(fi, 0, SEEK_SET);

            //send packet's length first and then send the packet
            //(one file includes multiple packets) and SIZE_BUFFER=4096
            int result=-1;
            int size = (file_size < SIZE_BUFFER) ? (int)file_size : SIZE_BUFFER;
            while (file_size >= (long long)SIZE_BUFFER) {
                fread(buffer, size, 1, fi);
                result = send(Connector, (char*)&file_size, sizeof(int), 0);

                if (result == SOCKET_ERROR) return 0;
                do { size = send(Connector,buffer, size, 0); } while (size == -1);

                memset(buffer, 0, SIZE_BUFFER);
                file_size = file_size - (long long)size;
            }
            if (file_size != 0) {
                if (send(Connector, (char*)&file_size, sizeof(int), 0) == SOCKET_ERROR) return 0;
                fread(buffer, file_size, 1, fi);
                do { size = send(Connector,buffer, file_size, 0); } while (size == -1);

            }

            delete[] buffer;
            fclose(fi);
        }
        else {
            return 0;
        }
        Connector.Close();
        ServerSocket.Close();
    }
    return 1;
}
服务器和客户端已连接,但似乎无法发送数据包。即使它发送失败,但执行文件没有调用错误,但当我调试它时,调用了错误。我在
fread(缓冲区,文件大小,1,fi)
处遇到未知错误:
Server.exe中0x00007FF6F17BDF28处未处理的异常:向认为无效参数致命的函数传递了无效参数。

您必须为缓冲区分配内存,因为fread的第一个参数是:

指向大小至少为(
size*count
)字节的内存块的指针,转换为
void*

使用或


我建议使用,不要担心删除内存。

您必须为缓冲区分配内存,因为
fread
的第一个参数:

指向大小至少为(
size*count
)字节的内存块的指针,转换为
void*

使用或


我建议使用,不要担心删除内存。

是的,我尝试过,它解决了问题,但发送函数返回了错误的号码。我得到的文件有7个字节,它返回4,因为您编写了
send(Connector,(char*)&文件大小,sizeof(int),0)第三个参数是要发送的数据长度。必须传递数组长度。例如
send(连接器,(字符*)&文件大小,buffer.length,0)。但请记住,如果缓冲区是
char*
,则
sizeof(buffer)
返回指针大小(4或8字节)。如果使用
std::array
只需传递
buffer.size()
。对不起,我的英语不好。是的,我尝试了一下,它解决了问题,但发送函数返回了错误的号码。我得到的文件有7个字节,它返回4,因为您编写了
send(Connector,(char*)&文件大小,sizeof(int),0)第三个参数是要发送的数据长度。必须传递数组长度。例如
send(连接器,(字符*)&文件大小,buffer.length,0)。但请记住,如果缓冲区是
char*
,则
sizeof(buffer)
返回指针大小(4或8字节)。如果使用
std::array
只需传递
buffer.size()
。对不起,我的英语不好。
bool MainDlg::DownloadFile(char* file_name, int port) {
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        return FALSE;
    }
    //copy tu demo
    if (AfxSocketInit() == FALSE)
    {
        return FALSE;
    }

    CSocket ClientSocket;
    ClientSocket.Create();

    if (ClientSocket.Connect(_T("127.0.0.1"), port) != 0)
    {
        char* buffer = NULL;
        int size = 0;
        FILE* fo = fopen(file_name, "wb");
        recv(ClientSocket, (char*)&size, sizeof(int), 0);
        while (size >= SIZE_BUFFER) {
            do { size = recv(ClientSocket, (char*)buffer, SIZE_BUFFER, 0); } while (size == -1);

            fwrite((char*)buffer, size, 1, fo);
            memset(buffer, 0, SIZE_BUFFER);
            size = recv(ClientSocket, (char*)&size, sizeof(int), 0);
        }
        if ((size != SOCKET_ERROR)) size = recv(ClientSocket, (char*)buffer, size, 0);
        else return 0;
        delete[] buffer;
        fclose(fo);
    }
    else {
        return 0;
    }
    ClientSocket.Close();
    return 1;
}