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

關於 Indy TCPClient 接收資料

答題得分者是:alikering
blue
中階會員


發表:169
回覆:136
積分:81
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2016-07-26 17:52:38 IP:124.219.xxx.xxx 訂閱
Hi,各位先進大家好:
一個關於 Indy TCPClient 等待資料的問題請教,
如下程式是一個註冊程式程式對主機192.168.50.5送出註冊資料並等侯,若主機有發現與我配對的將回傳資訊給我,
實測後怪怪的,當無配對時會從 if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit; 退出這部份沒問題,
可是當有配對時,應該要能接收到資料,可是顥示完 'Received data 後,在 IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);就Time out了,使用Wireshark 看確實是有資料回來,是我的用法有錯嗎?
還請各位先進指導一下,謝謝!

procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: TIdBytes;
BufferSize: Integer;

i: Integer;
begin
IdTCPClient1.Host := '192.168.50.5';
IdTCPClient1.Port := 8080;
IdTCPClient1.ConnectTimeout := 5000;
IdTCPClient1.ReadTimeout := 5000;
BufferSize := 4 512; // 512 as specified by RFC 1350
SetLength(Buffer, Length(MSG_BOX_REG));
Move(MSG_BOX_REG[0], Buffer[0], Length(MSG_BOX_REG));
Buffer := Buffer ToBytes('MyID,20150514010101,');

try
if IdTCPClient1.Connected then begin
IdTCPClient1.Disconnect;
IdTCPClient1.IOHandler.Free;
end;

IdTCPClient1.Connect();

IdTCPClient1.IOHandler.Write(Buffer);

_Log('wait Received data ');
i := 0;
while ((i < 10) and IdTCPClient1.IOHandler.InputBufferIsEmpty) do begin
IdTCPClient1.IOHandler.CheckForDataOnSource(1000);
inc(i);
end;
if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit;
_Log('Received data ');
IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);
_Log('Received data -1');
if length(buffer) > 0 then begin
_Log('Received data , length > 0');
if ByteInArrays(MSG_PAIRING, Buffer) then
ShowMessage('RegOK');
end;
except
on E : Exception do
_Log(E.ClassName ' error raised, with message : ' E.Message);
end;

alikering
一般會員


發表:8
回覆:11
積分:8
註冊:2006-08-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2016-07-27 01:48:03 IP:1.171.xxx.xxx 訂閱
Time Out 應該要去檢查Server端怎麼沒有傳送資料給Client




===================引 用 blue 文 章===================
Hi,各位先進大家好:
一個關於 Indy TCPClient 等待資料的問題請教,
如下程式是一個註冊程式程式對主機192.168.50.5送出註冊資料並等侯,若主機有發現與我配對的將回傳資訊給我,
實測後怪怪的,當無配對時會從 if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit; 退出這部份沒問題,
可是當有配對時,應該要能接收到資料,可是顥示完 'Received data 後,在 IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);就Time out了,使用Wireshark 看確實是有資料回來,是我的用法有錯嗎?
還請各位先進指導一下,謝謝!

procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: TIdBytes;
BufferSize: Integer;

i: Integer;
begin
IdTCPClient1.Host := '192.168.50.5';
IdTCPClient1.Port := 8080;
IdTCPClient1.ConnectTimeout := 5000;
IdTCPClient1.ReadTimeout := 5000;
BufferSize := 4 512; // 512 as specified by RFC 1350
SetLength(Buffer, Length(MSG_BOX_REG));
Move(MSG_BOX_REG[0], Buffer[0], Length(MSG_BOX_REG));
Buffer := Buffer ToBytes('MyID,20150514010101,');

try
if IdTCPClient1.Connected then begin
IdTCPClient1.Disconnect;
IdTCPClient1.IOHandler.Free;
end;

IdTCPClient1.Connect();

IdTCPClient1.IOHandler.Write(Buffer);

_Log('wait Received data ');
i := 0;
while ((i < 10) and IdTCPClient1.IOHandler.InputBufferIsEmpty) do begin
IdTCPClient1.IOHandler.CheckForDataOnSource(1000);
inc(i);
end;
if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit;
_Log('Received data ');
IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);
_Log('Received data -1');
if length(buffer) > 0 then begin
_Log('Received data , length > 0');
if ByteInArrays(MSG_PAIRING, Buffer) then
ShowMessage('RegOK');
end;
except
on E : Exception do
_Log(E.ClassName ' error raised, with message : ' E.Message);
end;

blue
中階會員


發表:169
回覆:136
積分:81
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2016-07-27 09:25:54 IP:122.146.xxx.xxx 訂閱
Hi,
感謝 alikering 先進的指導,
但是小弟從主機及用戶端的監控都確資料有回來,
而是用戶端沒有接收,
小弟先用一個TCPServer來取代原來的主機端程式並直接回覆,用戶端確實是正常收到了,
小弟猜想應該是註冊主機在回傳時並非是使用原來用戶端所建立的那個連線,
所以雖然是相同Port,用戶端的TCPClient不會接收,
不知道用戶端改用 TCPServer 是否有幫助(但.目前尚未查到該如何用TCPServer先發起通訊)?
或是各位先進有其他想法?
謝謝!

===================引 用 alikering 文 章===================
Time Out 應該要去檢查Server端怎麼沒有傳送資料給Client




