TCP Socket Issues

I believe that I’ve found an issue with TCP sockets and a workaround for it. Forgive me if these are either already well known or else I’m doing something foolish.

My development is taking place on Windows7. My code is pasted below.

First, when initiating a TCP connection with a remote server, I can verify that the server has logged the connection but the TCP socket’s isConnected property remains false. The workaround is that once I flush the socket the isConnected property becomes accurate. The reason that this has been an issues is because…

Second, if I attempt to make a connection with the remote server but deliberately pick a phony IP address so that no connection can be made, then write anything to the socket, and then flush, the program hangs and I have to shut it down using Windows.

The first issue is more of a problem from my perspective but I thought that I might make something googalable for others who run into this problem to see if they are encountering unexplained behavior in their TCP sockets.

Hope this helps.


Dim Command as new OutputCommand 'My own object to parse a protocol that I am using
Dim Response as new OutputResponse 'Another object to parse the protocol

app.IPAddress = “192.168.0.1” 'IP address and ports are kept in attributes of the application itself

Socket.Close 'Socket is a TCP Socket object within the same window as this code
Socket.Address = App.IPAddress
Socket.Port = ctype(App.Port, double)

Socket.Connect

Command.CommandText = “Foo”

Command.PrepAndSend 'This writes a parsed version of “Foo” to the socket. At this point, Socket.isConnected still reads False.

Socket.Flush 'This would cause the application to hang if Socket were not, in fact, connected in reality.

'The simple solution is to insert Socket.Flush immediately after attempting a connection and then poling Socket.isConnected and closing the port if it is still False.

You’re doing it wrong. You either need to use events (Connected, DataAvailable, …), or you need to poll.

What you try to attempt in your code is to communicate synchronously. TCPSockets are working asynchronously by nature. Polling is the way to do it synchronously.

See the example in the Language Reference: TCPSocket. It is worthwhile to read the whole page.

The Connect method is not synchronous. That is, it doesn’t wait for the connection to be established before returning. As such, IsConnected will (correctly) be False for an indeterminate period of time after Connect returns.

Generally, code that should run once the socket connects should be put in the Connected event. You can also wait in a loop for IsConnected to become True:

Socket.Connect
Do Until Socket.IsConnected // beware infinite loops if the connection fails
    Socket.Poll    
Loop

I don’t see it now, but I think the docs used to say that Connect was in fact synchronous. I know I have always relied on that.

[quote=177015:@Tim Hare]I don’t see it now, but I think the docs used to say that Connect was in fact synchronous. I know I have always relied on that.[/quote]This contradicts the example in the LR:

[code] …
//Print is a method that writes a new row of text into a TextField
Print “Beginning synchronous connection…”

//connect the socket
TCPSocket1.Connect

start = Ticks
//while the socket isn’t connected
While Not TCPSocket1.IsConnected
//check to see if the socket got an error
If TCPSocket1.LastErrorCode <> 0 then
Print "Socket Error: " + str(TCPSocket1.LastErrorCode)
Exit
End If
//poll the socket to let it do its thing
TCPSocket1.Poll
Wend
stop = Ticks
…[/code]

The docs are confusing, then. The example under Connect has a comment, “Begin synchronous connect” and has no loop.

There is no example in the docs under “SocketCore.Connect”.

It was there last night. I checked before posting.

Nope.

Well ok then. I guess I’ll adjust my medication. :slight_smile: