0

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?

Fat Monk
  • 2,077
  • 1
  • 26
  • 59
  • I've had a look at http://stackoverflow.com/questions/13045943/delphi-synapse-how-to-check-if-i-am-still-connected for the second question above, but `(mySocket.Socket = INVALID_SOCKET) or ((mySocket.WaitingData = 0) and mySocket.CanRead(0))` always returns false even though I have a good connection... – Fat Monk Oct 06 '14 at 16:10
  • After further investigation it seems that it is the `.CanRead(0)` that is returning `false` every time and causing the call to return `false` - even though the connection is good. – Fat Monk Oct 08 '14 at 08:31

1 Answers1

1

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)

Fat Monk
  • 2,077
  • 1
  • 26
  • 59