我在使用TcpClient和TcpListener時引發此異常時有點遺憾。它的工作原理是第一次,我然後再次運行它,我得到以下異常:TcpClient套接字 - 每個套接字地址的唯一用法例外
每個套接字地址(協議/網絡地址/端口)只有一個用法是正常允許127.0.0.1:8086
我檢查過以確保我關閉了任何打開的連接。我已經嘗試手動調用關閉TcpClient以及使用IDisposable使用模式,但仍然有同樣的問題。
下面的代碼,如果你複製粘貼在Visual Studio(前提是你已經添加以下using語句),它應該只是運行
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
internal class Program
{
private static void tcpClientConnection()
{
Console.WriteLine("Ready");
Console.ReadKey();
IPAddress address = IPAddress.Parse("127.0.0.1");
using (TcpClient client = new TcpClient(new IPEndPoint(address, 8087)))
{
client.Connect(new IPEndPoint(address, 8086));
using (NetworkStream ns = client.GetStream())
{
ns.Write(System.Text.Encoding.ASCII.GetBytes("Hello"), 0, "Hello".Length);
ns.Flush();
}
Console.WriteLine("Closing client");
}
}
internal static void Main(string[] args)
{
IPAddress address = IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(new IPEndPoint(address, 8086));
server.Start();
using (Task task2 = new Task(tcpClientConnection))
{
task2.Start();
using (TcpClient client = server.AcceptTcpClient())
{
using (NetworkStream ns = client.GetStream())
{
using (MemoryStream ms = new MemoryStream())
{
ns.CopyTo(ms);
byte[] data = ms.ToArray();
Console.WriteLine(System.Text.Encoding.ASCII.GetString(data));
}
}
}
Console.WriteLine("Server stop");
Console.ReadKey();
server.Stop();
}
Console.WriteLine("END");
Console.ReadKey();
}
}
請注意,我們查看了在提供的解決方案類似的問題,但一直沒有能夠看到什麼問題...
對於TcpClient,不要指定本地端點。我認爲這解決了問題,你不應該這樣做,因爲它什麼都不做。 – usr
非常棒的地方,那確實是馬上解決了問題,謝謝!我會詳細說明本地端點的具體含義。它不只是每次都使用同一個端口的套接字?如果所有連接都關閉了,是否應該繼續使用相同的地址和端口?只是想知道,謝謝 – redspidermkv