我正在连接到Asp.Net中的TCP/IP端口,基本上我在这个端口上连接了一个设备,工作正常,但第二次当tcp侦听器尝试启动时,它会产生上述错误。我怎样才能摆脱 任何机构可以指导我这个错误的,这里是我的代码,我使用连接到TCP/IP端口:通常只允许使用每个套接字地址(协议/网络地址/端口)
try
{
byte[] ipaddress = new byte[4];
string ip = ConfigurationManager.AppSettings["IP"].ToString();
string[] ips = ip.Split('.');
ipaddress[0] = (byte)Convert.ToInt32(ips[0]);
ipaddress[1] = (byte)Convert.ToInt32(ips[1]);
ipaddress[2] = (byte)Convert.ToInt32(ips[2]);
ipaddress[3] = (byte)Convert.ToInt32(ips[3]);
int portNumber = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
tcpListener = new TcpListener(new IPAddress(ipaddress), portNumber);
tcpListener.Start();
tcpClient = new TcpClient();
tcpClient.NoDelay = true;
try
{
System.Threading.Thread.Sleep(60000);
tcpClient.ReceiveTimeout = 10;
tcpClient = tcpListener.AcceptTcpClient();
}
catch (Exception ex)
{
tcpClient.Close();
tcpListener.Stop();
}
NetworkStream networkStream = tcpClient.GetStream();
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
try
{
networkStream.ReadTimeout = 2000;
networkStream.Read(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
tcpClient.Close();
tcpListener.Stop();
}
string returndata = Encoding.Default.GetString(bytes);
tcpClient.Close();
tcpListener.Stop();
return returndata.Substring(returndata.IndexOf("0000000036"), 170);
}
catch (Exception ex)
{
if (tcpClient != null)
tcpClient.Close();
tcpListener.Stop();
LogError("Global.cs", "ReceiveData", ex);
ReceiveData();
}
当谈到在这条线tcpListener.Start
();对于第二时间则生成错误 “只有每个套接字地址(协议/网络地址/端口)中的一个的使用通常被许可”
乍一看,我会建议听者没有正确关闭。 – Russell 2010-02-10 23:04:50