tcp - Delphi Indy 10 Disconnection issue -


what doing: server sends data going through context list , after locks on specific client sends client data, , expects reply handle in connection timer. understand using thread better way went with.

here problem: request connection info , works fine, socket writes line getstring data 1 throught 8 when write 'hello' not respond data disconnects client.

any appreciated been trying figure out 2 days or so. in advance!

here code:

procedure tform1.commandtimertimer(sender: tobject); var sl:tstringlist; command: string; begin   if clientsocket.iohandler.inputbufferisempty   begin     clientsocket.iohandler.checkfordataonsource(10);     if clientsocket.iohandler.inputbufferisempty exit;   end;   command := clientsocket.iohandler.readln();   sl:= tstringlist.create;   sl.delimiter:= '|';   sl.strictdelimiter:=true;   sl.delimitedtext:= command;   if sl[0] = 'did data'   begin    showmessage('yea got data');   end;   if sl[0] = 'broadcasted message' begin clientsocket.iohandler.write('data'); showmessage(sl[1]); // data sent server client, in order manage data have created tstringlist , delimited out '|'.  end; if sl[0] = 'hello' begin   clientsocket.iohandler.write('data'); end; if sl[0] = 'get connection info' begin // 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' begin commandtimer.enabled:=false; connectiontimer.enabled:=true; // timer checks , keeps tidtcpclientsocket connected server reverse connection protocol. exit; end; commandtimer.enabled:=true; end; 

remy thank quick reply, friend , working on project , shortly after posting question both determined true issue having , have fixed it!

the fixed code needs it:

client side:(tidtcpclient)

     procedure tform1.commandtimertimer(sender: tobject);         var         sl:tstringlist;         command: string;         begin             if clientsocket.iohandler.inputbufferisempty         begin             clientsocket.iohandler.checkfordataonsource(10);             if clientsocket.iohandler.inputbufferisempty exit;           end;           command := clientsocket.iohandler.readln();           sl:= tstringlist.create;           sl.delimiter:= '|';           sl.strictdelimiter:=true;           sl.delimitedtext:= command;           if sl[0] = 'get connection info'           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' begin showmessage('i got hello'); clientsocket.iohandler.writeln('some data'); end; if sl[0] = 'ping!' 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;  // tlistview 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' 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 add items listview component go our own ping method.  allow me show problem was:   recived commands based on first string received clientsocket ie:  if sl[0] = 'command sent client' begin  end;  , kept going on , on , on ie:   if sl[0] = 'command sent client' begin  end; if sl[0] = 'command sent client' begin  end;  problem cannot allow code run on , on otherwise socket hangs , disconnects.  fix simple   example:  if sl[0] = 'command sent client' begin // have in instance of receiving command , exit sub / procedure. exit; end;  if sl[0] = 'command sent client' begin // got in here exit; // don't forget exit statement or code runs on , hang socket.  end; 

i hope example helps else whom having issues random disconnections while using indy sockets, lol programmers fault not component set or choosing use.

i'm sorry if example code unreadable first time posting on site , i'm sure in future posts strive format comments ect... ect...

also add whom trying receive data on client side best best create own background worker thread listen incoming connections tidtcpserver socket. understand timer bad programming practice , in fact friend , switch on separate thread soon.

remy once again think quick reply! appreciated sir.


Comments