How to immediately close socket release port in C#t?

L

L.J.SONG

I use following code to create socket and connect to a server app:
/// code /////
Socket connecter = null;

IPEndPoint localEp = null;
IPEndPoint remoteEp = null;

IPHostEntry he = Dns.Resolve("localhost");
IPAddress localAddr = he.AddressList[0];
localEp = new IPEndPoint(localAddr,11000);


remoteEp = new IPEndPoint(localAddr,8801);
connecter = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

connecter.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.DontLing
er,1);

try
{
connecter.Bind(localEp);
}
catch(SocketException se)
{


}

connecter.Connect(remoteEp);

connecter.Shutdown(SocketShutdown.Both);
connecter.Close();

//////////

These code could connected to server,
but when I close it and then run it second time .It throw a exception
told me that the port still be bounded and can't be use again.
use "netstat" command I can find the port still keep close_wait state
and this state will keep 3-5 minute.
Who can tell me why and how to make this port release immediately?
Thanks alot.
 
V

Vadym Stetsyak

AFAIR CLOSE_WAIT state is the TCP protocol property, it is better to reuse
connected socket than close/open it many times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top