Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Jun : Re: TCP client/server
| Subject: | Re: TCP client/server |
| Posted by: | "Jamie Dale" (j.da..@turboz.net) |
| Date: | Mon, 5 Jun 2006 22:20:03 |
>> Is this the correct way to handle it?
>
> The only thing I would suggest is to remove the Disconnect() from the
> server
> side and let the client be in control of disconnecting from the server
> rather than the server disconnecting the client.
No, using disconnect is perfectly fine! - I use it myself and there IS a way
to get around the error message.
>> I get a 'Connection closed gracefully' exception on the client side.
>
> You shouldn't be. Which version of Indy are you using? That exception is
> only thrown when the socket is written to, or when read from while the
> InputBuffer is empty, after the other party has closed the socket. You've
> already done all of your writing before calling Disconnect() on the client
> side, and all of your reading before calling Disconnect() on the server
> side, so there shouldn't be anything checking the socket's connected state
> in order to throw that exception. Disconnect() does not ever throw that
> exception.
Try this on the server side: AThread.Connection.Disconnect;
On the client Side:
Try
Response := IdTCPClient1.ReadLn;
Except
//"Connection Closed Gracefully" is handled here but nothing is actually
done and no message displayed.
end;
The above code handles the error. Basically when the "Connection Closed
Gracefully" error is thrown up, your compiled .exe will jump into the
'Except' clause and do nothing (If you leave it blank or a comment like I
have) OR it will do whatever you program it to do. The reason this happens
is that as Remy said, your program tries to read from the socket and fails.
Therefore the program raises the 'EIdConnectionClosedGracefully'
error/exception. As you know, in a Try/Except clause your program jumps to
the Except section when this happens where you must either ignore or do
something with it. I personally leave it blank or commented and ignore it.
Also add "EIdConnectionClosedGracefully" to your Language exceptions
(Tools>Debugger>Language Execptions). This will stop the Delphi debugger
alerting you to the error while debugging the application. Despite you
handing the code (like I've explained above) If you don't, Delphi will still
alert you while debugging unless you add it to the Language Exceptions you
want the debugger to ignore.