2011-09-06 42 views
0
public Server([Optional, DefaultParameterValue(0x6c1)] int port, [Optional, DefaultParameterValue("127.0.0.1")] string ip) 
{ 
    this.IP = IPAddress.Parse(ip); 
    this.Port = port; 
    this.listenerConnection = new TcpListener(this.IP, this.Port); 
    this.listenerConnection.Start(); 
    this.listenerThread = new Thread(new ThreadStart(this.listen)); 
    this.listenerThread.Start(); 
} 

是我的代碼,它運行良好,但是當我調試它,我得到的消息:TCP調試錯誤C#

指定參數超出有效值的範圍。參數名稱:端口

任何人都可以幫忙嗎?

+0

在調試時您給出的端口值是多少?你能告訴? –

+0

這是什麼類型的端口?什麼是價值? TCP的端口範圍是從0到65535 –

+0

我使用的端口是1274 – Neel

回答

2

那麼,那麼port超出有效值的範圍,這是在IPEndPoint.MinPortIPEndPoint.MaxPort之間。

+0

我使用的端口是1274,我知道它是正確的,因爲它在我的VPS上工作過。 – Neel

+0

@Neel:那不可能。在this.listenerConnection = new TcpListener(this.IP,this.Port)之前設置斷點;並觀察變量this.Port。 – CodeCaster

+0

@Neel還有其他的東西在你的電腦上使用TCP端口1274嗎?運行netstat -a –

0

您是否嘗試過使用機器的IP地址?您可以使用下面的代碼,以獲得您正在運行的應用程序的計算機的ip地址:

IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); 
IPAddress localIpAddress = null; 

forach(IPAddress address in host.AddressList) 
{ 
    if(address.AddressFamily == AddressFamily.InterNetwork) 
    { 
      localIpAddress = address; 
    } 
} 

TcpListener listener = new TcpListener(localIpAddress, port); 
listener.Start(); 

此外,您可能希望因爲有0和5000之間的許多港口考慮使用默認的端口> 5000。保留的或已被服務使用的。