我正在連接到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