Thread View: borland.public.cppbuilder.internet.socket
12 messages
12 total messages
Started by Stormy
Thu, 13 Oct 2005 23:27
TClientSocket and error events
Author: Stormy
Date: Thu, 13 Oct 2005 23:27
Date: Thu, 13 Oct 2005 23:27
35 lines
2572 bytes
2572 bytes
Greetings,
I have a small problem with client socket. If I set its Host property to an
unknown host and activate it (either by Open() method or by setting it's
Active property to true), application throws an exception which is handled
inside my handler:
...
Application->OnException = AppException;
...
void __fastcall TForm1::AppException(TObject *Sender, Exception *E)
{
TClientWinSocket *sckt1 = dynamic_cast< TClientWinSocket * >(Sender);
if (sckt1) { ...log error... check if active... }
...
}
When reading socket's Active property inside the exception handler above, it
reads false, which means socket is closed or in process of being closed.
Now, the problem is when I now set socket's Host property to existing,
running host, and open the connection, instead of connecting it throws an
error via OnError event. Error is, of course, related to prior connection
failure to unknown host. OnError event simply does not occur after exception
being thrown. Exception message is: "Windows socket error: No such host is
known (11001), on API 'ASync Lookup'" and ErrorCode inside OnError event is
#10049.
Is there any way of resetting ErrorCode inside exception handler, and if so,
would it prevent socket from triggering an OnError event? My current
solution involves setting socket's Active property back to true inside
exception handler what causes OnError event to occur for given unknown host
connection (I moved error logging inside OnError event), but this just
doesn't seem to be the right way to solve this problem.
Thank you.
Re: TClientSocket and error events
Author: Remy Lebeau (TeamB)
Date: Fri, 14 Oct 2005 00:44
Date: Fri, 14 Oct 2005 00:44
39 lines
2423 bytes
2423 bytes
"Stormy" <s70rmy@hotmail.com> wrote in message news:434eecdf@newsgroups.borland.com... > I have a small problem with client socket. If I set its Host property > to an unknown host and activate it (either by Open() method or by > setting it's Active property to true), application throws an exception > which is handled inside my handler: You should be using the TClientSocket's OnError event to handle socket errors. > Now, the problem is when I now set socket's Host property to existing, > running host, and open the connection, instead of connecting it throws > an error via OnError event. Error is, of course, related to prior connection > failure to unknown host. OnError event simply does not occur after > exception being thrown. Exception message is: "Windows socket error: No > such host is known (11001), on API 'ASync Lookup'" and ErrorCode inside > OnError event is #10049. Please show your actual code to reproduce that. You should not be getting any error involving the previous Host. > Is there any way of resetting ErrorCode inside exception handler, and if so, > would it prevent socket from triggering an OnError event? My current solution > involves setting socket's Active property back to true inside exception handler > what causes OnError event to occur for given unknown host connection (I > moved error logging inside OnError event), but this just doesn't seem to be > the right way to solve this problem. What exactly are you trying to accomplish? Gambit
Re: TClientSocket and error events
Author: Stormy
Date: Fri, 14 Oct 2005 07:39
Date: Fri, 14 Oct 2005 07:39
19 lines
1686 bytes
1686 bytes
Remy Lebeau (TeamB) wrote: > You should be using the TClientSocket's OnError event to handle socket > errors. OnError event does not get triggered if exception occurs. Sockets mostly throw exceptions and I have no idea how to omit this. > Please show your actual code to reproduce that. You should not be > getting any error involving the previous Host. I posted a simplified code that demonstrates this on attachments group. Please take a look. > What exactly are you trying to accomplish? Prevent socket exceptions from triggering messagebox. Since messagebox requires user interaction (must click OK), and I want my application to run in background, logging or showing errors other way. This is why I use exception handler in my code.
Re: TClientSocket and error events
Author: Remy Lebeau (TeamB)
Date: Fri, 14 Oct 2005 07:39
Date: Fri, 14 Oct 2005 07:39
27 lines
1761 bytes
1761 bytes
"Stormy" <s70rmy@hotmail.com> wrote in message news:434f5fe5@newsgroups.borland.com... > OnError event does not get triggered if exception occurs. For most socket errors, the OnError event is triggered BEFORE an exception is thrown. This gives the code a chance to decide whether or not the exception should be thrown at all. > Sockets mostly throw exceptions and I have no idea how to omit this. In the OnError event, set the ErrorCode parameter to 0. > Prevent socket exceptions from triggering messagebox. See above. > Since messagebox requires user interaction (must click OK), and I > want my application to run in background, logging or showing errors > other way. Move the TClientSocket into a worker thread and use it in blocking mode. It is easier to handle errors and exceptions that way. Gambit
Re: TClientSocket and error events
Author: Stormy
Date: Fri, 14 Oct 2005 12:18
Date: Fri, 14 Oct 2005 12:18
18 lines
1793 bytes
1793 bytes
Remy Lebeau (TeamB) wrote: > For most socket errors, the OnError event is triggered BEFORE an > exception is thrown. This gives the code a chance to decide whether > or not the exception should be thrown at all. Unfortunately, this is the case where exception is thrown before OnError event occurs... > In the OnError event, set the ErrorCode parameter to 0. ...so setting ErrorCode to 0 (which I do in this event) does not prevent exception that already happend. > Move the TClientSocket into a worker thread and use it in blocking > mode. It is easier to handle errors and exceptions that way. I always thought it's easier and more reliable working with non-blocking sockets. This is why I avoid using sockets in threads.
Re: TClientSocket and error events
Author: Remy Lebeau (TeamB)
Date: Fri, 14 Oct 2005 17:53
Date: Fri, 14 Oct 2005 17:53
24 lines
1801 bytes
1801 bytes
"Stormy" <s70rmy@hotmail.com> wrote in message news:434fa1c3@newsgroups.borland.com... > Unfortunately, this is the case where exception is thrown > before OnError event occurs... That bug was already fixed in BCB6. The lookup error triggers the OnError event before throwing the exception. > I always thought it's easier and more reliable working with > non-blocking sockets. Nope. It makes you have to separate your processing logic. That makes things harder, not easier. > This is why I avoid using sockets in threads. Blocking sockets work fine in threads. Non-blocking sockets require a message queue, which threads don't have by default. Blocking sockets don't need a queue. Gambit
Re: TClientSocket and error events
Author: Stormy
Date: Fri, 14 Oct 2005 18:54
Date: Fri, 14 Oct 2005 18:54
5 lines
1321 bytes
1321 bytes
Remy Lebeau (TeamB) wrote: > That bug was already fixed in BCB6. The lookup error triggers the > OnError event before throwing the exception. I see now. I'm using BCB5. Well, I guess it's time to upgrade. Thanx.
Re: TClientSocket and error events
Author: Vladimir Stefanovic
Date: Sat, 15 Oct 2005 08:28
Date: Sat, 15 Oct 2005 08:28
9 lines
1429 bytes
1429 bytes
> [...] Well, I guess it's time to upgrade. Thanx. ...and/or look at what Indy offers :) -- Best regards, Vladimir Stefanovic
Re: TClientSocket and error events
Author: Stormy
Date: Tue, 25 Oct 2005 19:08
Date: Tue, 25 Oct 2005 19:08
21 lines
2149 bytes
2149 bytes
Remy Lebeau (TeamB) wrote: > "Stormy" <s70rmy@hotmail.com> wrote in message > news:434fa1c3@newsgroups.borland.com... > >> Unfortunately, this is the case where exception is thrown >> before OnError event occurs... > > That bug was already fixed in BCB6. The lookup error triggers the > OnError event before throwing the exception. Tried recompiling my code with BCB6 today and got same result. Well, not entirely... Exception does not get thrown before OnError event any more, (and you can supress it by resetting error code to 0 in OnError event) but if you try to connect TClientSocket to server that doesn't exist, OnError event gets triggered with error code #11001. OK. Now if you try to connect same socket to server that does exist and is running, OnError event gets triggered again (for some reason unknown to me) with error #10049. If you try connecting it to same server again, OnRead event gets triggered (finally!) and everything seems fine. I dont understand why #11001 and #10049 have to come in pairs.?. VCL, winsock, bug, feature, standard??? *Confused*.
Re: TClientSocket and error events
Author: Remy Lebeau (TeamB)
Date: Tue, 25 Oct 2005 20:50
Date: Tue, 25 Oct 2005 20:50
22 lines
1754 bytes
1754 bytes
"Stormy" <s70rmy@hotmail.com> wrote in message news:435e81f9@newsgroups.borland.com... > if you try to connect TClientSocket to server that doesn't exist, > OnError event gets triggered with error code #11001. As it should. 11001 is WSAHOST_NOT_FOUND. > OK. Now if you try to connect same socket to server that does > exist and is running, OnError event gets triggered again (for some > reason unknown to me) Probably because TClientSocket is not fully cleaning up after itself in that situation. > with error #10049. If you 10049 is WSAEADDRNOTAVAIL Gambit
Re: TClientSocket and error events
Author: Stormy
Date: Thu, 27 Oct 2005 11:48
Date: Thu, 27 Oct 2005 11:48
14 lines
1737 bytes
1737 bytes
Remy Lebeau (TeamB) wrote: > "Stormy" <s70rmy@hotmail.com> wrote in message > news:435e81f9@newsgroups.borland.com... >> OK. Now if you try to connect same socket to server that does >> exist and is running, OnError event gets triggered again (for some >> reason unknown to me) > > Probably because TClientSocket is not fully cleaning up after itself > in that situation. Is there anything one could do to make it fully clean up after itself in this situation? Or should one try connecting to server twice, to be on the safe side?
Re: TClientSocket and error events
Author: Remy Lebeau (TeamB)
Date: Mon, 31 Oct 2005 07:35
Date: Mon, 31 Oct 2005 07:35
12 lines
1558 bytes
1558 bytes
"Stormy" <s70rmy@hotmail.com> wrote in message news:4360bd9e@newsgroups.borland.com... > Is there anything one could do to make it fully clean up > after itself in this situation? Short of destroying and recreating the TClientSocket instance each time, then no. Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads