bestlong 怕失憶論壇

標題: 利用 TCP/IP 作檔案續傳 [打印本頁]

作者: bestlong    時間: 2006-6-23 17:24     標題: 利用 TCP/IP 作檔案續傳

利用 TWinSocketStream 可以做到
  1. var
  2.   TServerWinSocket serverSocket;
  3.   TClientWinSocket clientSocket;
  4.   TFileStream inFile, outFile;
  5.   // 假設各 Stream 都已 created

  6. // 在 Server 端
  7. // Client send 一個命令告知 Server 要由 536 bytes 開始傳
  8. inFile.Position := 536;
  9. wtih TWinSocketStream.Create(serverSocket, 60000) do
  10. begin
  11.   CopyFrom(inFile, inFile.Size - inFile.Position + 1);
  12.   Free;
  13. end;

  14. // 在 Client 端 OnReadEvent 中
  15. memStream = TMemoryStream.Create;
  16. memStream.Size = TOTAL_FILE_SIZE;
  17. memStream.Position := 0;
  18. socketStream := TWinSocketStream.Create(clientSocket, 60000) do
  19. if socketStream.WaitForData(60000) then // give the client 60 seconds writing
  20.   socketStream.Read(PCharArray(memStream.Memory)^, TOTAL_FILE_SIZE)
  21. memStream.Position := 0;
  22. outFile.Position := 536;
  23. outFile.CopyFrom(memStream);

  24. // 释放全部的对象
複製代碼





歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/) Powered by Discuz! X1.5