【问题标题】:Delphi Indy 10 Disconnection issueDelphi Indy 10 断线问题
【发布时间】:2013-05-05 23:38:35
【问题描述】:

我在做什么: 我的服务器通过上下文列表发送数据,并在锁定到特定客户端后发送该客户端数据,并期待我们在此连接计时器中处理的回复。 我知道使用线程会是更好的方法,但这就是我所采用的。

这是我的问题: 我请求获取连接信息,它工作正常,套接字写入行 getstring 数据 1 到 8 但是当我写“你好”时它没有响应任何数据它只是断开客户端。

如果您在 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;

【问题讨论】:

  • 您使用WriteLn() 回复"Get Connection Info",但使用Write() 回复其他回复。服务器是否希望每次回复都换行?

标签: delphi tcp client indy indy10


【解决方案1】:

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 套接字时遇到随机断开问题的人,哈哈,这通常是程序员的错误,而不是他或她选择使用的组件集。

如果这个示例代码不可读,我很抱歉这是我第一次在这个网站上发帖,我相信在以后的帖子中我会努力正确格式化我的 cmets 等等...等等...

另外,我想为任何试图在客户端接收此类数据的人补充一点,最好创建自己的后台工作线程来监听来自 TidTCPserver 套接字的传入连接。我知道计时器是不好的编程习惯,事实上我和我的朋友很快就会将它切换到一个单独的线程。

Remy 再次感谢您的快速回复!非常感谢先生。

【讨论】:

  • 我知道这条评论太晚了,哈哈,但是是的,为了解决这个问题,代码是粗鲁的,我们的实际代码要好得多,我们在最终产品中使用了线程保存队列列表和各种东西: )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 2011-08-16
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
相关资源
最近更新 更多