1
我正在使用套接字編程的一個項目,但是當使用相同套接字多次時,即使在關閉listener()
每次我用插座:當我運行應用程序它給人的錯誤:通常只允許使用每個套接字地址(協議/網絡地址/端口)
Only one usage of each socket address (protocol/network address/port) is normally permitted
我真的很擔心。有人可以幫我嗎?
下面是代碼,我使用,我關閉所有連接是否正確
TcpClient tcpClient;
TcpListener tcpListener;
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
{
tcpClient.ReceiveTimeout = 1;
tcpClient = tcpListener.AcceptTcpClient();
}
catch (Exception ex)
{
tcpClient.Close();
tcpListener.Stop();
}
NetworkStream networkStream = tcpClient.GetStream();
byte[] bytes = new byte[networkStream.Length];
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;
你能發表一些代碼和你看到的錯誤嗎? – 2010-02-11 16:24:54
你已經在這裏問過這個問題:http://stackoverflow.com/questions/2240990沒有答案解決你的問題? – dtb 2010-02-11 16:51:21