[使用C ++通过TCP发送文件,大小错误

问题描述 投票:0回答:1

我对套接字编程非常陌生,我正在尝试通过TCP连接发送,但几乎没有错误。

这是我的代码

        FILE* File;
        char* Buffer;
        unsigned long Size;

        File = fopen("C:\\test.zip", "rb");
        if (!File)
        {
            printf("Error while readaing the file\n");
            return;
        }
        // file size 1
        fseek(File, 0, SEEK_END);
        Size = ftell(File);
        fseek(File, 0, SEEK_SET);


        Buffer = new char[Size]; 

        fread(Buffer, Size, 1, File);
        char cSize[MAX_PATH];
        sprintf(cSize, "%i", Size);

        cout << "MAX PATH " << MAX_PATH<<endl;
        cout << "cSize: " << cSize << endl;
        fclose(File);

`所以这找到了我文件的大小。我正在从这里的其他问题中尝试大多数代码,但它没有解决我的问题。'我的发送和接收:

unsigned long filechunk = 1025;

unsigned long byteSent = 0;
unsigned long bytesToSend = 0;

send(Sub, cSize, MAX_PATH, 0); // File size to client

while (byteSent < Size) {

    if ((Size - byteSent) >= filechunk) {
        bytesToSend = filechunk;
    }
    else {
        bytesToSend = Size - byteSent;
    }
    if (send(Sub, Buffer + byteSent, bytesToSend, 0)) {
        std::cout << "Sent: ";
    }
    byteSent += bytesToSend;
    std::cout << "Size : "<<Size<<"    BytesSent : "<<byteSent<<"   Bytes to send: " << bytesToSend << std::endl;
    system("pause");

在客户端:

int Size;
char* Filesize = new char[5000000]; // is there a better way? my sfiles size are unknown but at least 50mb

if (recv(Socket, Filesize, 5000000, 0)) // File size
{
    Size = atoi((const char*)Filesize);
    printf("File size: %d\n", Size);
}

char* Buffer = new char[Size];
FILE* File;
File = fopen("test.zip", "wb"); //start copying from the server, creating the file first.
std::string convert;
long conv;

std::cout << "Size: " << Size << std::endl;

int total=Size;
int byteRecv = 0;
int recvCheck;
int bytes = 1025;

//getting the file
while (byteRecv < Size ) {

    recvCheck = recv(Socket, Buffer, bytes, 0);
    if (recvCheck >0) // File
    {
        fwrite(Buffer, 1, byteRecv, File);

        std::cout << "Recieved:" << byteRecv << std::endl;

        Size -= byteRecv;
        byteRecv += byteRecv;
        std::cout << "Error: " << WSAGetLastError();
    }
    else {
        std::cout << "Error: " << WSAGetLastError();
        total += 1; // the loop often get into infinit loop so i force it in case of this error.
        if (total > 3) {
            break;
        }
    }
}
fclose(File);

所以,我知道这不是很有效,我不确定是否有类似的问题,就像我在这里已经研究了几个星期了。

-有没有更好的方法可以制作char * []?因为我不知道我要发送的文件大小。-ftell()和sifeof()是否以相同的方式工作?-当我检查从服务器获取的大小时,出现错误。例如:伺服器档案:32633513,接收大小:3263-我从其他问题中提取并合并了大部分代码。如果您看到不需要的内容,请告诉我,以便我记录下来。

c++ file websocket tcp transfer
1个回答
0
投票

有很多错误的东西,但起初可能会纠正您的问题:

在客户端替换(您都减少了字节总数,并且接收到的字节数都有错误的值):

Size -= byteRecv;
byteRecv += byteRecv;

with:

byteRecv += recvCheck; // actualizes the count of received bytes

另一个问题是您的缓冲区大小。永远不要试图在内存中获取整个文件,这通常是胡说八道。文件通常是按块管理的。由于每个循环最多读取1025个字节,因此仅使用大小为1025的缓冲区,则不需要更多。读写也一样...

© www.soinside.com 2019 - 2024. All rights reserved.