全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1612
推到 Plurk!
推到 Facebook!

socket接收文件的問題

答題得分者是:malanlk
takdick
一般會員


發表:50
回覆:63
積分:22
註冊:2002-08-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-02 17:45:13 IP:203.218.xxx.xxx 未訂閱
我想用socket接收文件,服務端已得到要接收文件的大小和儲存路徑,但接收文件內容的代碼不知該如何寫?我知道使用記錄類型會較方便,但想知道在不使用的情況下又該如何處理?謝謝. 客戶端 filesize:longint; filename,shortname:string; m,ms1:tmemorystream; procedure TForm1.uploadClick(Sender: TObject); begin if opendialog1.Execute then begin filename:=opendialog1.FileName; shortname:=ExtractFileName(filename); ms1:=tmemorystream.Create; ms1.Position:=0; ms1.LoadFromFile(filename); filesize:=ms1.Size; if filesize >8388608 then begin application.MessageBox('文件>8M,無法上傳','提示'); ms1.Free; exit; end; ss.Socket.Connections[0].SendText('send'); end; end; procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket); var str:string; begin str:=socket.ReceiveText; if str='size' then Socket.SendText('size' inttostr(filesize)); if str='name' then Socket.SendText('name' filename '.bak'); if str='data' then Socket.SendStream(ms1); if str='okok' then begin ss.Close; showmessage('file send success.'); end; end; 服務端 procedure TForm1.cs2Read(Sender: TObject; Socket: TCustomWinSocket); var ms:tmemorystream; len,filesize:longint; str,filename,filepath,head,tail:string; buffer:array [0..10000] of byte; begin str:=Socket.ReceiveText; head:=copy(str,1,5); tail:=copy(str,6,length(str)-5); if head='send' then begin Socket.SendText('size'); end; if head='size' then begin filesize:=strtoint(tail); Socket.SendText('name'); end; if head='name' then //已收到要接收文件的大小和儲存路徑 begin filename:=tail; Socket.SendText('data'); end; len:=socket.ReceiveLength; //請問以下接收文件的代碼該如何寫?還要加些什麼才能正常接收文件? socket.ReceiveBuf(buffer,len); ms:=tmemorystream.Create; ...... ...... ...... ms.SaveToFile(filename); cs.Socket.SendText('okok'); cs.Close; end; end;
malanlk
尊榮會員


發表:20
回覆:694
積分:577
註冊:2004-04-19

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-04 13:37:59 IP:61.219.xxx.xxx 未訂閱
http://delphi.ktop.com.tw/topic.php?topic_id=21606 這篇有你要的答案 不過你的程式碼要記得 ms.Free... 請問delphi的ClientSocket跟ServeSocket傳檔的問題 http://delphi.ktop.com.tw/topic.php?topic_id=28129 參考參考 網際網路傳檔程式範例(Source) 修正版 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=27613 發表人 - malanlk 於 2005/09/04 13:39:39
系統時間:2024-05-02 11:23:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!