我试过的
可以的啊
给你一个用SOCKET传送大文件的例子
用C++ Builder实现的,和Delphi一样
传送过程:
1.Client向Server传送文件名;
2.Server向Client传送文件大小;
3.Client向Server传送"GetFile";
4.开始传送;
由于前文所述的原因,我每次只写入10M,程序如下:
//发送端
void __fastcall TForm1::sckServerClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString strData;
if(SendFile){
strData = Socket->ReceiveText();
Label1->Caption = "传送文件";
int bufSize, byteSend, byteTotalSend;
byteTotalSend = 0;
while(byteTotalSend < srcFileSize){
if (srcFileSize <= SendBlock * SendCount){ //分段传输
bufSize = srcFileSize - SendBlock * (SendCount-1);
buf = new char[bufSize];
FileRead(srcFileHandle,buf,bufSize);
do{
Application->ProcessMessages();
byteSend = Socket->SendBuf(buf,bufSize);
}while(byteSend == -1);
byteTotalSend += byteSend;
delete buf;
FileClose(srcFileHandle);
SendFile = false;
}
else{
bufSize = SendBlock; //SendBlock = 10M
buf = new char[bufSize];
FileRead(srcFileHandle,buf,bufSize);
do{
Application->ProcessMessages();
byteSend = Socket->SendBuf(buf,bufSize);
}while(byteSend == -1);
byteTotalSend +=byteSend;
delete buf;
SendCount++;
}
}
}
else{
int byteRead;
strData = Socket->ReceiveText();
srcFileHandle = FileOpen(strData,fmOpenRead);
srcFileSize = FileSeek(srcFileHandle,0,2) + 1;
FileSeek(srcFileHandle,0,0); //回到文件起始位置;
strData = IntToStr(srcFileSize);
Socket->SendText(strData);
SendCount = 1;
SendFile = true;
}
}
//接收端
void __fastcall TForm1::sckClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
if(GetSrcFileSize){
AnsiString strData;
strData = Socket->ReceiveText();
fSize = StrToInt(strData);
desFileHandle = FileCreate("Recieved.zip");
ByteRecievedTotal = 0;
GetSrcFileSize = false;
Socket->SendText("GetFile");
}
else{
char *buf;
int ByteToRecieve, ByteRecieve, ByteRecieved;
ByteRecieved = 0;
ByteToRecieve = Socket->ReceiveLength();
buf = new char[ByteToRecieve];
ByteRecieved = Socket->ReceiveBuf(buf,ByteToRecieve);
FileWrite(desFileHandle,buf,ByteRecieved);
delete buf;
ByteRecievedTotal += ByteRecieved;
Label1->Caption = IntToStr(ByteRecievedTotal);
if (ByteRecievedTotal == fSize){
FileClose(desFileHandle);
MessageBox(Handle,"接收完成","接收文件",MB_OK);
}
}
}