怎样彻底释放一个Socket占用的端口
仓木泽 2006-04-12 03:19:13 while(true)
{
for(int i=0;i<1000;i++)
{
Socket sender = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(i.ToString());
IPEndPoint localEP = new IPEndPoint( IPAddress.Parse("172.23.163.169") , 8250);
IPEndPoint remoteEP = new IPEndPoint( IPAddress.Parse("172.23.163.244") , 1860);
sender.Bind(localEP);
sender.Connect(remoteEP);
sender.Send(data);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
//由于系统缓冲区空间不足或列队已满,不能执行套接字上的操作。
}
finally
{
sender.Close();
System.Threading.Thread.Sleep(100);
}
}
}
创建Socket以后,虽然显式调用了sender.Close();
但是重新在8250端口创建Socket连接的时候就报错。
那我需要怎样才能重用之前的套接字连接的端口?