Remy Thank you for the quick reply, My friend and I are working on this project together and shortly after posting this question we both quickly determined the true issue we where having and we have Fixed it!
The Fixed code for anyone who needs it:
Client Side:(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;
I hope this Example helps anyone else whom is having issues with random disconnections while using indy sockets, lol its usually the programmers fault not the component set he or she is choosing to use.
I'm sorry if this Example code is unreadable this is my first time posting on this site and I'm sure that in future posts I will strive to properly format my comments Ect... Ect...
Also I would like to add for anyone whom is trying to receive data like this on the client side that it would be best best to create your own background worker thread to listen for incoming connections from a TidTCPserver socket. I understand that the timer is bad programming practice and in fact my friend and I will switch it over to a separate thread soon.
Remy once again I think you for your quick reply! Much appreciated sir.