簡體   English   中英

Delphi Indy 10斷開連接問題

[英]Delphi Indy 10 Disconnection issue

我正在做什么:我的服務器通過上下文列表發送數據,並鎖定到特定的客戶端后,它發送該客戶端數據,並期望我們在此連接計時器中處理答復。 我知道使用線程是執行此操作的更好方法,但這就是我所追求的。

這是我的問題:我請求獲取連接信息,並且工作正常,套接字將行getstring數據寫入1至8,但是當我寫“ Hello”時,它不響應任何數據,只會斷開客戶端連接。

試圖解決2天左右的任何幫助將不勝感激。 提前致謝!

這是我的代碼:

procedure TForm1.CommandTimerTimer(Sender: TObject);
var
sl:Tstringlist;
Command: String;
begin
  if clientsocket.IOHandler.InputBufferIsEmpty then
  begin
    clientsocket.IOHandler.CheckForDataOnSource(10);
    if clientsocket.IOHandler.InputBufferIsEmpty then Exit;
  end;
  Command := clientsocket.IOHandler.ReadLn();
  sl:= Tstringlist.Create;
  sl.Delimiter:= '|';
  sl.StrictDelimiter:=true;
  sl.DelimitedText:= Command;
  if sl[0] = 'did i get data' then
  begin
   showmessage('Yea I got Data');
  end;


if sl[0] = 'BroadCasted Message' then
begin
Clientsocket.IOHandler.write('data');
showmessage(sl[1]); // This is data sent from my server to this client, in order to manage the data I have created a Tstringlist and delimited it out by '|'. 
end;
if sl[0] = 'hello' then
begin
  clientsocket.IOHandler.write('data');
end;
if sl[0] = 'Get Connection Info' then
begin
// This part
clientsocket.IOHandler.writeln('Newconnection' + '|' + 'string data 1'  + '|' + 'string data 2' + '|' + 'string data 3' + '|'+ 'string data 4'+'|' + 'string data 5'+'|'+'string data 6'+'|'+'string data 7'+'|'+'string data 8');
end;

if sl[0] = 'Reconnect' then
begin
commandtimer.Enabled:=false;
Connectiontimer.Enabled:=true; // This is a timer that constantly checks and keeps the TidTCPclientsocket connected with my server as this is a Reverse connection Protocol.
Exit;
end;
commandtimer.enabled:=true;
end;

Remy感謝您的快速答復,我和我的朋友正在共同努力進行這個項目,發布此問題后不久,我們倆都迅速確定了我們所遇到的真正問題,並已修復它!

需要的人的固定代碼:

客戶端:(Tidtcpclient)

     procedure TForm1.CommandTimerTimer(Sender: TObject);
        var
        sl:Tstringlist;
        Command: String;
        begin
            if clientsocket.IOHandler.InputBufferIsEmpty then
        begin
            clientsocket.IOHandler.CheckForDataOnSource(10);
            if clientsocket.IOHandler.InputBufferIsEmpty then Exit;
          end;
          Command := clientsocket.IOHandler.ReadLn();
          sl:= Tstringlist.Create;
          sl.Delimiter:= '|';
          sl.StrictDelimiter:=true;
          sl.DelimitedText:= Command;
          if sl[0] = 'Get Connection Info' then
          begin
      clientsocket.IOHandler.writeln('String Data 1' + '|' + 'String Data 2'+ '|' + 'String     Data 3'+ '|' + 'String Data 4' + '|'+ 'String Data 5'+'|' + 'String Data 6'+'|'+'String Data 7'+'|'+'String Data 8'+'|'+'String Data 9' + '|' + 'String Data 10' + '|' + 'String Data 11');
end
else
if sl[0] = 'hello' then
begin
showmessage('I got Hello');
clientsocket.IOHandler.writeln('Some Data');
end;
if sl[0] = 'PING!' then
begin
clientsocket.IOHandler.WriteLn('PINGBACK!');
end;

commandtimer.enabled:=true;
end;

Server Side: (TidTCPserver) OnExecute Event Procedure

procedure TForm1.ServersocketExecute(AContext: TIdContext);
Var
sl:Tstringlist;
listitem:Tlistitem;  // This is for a Tlistview where we are adding connection information
Data:String;
Begin
data:= acontext.connection.iohandler.readln(); 
sl:= Tstringlist.create;
sl.delimiter:= '|';
sl.delimitedtext:= data;

    If sl[0] = 'String Data 1' then
begin
listitem:= listview1.items.add;
listitem.caption:= acontext.binding.peerip;
listitem.subitems.add(sl[2]);
listitem.subitems.add(sl[3]);
listitem.subitems.add(sl[4]);
listitem.subitems.add(sl[5]);
listitem.subitems.add(sl[6]);
listitem.subitems.add(sl[7]);
listitem.subitems.add(sl[8]);
listitem.subitems.add(sl[9]);
listitem.subitems.add(sl[10]);

after we add items to the listview component we then go into our own ping method.

Allow me to show everyone what the problem was: 

we recived commands based on the FIRST string received by the clientsocket IE:

If sl[0] = 'Command sent by Client' then
begin

end;

and we kept it going on and on and on IE: 

If sl[0] = 'Command sent by Client' then
begin

end;
If sl[0] = 'Command sent by Client' then
begin

end;

The problem with this is that we cannot allow this code to run on and on like this otherwise the socket hangs and disconnects.

The fix was Simple   Example:

If sl[0] = 'Command sent by Client' then
begin
// Do what we have to do in this instance of receiving a Command and then Exit the sub / Procedure.
exit;
end;

If sl[0] = 'Command sent by Client' then
begin
// do what we got to do in here
Exit; // DON'T forget the exit statement or code runs on and will hang the socket. 
end;

我希望這個示例可以幫助使用indy套接字時出現隨機斷開連接問題的其他任何人,這通常是程序員認為不是他或她選擇使用的組件集的錯誤。

很抱歉,如果此示例代碼不可讀,這是我第一次在該網站上發布文章,並且我相信在以后的文章中,我將努力正確地設置我的評論格式等等。

另外,我想向嘗試在客戶端接收此類數據的任何人添加一個最好的方法,那就是最好創建您自己的后台工作線程,以偵聽來自TidTCPserver套接字的傳入連接。 我知道計時器是不好的編程習慣,實際上我的朋友和我會盡快將其切換到單獨的線程。

雷米,我想再次感謝您的快速回復! 非常感謝先生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM