I use TIdTCPServer and the following code to read client input :
repeat
cl3:=cl3+AContext.Connection.IOHandler.ReadLnSplit(WasSplit,#0,-1,-1,TEncoding.UTF8);
until not WasSplit;
However if client is connected to the server and I close the server it raises an exception class (EIdNotConnected) whith message ‘Not connected’.
If I use ReadLn instead ReadLnSplit no exception raises.
What causes this exception and how could I prevent it?
I suppose the solution is simple but I am new to sockets and Indy and I cant figure it out.
Thanks in advance.
What is the actual problem? When you close the server, it is supposed to make active reading/writing operations raise an exception. That is normal behavior for Indy.
ReadLn()is just as likely to raise an exception asReadLnSplit()is. Indy relies on exceptions for its internal notifications. Just let the server handle the exception for you, so it can terminate and cleanup the thread that is managing theTIdContextand its connection. The exception is in the context of that thread, the rest of your code (or your users) will not see it.The only thing
ReadLnSplit()does differently thanReadLn()is to force the IOHandler’sMaxLineActionproperty tomaSplitduring that call, nothing else. The only reason to useReadLnSplit()is to handle lines that are longer than the IOHandler’sMaxLineLengthproperty without changing theMaxLineLength. If you don’t like the wayReadLnSplit()behaves, then don’t use it. You could just increase the value of the IOHandler’sMaxLineLengthproperty and callReadLn()instead:Or you could call the overloaded version of
ReadLn()that has an optionalAMaxLineLengthparameter: