想搞个传文件,传完以后文件扩大了100倍。7.08M 的 MP3 变成了 708M
下面在C盘根目录准备个MP3文件,然后用传文件传到D盘根目录
下面的代码是从客户端向服务器端传#include <iostream>
using namespace std;
#include <afxtempl.h>
#include <afxwin.h>
void main()
{
cout<<" 服务器 "<<endl;
WSADATA wd;
WSAStartup(MAKEWORD(2,2), &wd);
SOCKET s1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN tcpaddr;
tcpaddr.sin_family = AF_INET;
tcpaddr.sin_port = htons(5150); tcpaddr.sin_addr.S_un.S_addr = htonl( INADDR_ANY );
bind(s1, (SOCKADDR*)&tcpaddr, sizeof(tcpaddr)) ;
listen(s1, 5);

SOCKADDR_IN ClientAddr;
int len= sizeof(SOCKADDR_IN);;
SOCKET s2;
s2 = accept(s1, (SOCKADDR*)&ClientAddr, &len);
if( s2 != SOCKET_ERROR )
cout<<"有客户机连入"<<endl; char size[100];
recv(s2, size, 100,0);
cout<<"服务器端收到的文件大小为\t"<<size<<endl;
ULONGLONG nSize = _atoi64(size); char * buf = new char[ _atoi64(size) ];
::ZeroMemory(buf,_atoi64(size));
//_atoi64(buf);
CFile file(L"d:\\b.mp3", CFile::modeCreate | CFile::typeBinary | CFile::modeReadWrite);

while(1)
{
char * buf;
if( nSize >= 1000)
{
buf = new char[1000];
recv(s1, buf, 1000, 0);
file.Write(buf, 1000);
nSize -= 1000;
}
else if( nSize == 0 )
break;
else
{
buf = new char[nSize];
recv(s1, buf, nSize, 0);
file.Write(buf, nSize);
break;
}
}

file.Close();
delete [] buf;
WSACleanup();
}#include <iostream>
using namespace std;
#include <afxwin.h>
void main()
{
cout<<" 客户端 "<<endl;
WSADATA wd;
WSAStartup(MAKEWORD(2,2), &wd);
SOCKET s1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKADDR_IN add;
add.sin_family = AF_INET;
add.sin_port = htons(5150);
add.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); int a=connect(s1, (SOCKADDR*)&add, sizeof(add));
if(a==WSAECONNREFUSED || a==WSAETIMEDOUT)
{
cout<<"没连上"<<endl;
return;
} CFile file(L"C:\\和兰花在一起.mp3", CFile::typeBinary);
ULONGLONG FileLength = file.GetLength(); char * size = new char[100];
::ZeroMemory(size,100);
sprintf(size, "%i64", FileLength);
cout<<" size: "<<size<<endl;
send(s1, size, 10, 0); while(1)
{
char *buf;
if( FileLength >= 1000 )
{
buf = new char[1000];
file.Read(buf, 1000);
send(s1, buf, 1000, 0);
FileLength -= 1000;
}
else if( FileLength == 0 )
break;
else
{
buf = new char[FileLength];
file.Read(buf, FileLength );
send(s1, buf, FileLength, 0);
break;
}
}

}

解决方案 »

  1.   

    接收的时候
    if( nSize >= 1000)
            {
                buf = new char[1000];
                recv(s1, buf, 1000, 0);
                file.Write(buf, 1000);
                nSize -= 1000;
            }
    你这个位置每次收到的不一定是1000
    通过recv的返回值决定file.Write写入多少别的地方没仔细看
    你先检查一下这个吧
    你每次的认为收到了1000
    但是其实都小于1000
    然后又按1000写
    文件自然就大了
      

  2.   

    改完以后 CFile 跑出个未指明原因的异常。CFileException::m_cause 等于genericException
    while(1)
    {
    char * buf;
    if( nSize )
    {
    buf = new char[1000];
    int r = recv(s2, buf, 1000, 0);
    file.Write(buf, r);
    nSize -= r;
    }
    else
    break;
    }
    while(1)
    {
    char *buf;
    if( FileLength  )
    {
    buf = new char[1000];
    UINT r = file.Read(buf, 1000);
    send(s1, buf, r, 0);
    FileLength -= r;
    }
    else
    break;
    }
      

  3.   

    上面应该是在 file.Wriet 的最后一次抛出的异常。因为我设断点,用F10执行好多次循环都没问题