通过socket文件传输,以较少的字节大小决赛 [英] File transfer through sockets, final size with less bytes

查看:277
本文介绍了通过socket文件传输,以较少的字节大小决赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接收通过在C插座一些文件,但服务器发送我的64个字节的数据包例如百万字节的文件,我也得到约999902个字节的目标文件。

I'm trying to receive some file through sockets in C. But the server sends me 64-byte packets for a 1000000 byte file for example and I get approximately 999902 bytes on the destination file.

while ((n = read(sd, buffer_in, BUFSIZE ))) //  BUFSIZE = 64 
{
    if(n<0)
    {
       printf("Fail.\n");
       fclose(archivo);
       return -1;
    }

    if(fwrite(buffer_in, n, 1, f) !=1 ) 
    { 
       printf("fwrite error.\n");
       fclose(archivo);
       return -1;
    }

    bytes+=n;
}

printf("We received %d bytes",  bytes);

在通过本地TCP / IP套接字它的作品使用,但不是在一个缓慢的连接。我通过调试,我得到了很多的64字节块,靠近EOF一个30字节块看看。我知道,你可以得到一个read()少字节,因为调用返回时将所有数据(> 1字节)可用。但是,这个条件不应该由同时被钓到?应该返回当n == 0,即没有更多的数据(EOF)。

When used through a local TCP/IP socket it works, but not in a slow connection. I see through debugging that I get a lot of 64 byte chunks, and a 30 byte chunk near EOF. I know that you can get less bytes on read() since the call returns when any data (>1 byte) is available. But this condition shouldn't be catched by the while? Should return when n == 0, that is no more data (EOF).

THX对您有所帮助。

Thx for your help.

(编辑)

发送code如下:

while (n=read(file_fd, buffer, BUFSIZE))
{
   write (sdaccept, buffer, n)
}

我知道,这两个的read()和write()可能返回N'LT; BUFSIZE,但不应这个循环制定出相应的那个?我加了n,返回1000000的确切大小。

I know that both read() and write () may return N < BUFSIZE, but shouldn't this loop work out that accordingly? I added up n and returns 1000000, the exact size.

(编辑II)

与10673个字节的C源测试,接收10575没有腐败,但目标文件缺少第一个98个字节!

Tested with a C source with 10673 bytes, receives 10575 without corruption, except that the destination file LACKS the first 98 bytes!!!

推荐答案

提供的送code忽略写()(或发送())套接字上没有义务写全缓冲的事实。

The sending code provided ignores the fact that write() (or send() ) on a socket is not obliged to write the whole buffer.

写()/发送()可能会决定将其部分写或如果底层子系统拒绝接收更多的数据(例如网络子系统可能对数据的队列发送和队列已经都不写充分)。这是一个缓慢的连接可能性非常大。

write()/send() might decide to write it partially or not write at all if the underlying subsystem refuses to receive more data (for example the network subsystem may have a queue for the data to send and this queue is already full). That's highly likely on a slow connection.

发送方应该检查的write()的返回值来检测多少数据被实际写入,并相应调整。

The sending side should check the return value of write() to detect how much data has been actually written and adjust accordingly.

写应该得到某种做过这样的:

Write should be done somehow like this:

int readAmount;
while( readAmount = read(file_fd, buffer, BUFSIZE) > 0 )
{
    int totalWritten = 0;
    do {
       int actualWritten;
       actualWritten = write (sdaccept, buffer + totalWritten, readAmount - totalWritten);
       if( actualWritten == - 1 ) {
           //some error occured - quit;
       }
       totalWritten += actualWritten;
    } while( totalWritten < readAmount );
}

这篇关于通过socket文件传输,以较少的字节大小决赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