2011-09-22 69 views
1

這裏是網絡編程的新手。在同一臺機器上調試TcpClient和TcpListener

我有一個兩件套的應用程序。我試圖在本地進行調試。
服務監聽new IPEndPoint(IPAddress.Any, 3000)上的連接。
致電tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000))拋出No connection could be made because the target machine actively refused it 127.0.0.1:3000。 Windows防火牆已關閉。

我在做一些根本上愚蠢的事情嗎?

回答

1

您應該調用TcpListener上的Start方法使其工作,否則它將不接受任何連接。

我已經測試和這個片段工程:)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 

namespace ConsoleApplication3 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int port = 5124; 

      var myListener = new TcpListener(new IPEndPoint(IPAddress.Any, port)); 
      myListener.Start(); 

      var tcpClient = new TcpClient(); 
      tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port)); 

      tcpClient.Close(); 

     } 
    } 
} 
+0

是我打電話'開始()'是肯定的。奇怪的是,當我改變你的端口(我以前使用的是3000端口),我的代碼也工作。是否可以通過不關閉客戶端或偵聽器來阻塞端口?應用程序退出時是否應清除任何非託管資源? –

+0

我不知道,我只是不使用那個端口...看到這裏:http://www.pc-library.com/ports/tcp-udp-port/3000/ –

+0

嗯有趣...但無論如何,這回答了我的問題,無論我在哪裏做任何根本錯誤的事情。 –

相關問題