用TCP/IP做文件续传
【打印文章】
利用 TWinSocketStream 可以做到.
TServerWinSocket serverSocket;
TClientWinSocket clientSocket
TFileStream inFile, outFile;
// 假设各 Stream 都己 created
// 在 Server 端
// Client send 一个命令告知 Server 要由 536 bytes 开始传
inFile.Position := 536;
wtih TWinSocketStream.Create(serverSocket, 60000) do
begin
CopyFrom(inFile, inFile.Size - inFile.Position + 1);
Free;
end;
// 在 Client 端 OnReadEvent 中
memStream = TMemoryStream.Create;
memStream.Size = TOTAL_FILE_SIZE;
memStream.Position := 0;
socketStream := TWinSocketStream.Create(clientSocket, 60000) do
if socketStream.WaitForData(60000) then // give the client 60 seconds writing
socketStream.Read(PCharArray(memStream.Memory)^, TOTAL_FILE_SIZE)
memStream.Position := 0;
outFile.Position := 536;
outFile.CopyFrom(memStream);
// 释放全部的对象
TServerWinSocket serverSocket;
TClientWinSocket clientSocket
TFileStream inFile, outFile;
// 假设各 Stream 都己 created
// 在 Server 端
// Client send 一个命令告知 Server 要由 536 bytes 开始传
inFile.Position := 536;
wtih TWinSocketStream.Create(serverSocket, 60000) do
begin
CopyFrom(inFile, inFile.Size - inFile.Position + 1);
Free;
end;
// 在 Client 端 OnReadEvent 中
memStream = TMemoryStream.Create;
memStream.Size = TOTAL_FILE_SIZE;
memStream.Position := 0;
socketStream := TWinSocketStream.Create(clientSocket, 60000) do
if socketStream.WaitForData(60000) then // give the client 60 seconds writing
socketStream.Read(PCharArray(memStream.Memory)^, TOTAL_FILE_SIZE)
memStream.Position := 0;
outFile.Position := 536;
outFile.CopyFrom(memStream);
// 释放全部的对象
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】