TCPSocket refuses connection

I have a Xojo console app that uses TCPSockets to communicate with some JavaScript inside of Adobe InDesign, using a simple HTTP protocol. This has worked fine for quite a while on Mac, but now I need to make a Windows version. BUT: what happens is that the first message that the JavaScript client sends to the Xojo app works fine, but after that the client gets “connection refused” every time it tries to send a message to Xojo.

The code is identical on Windows and Mac; the socket’s DataAvailable event looks approximately like this:

dim request as string = me.ReadAll() ... do something me.Write(replyString)
And in the SendComplete event:

me.listen
Why would this work fine on Mac but not on Windows? The first message always gets through on both platforms, but then on Windows it stops accepting connections for some reason. On Windows, after the first message is successfully handled, the DataAvailable event never fires.

Puzzled; any suggestions? Thanks!

Just guessing, but it might relate to TIME_WAIT assuming the JavaScript issues a close on the connection:

http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html

http://blog.davidvassallo.me/2010/07/13/time_wait-and-port-reuse/

I seem to recall having issues re-using closed (outbound, though ) sockets on Win and getting around it by trashing the socket ( close and NIL ) and creating a new one. You’d have to watch the Win limits on inbound connections, as you are listening. But then again, my memory is turning to mush. It would be interesting to set up a client app and a server app to test with.

You might have to look at using a server socket.

I looked at an oldish program of mine that uses a normal TCPSocket to listen, as I remember having some issues. To get around the TIME_WAIT problem I had to use a timer set at 5 seconds to re-start the listen, else I could not connect again.

Maybe a server socket would work?

Oh hell, I can’t edit.

Seeing as you are using HTTP, is there a way you could make your incoming connection use ‘keep alive’ and not close the connection?

Connection: keep-alive

Peter,

Your first answer was very interesting, and set me on the path to put a lot more debugging into the protocol. And it turns out your third answer was very close to the solution. I was closing the connection after each message, but the client kept reconnecting. And since it was reconnected, when it went to connect again it couldn’t. I fixed it by adding the Keep-Alive header and not closing the connection on the Xojo side.

Curious that I didn’t see this problem at all on the Mac (with the same client). Must be something to do with the TCP library Adobe is using on each platform.

So thanks very very much for your suggestions! I’m marking your second answer as correct.

I’m glad you got it working.