2016-05-13 18 views
-1

爲什麼會拋出使用異常的套接字爲TcpClient(IPEndPoint)而不是TcpClient(String, Int32)爲什麼會爲TcpClient(IPEndPoint)而不是TcpClient(String,Int32)引發使用中的套接字異常?

我在我的代碼,我使用偵聽器(IP是:: 1,端口爲12345)

listener = new TcpListener(ip, port); //create listener 
listener.Start(); //start listener 
//now code will wait at the while loop until someone connects. 
while (listener.Pending()) { System.Threading.Thread.Sleep(1000); } //check for pending connections every second. 
client = listener.AcceptTcpClient(); //when incoming connection is found accept it. 

注意代碼裏面有個Task listenerTask = Task.Run(() =>{});

另一個任務我有以下

//client = new TcpClient(new IPEndPoint(ip, port)); //does not work 
client = new TcpClient("localhost", port); //localhost resolves to ::1 

那麼有什麼交易?有什麼不同?我錯了localhost解決:: 1?如果它不解決這個問題,那麼我的程序如何能夠回顯自己?

我會盡量在此期間獲取更多信息。

異常詳細信息:

client = new TcpClient(new IPEndPoint(ip, port)); 

綁定與本地 IP地址本地端口使用:

System.Net.Sockets.SocketException was unhandled 
ErrorCode=10048 
HResult=-2147467259 
Message=Only one usage of each socket address (protocol/network address/port) is normally permitted 
NativeErrorCode=10048 
Source=System 
StackTrace: 
    at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) 
    at System.Net.Sockets.Socket.Bind(EndPoint localEP) 
    at System.Net.Sockets.TcpClient..ctor(IPEndPoint localEP) 
    at ConsoleApplication1.Program.Connector(IPAddress[] ips, Int32 port) 
+0

看起來像你正試圖再次使用相同的端口這是不允許的。 – Rahul

+0

在資源監視器中,我看到我的ConsoleApplication1.exe有兩個條目(當它工作時)。一個條目具有本地端口12345和遠程端口63123,而另一條目具有本地端口63123和遠程端口12345. @Rahul是你在說什麼? – UpTide

+1

不太確定,但嘗試使用不同的端口。 – Rahul

回答

相關問題