【请问】TCP连接出现“通常每个套接字地址(协议/网络地址/端口)只允许使用一次”异常
【请教】TCP连接出现“通常每个套接字地址(协议/网络地址/端口)只允许使用一次”错误
代码如下:
问题如代码里注释所说的,请问该如何处理?
------解决思路----------------------
本地端口用0试试。
代码如下:
private void SendCommand(string cmd)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(_LocalIp), 9000);
TcpClient tcp = new TcpClient(ipe);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse(_RemoteIp), 80);
tcp.SendTimeout = 1000;
tcp.Connect(remote); // <---------------------------第一次运行可以,第二次运行到这里出错
NetworkStream ns = tcp.GetStream();
byte[] b = Encoding.Default.GetBytes(string.Format(cmd, "", txbPassword.Text, txbCmd.Text));
ns.Write(b, 0, b.Length);
Thread.Sleep(1000);
while(ns.DataAvailable)
{
byte[] bb=new byte[1024];
ns.Read(bb, 0, 1024); // <------------------------------- 如果把这一句注释掉,则一直不会出错
this.Invoke(new MethodInvoker(delegate
{
txbResp.AppendText(Encoding.Default.GetString(bb));
}));
}
ns.Close();
tcp.Close();
ns = null;
tcp = null;
ipe = null;
remote = null;
}
问题如代码里注释所说的,请问该如何处理?
------解决思路----------------------
本地端口用0试试。