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.