socket not released

Hi,
I currently have an app that spawns a connection to server, the server requires hard IP and Port for both my app and the server itself. It then sends a command and disconnects. The problem is that I have to wait ~5 minutes for the socket to become usable again. I know that windows likes to maintain the socket for a few minutes. Any idea on how I can get around this. I call the socket.close() method when I am done sending my parameters. I tried setting

socket.setSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true)

but it does not seem to release the socket fast enough (still taking ~5 minutes) for me to spawn new connections. The hard IP and port can not be changed (unfortunatly for me). Does anyone know of a way to drop a socket connection and release the resources immediatly so I can bind a new socket and send another command Thanks for your help

Tom


Answer this question

socket not released

  • mol233

    Yeah, that is too bad.  I would try to contact the software vender and try to understand their requirment better.  There really is no added security for the server by requiring a client to connect from a specific port (I can understand the IPAddress requirement though).

    Does the server allow you to reuse the connection for more than a short period of time Do you absolutely have to close the connection so frequently



  • Whitegirl23

    I looked at the netstat output and it is showing just my one socket (aside from other applications) in a TIME_WAIT state. The application has an error binding the socket when I try to respawn the connection because that socket is still in the TIME_WAIT state. I tried changing the default timeout by adjusting the registry value. The problem is that it has a minimum of 30 seconds to drop the TIME_WAIT state and free up the socket. I want it to shutdown the socket and free up that port immediatly so that I can spawn a new connection on the same port. I am not able to get the error message and stack trace at this point. I dont have the ability to recreate the error here in the office. I am awaiting the printscreen of the output I was just hoping I could get this out and someone may have a solution to the problem.

  • AmrendraKumar

    Unfortunately thats how the TCP protocol works.
    I would say that in general you should let the client choose the port
    that is dynamic and available. The server is the one that should bind to the same post always.
    If you look at my blog
    http://blogs.msdn.com/dgorti
    You will see some ideas on how to do this



  • sahan39760

    From a command prompt, type "netstat -a -o -p tcp", which will print out all of the tcp sockets that are either in use or in the TIME_WAIT state. What do you see Are there a bunch of sockets in the TIME_WAIT state If so, read http://blogs.msdn.com/dgorti/archive/2005/09/18/470766.aspx.

    If this is not the error you are seeing, please post the exception message and stack trace.



  • settinghead

    Unfortunately for me, I am connecting to another party's equipement and because of the way the 3rd party operates with their encoding schemes. I have to have a static IP and Port for both ends of the communication channel. I wish I could use dynamic ports, I wouldnt have had a problem, but I have to have static all the way through the communication, 3rd party's rules and I have to abide by them if I want to play nicely with their software. I guess I will just have to use a registry key to change the time_wait state to 30 seconds and build in some delaying mechanism.

  • DeBiese

    I dont know if the server allows me to stay connected. At this point I have a bandwidth restriction placed on me in that idle flags and keep alive packets are a no-no because we are paying a premium for some of this bandwidth. I need to send the information I have and disconnect and then reconnect if I need to send something else. It runs into a problem when I send more than one command w/in 4 minutes. So I am in a bit of a damned if I do, damned if I dont situation. I have tried talking with the vendor and they are useless and tell me its this way because (damn french, sorry if you are french, just a bit frustrated).

  • Dreamwinter

    Can you describe why the client has to connect from a specific port It seems to me like you have a design flaw.



  • Iyana

    Well, you probably need to do some analysis as to the actual amount of data on the network.  If you are reconnecting often, the network traffic of establishing the connection may be more than the overhead of keeping the connection alive. Also, if I remember right, the sending of keepalive packets is something you opt into (not on by default), so the cost of keeping the connection alive may not be as bad as you think.  You will have to do your own analysis to make sure, but I think it is worth the effort in this case.

    Of course, this all depends on whether the server will even allow you to keep the connection alive...



  • Stephen XYZ

    I also have added the socket.Shutdown(SocketShutdown.Both) and that does not appear to free up the resources.

  • socket not released