===================引 用 blue 文 章===================
Hi,各位先進大家好:
一個關於 Indy TCPClient 等待資料的問題請教,
如下程式是一個註冊程式程式對主機192.168.50.5送出註冊資料並等侯,若主機有發現與我配對的將回傳資訊給我,
實測後怪怪的,當無配對時會從 if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit; 退出這部份沒問題,
可是當有配對時,應該要能接收到資料,可是顥示完 'Received data 後,在 IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);就Time out了,使用Wireshark 看確實是有資料回來,是我的用法有錯嗎?
還請各位先進指導一下,謝謝!

procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: TIdBytes;
BufferSize: Integer;

i: Integer;
begin
IdTCPClient1.Host := '192.168.50.5';
IdTCPClient1.Port := 8080;
IdTCPClient1.ConnectTimeout := 5000;
IdTCPClient1.ReadTimeout := 5000;
BufferSize := 4 512; // 512 as specified by RFC 1350
SetLength(Buffer, Length(MSG_BOX_REG));
Move(MSG_BOX_REG[0], Buffer[0], Length(MSG_BOX_REG));
Buffer := Buffer ToBytes('MyID,20150514010101,');

try
if IdTCPClient1.Connected then begin
IdTCPClient1.Disconnect;
IdTCPClient1.IOHandler.Free;
end;

IdTCPClient1.Connect();

IdTCPClient1.IOHandler.Write(Buffer);

_Log('wait Received data ');
i := 0;
while ((i < 10) and IdTCPClient1.IOHandler.InputBufferIsEmpty) do begin
IdTCPClient1.IOHandler.CheckForDataOnSource(1000);
inc(i);
end;
if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit;
_Log('Received data ');
IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);
_Log('Received data -1');
if length(buffer) > 0 then begin
_Log('Received data , length > 0');
if ByteInArrays(MSG_PAIRING, Buffer) then
ShowMessage('RegOK');
end;
except
on E : Exception do
_Log(E.ClassName ' error raised, with message : ' E.Message);
end;

blue
中階會員


發表:169
回覆:136
積分:81
註冊:2002-04-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2016-07-28 08:44:38 IP:122.146.xxx.xxx 訂閱
Hi,感謝各位先進,
應該說小弟需要的是在 TIdTCPClient中俱有類似 TIdTCPServer 中 TIdListenerThread來負責監聽與出去相同埠位的程序,
不知道各位先進是否有相關的資料可以指導,
謝謝!

===================引 用 blue 文 章===================
Hi,
感謝 alikering 先進的指導,
但是小弟從主機及用戶端的監控都確資料有回來,
而是用戶端沒有接收,
小弟先用一個TCPServer來取代原來的主機端程式並直接回覆,用戶端確實是正常收到了,
小弟猜想應該是註冊主機在回傳時並非是使用原來用戶端所建立的那個連線,
所以雖然是相同Port,用戶端的TCPClient不會接收,
不知道用戶端改用 TCPServer 是否有幫助(但.目前尚未查到該如何用TCPServer先發起通訊)?
或是各位先進有其他想法?
謝謝!

===================引 用 alikering 文 章===================
Time Out 應該要去檢查Server端怎麼沒有傳送資料給Client




===================引 用 blue 文 章===================
Hi,各位先進大家好:
一個關於 Indy TCPClient 等待資料的問題請教,
如下程式是一個註冊程式程式對主機192.168.50.5送出註冊資料並等侯,若主機有發現與我配對的將回傳資訊給我,
實測後怪怪的,當無配對時會從 if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit; 退出這部份沒問題,
可是當有配對時,應該要能接收到資料,可是顥示完 'Received data 後,在 IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);就Time out了,使用Wireshark 看確實是有資料回來,是我的用法有錯嗎?
還請各位先進指導一下,謝謝!

procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: TIdBytes;
BufferSize: Integer;

i: Integer;
begin
IdTCPClient1.Host := '192.168.50.5';
IdTCPClient1.Port := 8080;
IdTCPClient1.ConnectTimeout := 5000;
IdTCPClient1.ReadTimeout := 5000;
BufferSize := 4 512; // 512 as specified by RFC 1350
SetLength(Buffer, Length(MSG_BOX_REG));
Move(MSG_BOX_REG[0], Buffer[0], Length(MSG_BOX_REG));
Buffer := Buffer ToBytes('MyID,20150514010101,');

try
if IdTCPClient1.Connected then begin
IdTCPClient1.Disconnect;
IdTCPClient1.IOHandler.Free;
end;

IdTCPClient1.Connect();

IdTCPClient1.IOHandler.Write(Buffer);

_Log('wait Received data ');
i := 0;
while ((i < 10) and IdTCPClient1.IOHandler.InputBufferIsEmpty) do begin
IdTCPClient1.IOHandler.CheckForDataOnSource(1000);
inc(i);
end;
if IdTCPClient1.IOHandler.InputBufferIsEmpty then Exit;
_Log('Received data ');
IdTCPClient1.IOHandler.ReadBytes(Buffer, 512, False);
_Log('Received data -1');
if length(buffer) > 0 then begin
_Log('Received data , length > 0');
if ByteInArrays(MSG_PAIRING, Buffer) then
ShowMessage('RegOK');
end;
except
on E : Exception do
_Log(E.ClassName ' error raised, with message : ' E.Message);
end;

blue
中階會員


發表:169
回覆:136
積分:81
註冊:2002-04-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2016-07-29 15:45:28 IP:122.146.xxx.xxx 訂閱
Hi,目前改用 TClientSocket 解決這個問題,
謝謝各位先進!
系統時間:2024-04-26 8:27:44
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!