Hi guys,
First of all, I have tried to solve my problem after reading this thread: https://forums.embarcadero.com/thread.jspa?threadID=72940 but I still have some doubts.
I am using a TClientSocket to communicate, but sometimes the server goes down and the system tries to automatically connect again. The thing is that after trying to connect for a long time, I get the socket error 10055. After reading that thread I understood that I am " trying to connect the socket too many times too often". I am thinking about how to solve that and I would like to know if doing
{code}
ClientSocket->Close();
{code}
every time I get a socket disconnect would work. I am asking that instead of programming it because it is not easy for me to generate the conditions to force the error.
Thanks in advance,
Javi
Hello again,
Given that no one was able to give me an answer (most probably because I was not able to explain the problem I have), I would like to share my code to let us check what I am doing wrong. I guess there is not just one mistake, but hopefully someone will be able to find the origin of that 10055 error...
My configuration is:
{code}
TCPClient=new TClientSocket(NULL);
TCPClient->OnRead=ClientSocketRead;
TCPClient->OnError=ClientSocketError;
TCPClient->OnConnect=ClientSocketConnect;
TCPClient->OnDisconnect=ClientSocketDisconnect;
TCPClient->ClientType=ctNonBlocking;
{code}
And the functions I am using are:
{code}
void __fastcall TPuerto::ClientSocketError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
Active=false;
TCPClient->Close();
delete TCPClient;
TCPClient=new TClientSocket(NULL);
TCPClient->OnRead=ClientSocketRead;
TCPClient->OnError=ClientSocketError;
TCPClient->OnConnect=ClientSocketConnect;
TCPClient->OnDisconnect=ClientSocketDisconnect;
TCPClient->ClientType=ctNonBlocking;
Socket->Close();
}
void __fastcall TPuerto::ClientSocketConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Active=true;
}
void __fastcall TPuerto::ClientSocketDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
TCPClient->Close();
Active=false;
}
{code}
Thanks in advance!
Javier wrote:
> Would the following socket error handler be ok?
Replace TCPClient->Close() with Socket->Close():
{code}
void __fastcall TPuerto::ClientSocketError(TObject *Sender, TCustomWinSocket
*Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0;
Active = false;
Socket->Close();
}
{code}
I would even go as far as making sure it is not an error from an earlier
Close() attempt:
{code}
void __fastcall TPuerto::ClientSocketError(TObject *Sender, TCustomWinSocket
*Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0;
Active = false;
if (ErrorEvent != eeDisconnect)
Socket->Close();
}
{code}
--
Remy Lebeau (TeamB)
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Javier wrote:
>
> > And the functions I am using are:
>
> It is not safe to destroy the TClientSocket object from inside its own events,
> so stop doing that. All you have to do is Close() the Socket, nothing more.
> Have your main code check the TClientSocket::Active property periodically
> and re-Open() the TClientSocket when needed. And if you do get disconnected
> unexpectedly, make sure you have a delay in between each attempt to re-connect
> if you cannot re-connect right away.
>
> --
> Remy Lebeau (TeamB)
Would the following socket error handler be ok?
{code}void __fastcall TPuerto::ClientSocketError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
Active=false;
TCPClient->Close();
}
{code}