简体   繁体   English

接收的字节数多于发送的C ++

[英]Receiving more bytes than sent C++

I want to send a file from a Linux server to a Windows client through sockets, the problem is that I receive more bytes than I send. 我想通过套接字将文件从Linux服务器发送到Windows客户端,问题是我收到的字节多于发送的字节。

Server code---------------------------------------------- 服务器代码----------------------------------------------

if (resultEnviarLongitud = send(ClientSocket,GotFileSize.c_str(),1024,0)<0){
    cout<<endl<<"Error mandando la longitud! "<<endl;
}
rewind(fs);

while ((len = fread(Buffer,1,1024, fs)) > 0)
{
    if((resultEnviar = send(ClientSocket,Buffer,1024,0)) < 0){
        printf("ERROR: Failed to send file %s.\n", nombreArchivoADescargar.c_str());
        break;
    }
    sumEnviada+=len;
}

send(ClientSocket,"Hi",sizeof(Buffer),0);


cout<<"Bytes enviados: "<<sumEnviada<<endl;
strcpy(data, "");

cout<<endl<<"ARCHIVO MANDADO EXITOSAMENTE!"<<endl;
rutaArchivoADescargar.clear();

Client code----------------------------------------- 客户代码-----------------------------------------

if (resultRecibirLongitud = recv(sock, Buffer, sizeof(Buffer), 0) > 0)
{
    LongitudArchivo = atoi(Buffer);
    cout<<endl<<"Longitud Archivo a Recibir: " <<LongitudArchivo<<endl; 
}

FILE *fp=fopen("imagen.jpg","wb");
if (fp==NULL){
    cout<<"Error al crear archivo."<<endl;
}else{
    bzero(Buffer2, 1024); 
    int fr_block_sz = 0;
    int contador=0;

    //shutdown(sock, SD_SEND); I HAVE TO USE IT?

    while((fr_block_sz = recv(sock, Buffer2, 1024, 0)) >= 0) 
    {
        if (fr_block_sz == 0) break;
        if ( strcmp (Buffer,"Hi") == 0) break;
        int write_sz = fwrite(Buffer2, 1, 1024, fp);
        if(write_sz < fr_block_sz)
        {
            printf("File write failed on server.\n");
        }
        bzero(Buffer2, 1024);
        contador+=fr_block_sz;
        if (contador >= LongitudArchivo)break;
        bzero(Buffer2, 1024); 
    }
    cout<<endl<<"Numero de bytes recibidos: "<<contador<<endl<<endl;
    if(fr_block_sz < 0)
    {
        printf("Error receiving file from client to server.\n");
    }
    printf("Ok received from client!\n");
    fclose(fp); 
}

Thanks, 谢谢,

while ((len = fread(Buffer,1,1024, fs)) > 0)
{
    if((resultEnviar = send(ClientSocket,Buffer,1024,0)) < 0)

One of your issues is that you always send 1024 bytes of the buffer even if you fread fewer bytes. 问题之一是,即使fread的字节数更少,也始终发送1024字节的缓冲区。 (Note that 1348656 rounded up to the nearest multiple of 1024 is 1349632.) (请注意,将1348656舍入为1024的最接近倍数是1349632。)

So, on the write side you want something like: 因此,在写方面,您需要以下内容:

while ((len = fread(Buffer,1,1024, fs)) > 0)
{
    if((resultEnviar = send(ClientSocket,Buffer,len,0)) < 0)

and on the read side you want something like: 在阅读方面,您需要类似:

while((fr_block_sz = recv(sock, Buffer2, 1024, 0)) >= 0) 
{
    // ...
    int write_sz = fwrite(Buffer2, 1, fr_block_sz, fp);

Your initial send is also problematic as you always send 1024 bytes with no check that this is the actual length of what is returned by c_str . 您的初始send也有问题,因为您总是发送1024字节而没有检查这是否是c_str返回的实际长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM