Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages
Started by Denis
Tue, 28 Jun 2005 13:58
How to reconnect a client ?
Author: Denis
Date: Tue, 28 Jun 2005 13:58
Date: Tue, 28 Jun 2005 13:58
65 lines
2944 bytes
2944 bytes
Hello, I'm sorry if it is a little long...
Can anybody say me if I'm correct using sockets. I must explain a little
what append:
I'm using many non-bloquing application with TServerSocket and
TClientSocket.
There is many executable who are both client and servers. It's a normal way
to start an application, use some features and close the application.
At time 't' an application is server with 'n' client connected. In the same
time this application is also a client connected to one or more servers.
My question: What exactly must i do when a client connection is broken ? The
socket is unusable and unable to reconnect to the server.
So I must delete it and create a new client socket to lookup again the
server.
What is the right way, and the order, to process ??
This a part of code that do actually this. ( this a client part ) is it
correct ?
void __fastcall client::ServerError(TObject *Sender, TCustomWinSocket
*Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
TClientSocket * err_socket = (TClientSocket * )Sender;
// On ferme le socket pour provoquer un evenement Disconnect et liberer
la m�moire
err_socket->Close();
// close immediately call OnDisconnect event
Socket->Close();
// free memory ?
delete err_socket;
// all done ?
ErrorCode = 0;
}
void __fastcall client::ServerDisconnect (TObject *Sender, TCustomWinSocket
*Socket)
{
TClientSocket * err_socket = (TClientSocket * )Sender;
// create a new connection socket
TClientSocket * new_socket = new TClientSocket (NULL);
new_socket->ClientType = ctNonBlocking;
new_socket->OnConnect = ServerConnect;
new_socket->OnDisconnect = ServerDisconnect;
new_socket->OnError = ServerError;
new_socket->OnLookup = ServerLookup;
new_socket->OnRead = ServerRead;
new_socket->Host = err_socket->Host;
new_socket->Port = err_socket->Port;
new_socket->FreeOnRelease();
// Active this new connection
new_socket->Active = true;
}
Thanks in advance,
-Denis
Re: How to reconnect a client ?
Author: Hans Galema
Date: Tue, 28 Jun 2005 14:20
Date: Tue, 28 Jun 2005 14:20
54 lines
2890 bytes
2890 bytes
Denis wrote: > I'm using many non-bloquing application with TServerSocket and > TClientSocket. Now which one is non-blocking ? Both ? > There is many executable who are both client and servers. Please clarify: There are more instances of your program. Each program has more than one TServerSocket and more then one TClientSocket. > At time 't' an application is server with 'n' client connected. In the same > time this application is also a client connected to one or more servers. > > My question: What exactly must i do when a client connection is broken ? Now which connection are you referring to ? A connection belonging to the TServerSocket or to the TClientSocket? > The > socket is unusable and unable to reconnect to the server. > So I must delete it and create a new client socket to lookup again the > server. So it was a clientconnection from you TClientSocket ? Of course the TClientSocket can reconnect. Why not ? > What is the right way, and the order, to process ?? > This a part of code that do actually this. ( this a client part ) is it > correct ? > > void __fastcall client::ServerError(TObject *Sender, TCustomWinSocket > *Socket, TErrorEvent ErrorEvent, int &ErrorCode) Please do not change the name of eventhandlers. I do not know about 'ServerError'. What exists though for TClientSocket is: void __fastcall TForm1::ClientSocket1Error(TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) > void __fastcall client::ServerDisconnect (TObject *Sender, TCustomWinSocket > *Socket) Do you mean for TClientSocket ?: void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender, TCustomWinSocket *Socket) Think that it is not needed to delete the old and create a new TClientSocket. But indeed you cannot reconnect in the same eventhandler. Start a TTimer and reconnect in the TTimer eventhandler. Hans.
Re: How to reconnect a client ?
Author: Denis
Date: Tue, 28 Jun 2005 15:35
Date: Tue, 28 Jun 2005 15:35
102 lines
4162 bytes
4162 bytes
I attempt to clarify. "Hans Galema" <notused@notused.nl> a �crit dans le message de news:42c15d78@newsgroups.borland.com... > Denis wrote: > > > I'm using many non-bloquing application with TServerSocket and > > TClientSocket. > > Now which one is non-blocking ? Both ? > > > There is many executable who are both client and servers. > > Please clarify: > There are more instances of your program. Each program has more than one > TServerSocket and more then one TClientSocket. ----------------- both TclientSocket and TServerSocket are non blocking. each program have one TServeurSocket and one TClientSocket ----------------- > > > At time 't' an application is server with 'n' client connected. In the same > > time this application is also a client connected to one or more servers. > > > > My question: What exactly must i do when a client connection is broken ? > > Now which connection are you referring to ? A connection belonging to the > TServerSocket or to the TClientSocket? > --------------- a connection to the TClientSocket --------------- > > The > > socket is unusable and unable to reconnect to the server. > > So I must delete it and create a new client socket to lookup again the > > server. > > So it was a clientconnection from you TClientSocket ? Of course > the TClientSocket can reconnect. Why not ? --------------- So what can I do to reconnect it ? When can I do that ? ( must I wait some seconds ? ) Where ? More precisely in what part of the handler can I do that ? can I process in a ClientSocket1Error handler ? or ClientSocket1Disconnect ? --------------- > > > What is the right way, and the order, to process ?? > > This a part of code that do actually this. ( this a client part ) is it > > correct ? > > > > void __fastcall client::ServerError(TObject *Sender, TCustomWinSocket > > *Socket, TErrorEvent ErrorEvent, int &ErrorCode) > > Please do not change the name of eventhandlers. I do not know about > 'ServerError'. What exists though for TClientSocket is: -------------- Sorry, you are right. ------------- If the handler is OnS > > void __fastcall TForm1::ClientSocket1Error(TObject *Sender, > TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) > > > void __fastcall client::ServerDisconnect (TObject *Sender, TCustomWinSocket > > *Socket) > > Do you mean for TClientSocket ?: > void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender, > TCustomWinSocket *Socket) > > Think that it is not needed to delete the old and create a new > TClientSocket. But indeed you cannot reconnect in the same eventhandler. > Start a TTimer and reconnect in the TTimer eventhandler. -------------- Hummm... so I have to try to reconnect outside all the event handlers ? It's damage because the socket is know inside the event handler. So if I understand correctly I have to maintain a list of all my clients and build a loop to check connection every 'n' seconds in a timer function ? that right ? But I don't know what I have to check. The property "Active" of the TClientSocket ? ( I have not acces to the TCustomWinSocket* provide in handler ? ) -Denis.
Re: How to reconnect a client ?
Author: Hans Galema
Date: Tue, 28 Jun 2005 15:40
Date: Tue, 28 Jun 2005 15:40
21 lines
1842 bytes
1842 bytes
Denis wrote: > Hummm... so I have to try to reconnect outside all the event handlers ? Yes. I think so. > It's damage because the socket is know inside the event handler. The Socket is not important in this case. ClientSocket1 is importantant. > So if I understand correctly I have to maintain a list of all my clients and > build a loop to check connection every 'n' seconds in a timer function ? > that right ? You said that there was only one TCientSocket. And if the Socket of TClientSocket1 looses connection then you do not have to have a list because TClientSocket can only be connected to one server. So why do you speak of a list ? As there is only one TClientSocket you can simply call ClientSocket1->Open() in the TTimer eventhandler. Hans.
Re: How to reconnect a client ?
Author: Denis
Date: Tue, 28 Jun 2005 16:14
Date: Tue, 28 Jun 2005 16:14
41 lines
2269 bytes
2269 bytes
> > So if I understand correctly I have to maintain a list of all my clients and > > build a loop to check connection every 'n' seconds in a timer function ? > > that right ? > > You said that there was only one TCientSocket. And if the Socket of > TClientSocket1 looses connection then you do not have to have a list > because TClientSocket can only be connected to one server. So why do > you speak of a list ? As there is only one TClientSocket you can simply > call ClientSocket1->Open() in the TTimer eventhandler. > Sorry Hans, I was talking about one TClientSocket per process only to made the thinks simple. In the reality all process are server for all other process. The configuration is dynamic, but if I launch 3 process called A, B and C, the result of the connections are: A ( server ) connect to B (client ) A ( server ) connect to C(client ) B( server ) connect to A(client ) B( server ) connect to C(client ) C( server ) connect to A(client ) C( server ) connect to B (client ) In this example A, B, and C have 1 TServerSocket and 3 TClientSocket. So last question, how can I test if the connection is broken in the TClientSocket object ? Can I check the property Active ? > Hans.
Re: How to reconnect a client ?
Author: Hans Galema
Date: Wed, 29 Jun 2005 06:33
Date: Wed, 29 Jun 2005 06:33
61 lines
3214 bytes
3214 bytes
Denis wrote: > ... I was talking about one TClientSocket per process only to made > the thinks simple. > In the reality all process are server for all other process. You are complicating things. Please do not speak about a process as with c++builder one makes applications. You had already stated that your application had one TServerSocket and one TClientSocket. So it could act as a server and a client. > The configuration is dynamic, but if I launch 3 process called A, B and C, > the result of the connections are: > > A ( server ) connect to B (client ) > A ( server ) connect to C(client ) Well already here I cannot follow you. A server does not connect to a client. It is the other way round. Or did you mean 'is connected to' ? A server can only listen and wait until a client makes a connection. So the active player here is the client. > B( server ) connect to A(client ) > B( server ) connect to C(client ) > > C( server ) connect to A(client ) > C( server ) connect to B (client ) In total this cannot be done with each application having only one TServerSocket and one TClientsocket. Cannot you rephrase the connections in this way:? A ( TClientSocket ) connects to B (TServerSocket ) A ( TClientSocket ) connects to C (TServerSocket ) B ( TClientSocket ) connects to A (TServerSocket ) B ( TClientSocket ) connects to C (TServerSocket ) C ( TClientSocket ) connects to A (TServerSocket ) C ( TClientSocket ) connects to B (TServerSocket ) But for this each application needs two TClientSocket's. > In this example A, B, and C have 1 TServerSocket and 3 TClientSocket. 3 ???? And where do you use the third for ? If you really use three than give an example using TClientSocket1, TClientSocket2, TClientSocket3. > So last question, how can I test if the connection is broken in the > TClientSocket object ? > Can I check the property Active ? Probably. Or Socket->Connected. But why do you want to check ? There are OnError and OnDisconnect events which will tell you right away. Hans.
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