為何我的TIDUDPCLIENT 或 TIDUDPSERVER 無法執行 SENDBUFFER ? |
尚未結案
|
adamchen
一般會員 發表:4 回覆:3 積分:1 註冊:2007-10-19 發送簡訊給我 |
但執行SEND O.K.
,因我不只要傳文字檔 procedure TForm1.Button5Click(Sender: TObject); var testmem : TMemoryStream ; i,L : integer; s : String; Buffer: array[0..1024] of Byte; begin i := 12345; s := 'ABCDEFG'; L := Length(s); // tstream.read; testmem.Clear; testmem := TMemoryStream.Create; testmem.WriteBuffer(i,Sizeof(i)); testmem.WriteBuffer(L,Sizeof(L)); testmem .WriteBuffer(s[1],L); IdUDPClient1.Active := false; IdUDPClient1.Active := true; IdUDPServer1.Active := false; IdUDPServer1.Active := True; IdUDPClient1.BroadcastEnabled := True; IdUDPSERVER1.BroadcastEnabled := True; testmem.Position := 0; If (testmem.Size > IdUDPClient1.BufferSize) Then Begin IdUDPClient1.SendBuffer('192.168.1.219',9001,testmem.Memory^,IdUDPClient1.BufferSize) <---錯誤 end Else IdUDPClient1.SendBuffer('192.168.1.219',9001,testmem.Memory^,testmem.Size);<---錯誤 testmem.Free; end; |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
testmem.Clear; // testmem還沒Create testmem := TMemoryStream.Create; 這裡就應該出錯了 ^o^ //-------------------------------------------------------- 以下程式碼供你參考(使用D7) 測試碼下載: http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=90904 另外也可以參考這一篇, 關於封包的做法 ^^... http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=90517 [code delphi] unit fMain; interface uses Forms, Controls, Classes, StdCtrls, IdUDPClient, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle, ExtCtrls; type TRec = packed record Time: TDateTime; Index: Integer; Len: Integer; Str: string; end; TForm1 = class(TForm) server: TIdUDPServer; client: TIdUDPClient; btnActive: TButton; ListBox1: TListBox; timer: TTimer; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure timerTimer(Sender: TObject); procedure serverUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle); procedure btnActiveClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private FActive: Boolean; procedure SetActive(const Value: Boolean); public property Active: Boolean read FActive write SetActive; end; var Form1: TForm1; implementation uses SysUtils; {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Randomize; timer.Enabled := False; //Initial UDP Server server.DefaultPort := 12345; //Initail UDP Client client.Host := '127.0.0.1'; client.Port := 12345; end; procedure TForm1.btnActiveClick(Sender: TObject); begin Active := not Active; end; procedure TForm1.SetActive(const Value: Boolean); begin if Value <> FActive then begin FActive := Value; server.Active := FActive; client.Active := FActive; timer.Enabled := FActive; if FActive then btnActive.Caption := '停止' else btnActive.Caption := '啟動'; end; end; procedure TForm1.timerTimer(Sender: TObject); var r: TRec; C: Integer; M: TMemoryStream; begin M := TMemoryStream.Create; try r.Time := Now; r.Index := Random(MaxInt); r.Str := ''; for C := 0 to 4 do r.Str := r.Str Format('%d-', [Random(MaxInt)]); r.Len := Length(r.Str); with r do begin M.Write(Time, SizeOf(Time)); M.Write(Index, SizeOf(Index)); M.Write(Len, SizeOf(Len)); M.Write(PChar(Str)^, Len); end; client.SendBuffer(M.Memory^, M.Size); finally FreeAndNil(M); end; end; procedure TForm1.serverUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle); var r: TRec; X: string; begin with AData do begin Read(r.Time, SizeOf(r.Time)); Read(r.Index, SizeOf(r.Index)); Read(r.Len, SizeOf(r.Len)); if r.Len > 0 then begin SetLength(r.Str, r.Len); Read(PChar(r.Str)^, SizeOf(r.Len)); end; X := FormatDateTime('YYYY-MM-DD, hh:nn:ss.zzz', r.Time) ' ^ ' Format('.12d', [r.Index]) ' ^ ' Format('%4.4d', [r.Len]) ' ^ ' r.Str; ListBox1.Items.Add(X); if ListBox1.Items.Count > 20 then ListBox1.Items.Delete(0); end; Label1.Caption := Format('Length of Received Data: %d', [AData.Size]); end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin Active := False; end; end. [/code] |
adamchen
一般會員 發表:4 回覆:3 積分:1 註冊:2007-10-19 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
(1)
testmem.Clear; testmem := TMemoryStream.Create; 前面指出的錯誤要先修正, testmem還沒產生 就叫用它的method, 一定會產生exception, 除非 是它宣告成 class function. (2)找不到rtl60.bpl? 取消專案中build with runtime packages. 也檢查你的路徑設定... (3)ServerUDPRead method references by server.OnUDPRead has an incompatible parameterlist.Remove the reference? 這個訊息指的就是版本不一致, 因為兩的版本 OnUDPRead 中所宣告的參數不一樣,我用的 是D7內含的元件. (4)另外, 建議你先建立一個可以正常執行的版本, 再來測試其他function. |
adamchen
一般會員 發表:4 回覆:3 積分:1 註冊:2007-10-19 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
===================引 用 adamchen 文 章=================== 謝謝... 我的是Indy 10版本,不知如何下SendBuffer?? 呵...我也是今天才知道原來在Indy10中, SendBuffer()已經改版了....^_^ http://delphi.ktop.com.tw/board.php?cid=30&fid=67&tid=91170 另如何取消專案的 build with runtime packages?? |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |