Search code examples
socketstcplazarusfpc

Closing a connection and re-opening with Synapse in Lazarus


I'm using the Synapse library with Lazarus and I'm trying to switch a TCPBlockSocket connection between two different IP addresses.

I create the Socket connection with:

mySocket:=TTCPBlockSocket.Create

I then connect to the first IP address with:

mySocket.Connect(firstIPaddress,portNumber);

This works fine, but if I try to switch to the second IP address with

mySocket.Connect(secondIPaddress,portNumber);

any data I send with mySocket.SendString() is still sent to firstIPaddress.

I have tried .Destroying mySocket and recreating it but I get crashes (I check if mySocket is assigned and if it is do a .Destory before recreating the TCPBlockSocket object - but that seems a bit severe anyway.

Is there a correct way to disconnect and then reconnect an exiting TCPBlockSocket to a different IP address?

And is there an easy way to test if a TCPBLockSocket has an active and working connection?


Solution

  • It's obvious when you find it...

    You need to call mySocket.CloseSocket() before calling .Connect with the new IP address and port.

    (Still problems checking if an opened connection is still open and working though - as per my comment to the OP)