2012-02-02 227 views
7

我試圖從this question的建議非常收效甚微。連接兩個UDP客戶到一個端口(發送和接收)

請...任何幫助將不勝感激!

這裏是我的代碼:

static void Main(string[] args) 
{ 

    IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000); 

    UdpClient udpServer = new UdpClient(localpt); 
    udpServer.Client.SetSocketOption(
     SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 

    UdpClient udpServer2 = new UdpClient(); 
    udpServer2.Client.SetSocketOption(
     SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 

    udpServer2.Client.Bind(localpt); // <<---------- Exception here 
} 
+1

你得到什麼異常? – 2012-02-02 21:14:47

+0

@ M.Babcock 我得到的異常消息是: 「嘗試以訪問權限禁止的方式訪問套接字」 – brooc 2012-02-02 21:24:43

+0

嘗試綁定時,您的udpServer實例是否會拋出相同的異常? – 2012-02-02 21:33:48

回答

23

你必須結合之前套接字選項設置。

static void Main(string[] args) 
    { 
     IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000); 

     UdpClient udpServer = new UdpClient(); 
     udpServer.Client.SetSocketOption(
      SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
     udpServer.Client.Bind(localpt); 

     UdpClient udpServer2 = new UdpClient(); 
     udpServer2.Client.SetSocketOption(
      SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 

     udpServer2.Client.Bind(localpt); // <<---------- No Exception here 

     Console.WriteLine("Finished."); 
     Console.ReadLine(); 
    } 

還是更說明性的例子:

static void Main(string[] args) 
    { 
     IPEndPoint localpt = new IPEndPoint(IPAddress.Loopback, 6000); 

     ThreadPool.QueueUserWorkItem(delegate 
     { 
      UdpClient udpServer = new UdpClient(); 
      udpServer.ExclusiveAddressUse = false; 
      udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
      udpServer.Client.Bind(localpt); 

      IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0); 
      Console.WriteLine("Listening on " + localpt + "."); 
      byte[] buffer = udpServer.Receive(ref inEndPoint); 
      Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + "."); 
     }); 

     Thread.Sleep(1000); 

     UdpClient udpServer2 = new UdpClient(); 
     udpServer2.ExclusiveAddressUse = false; 
     udpServer2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
     udpServer2.Client.Bind(localpt); 

     udpServer2.Send(new byte[] { 0x41 }, 1, localpt); 

     Console.Read(); 
    } 
+0

非常感謝! 工作就像一個魅力! – brooc 2012-02-03 12:01:59

+0

我在這裏發佈了一個後續問題:http://stackoverflow.com/questions/9140450/udp-hole-punching-implemetation-in-c-sharp – brooc 2012-02-04 11:19:09

+0

爲什麼你綁定'localpt'和'Receive' on 'inEndPoint',是否有不同? (如果我在'localpt'上接收''似乎沒有任何中斷) – Benjol 2012-03-19 14:12:51

2

我擡頭一看你的錯誤信息,這說明是什麼錯誤,爲什麼它正在發生。

這裏是確切的錯誤信息和原因WSAEACCES 10013(MSDN

權限被拒絕。

試圖通過其 訪問權限不允許的方式來訪問一個插座。一個示例是使用廣播地址sendto 未使用setsockopt(SO_BROADCAST)設置廣播權限。

另一個可能的原因WSAEACCES錯誤是,當綁定 函數被調用(在Windows NT 4.0 SP4或更高版本),另一種 應用程序,服務或內核模式驅動程序綁定到獨家同一 地址訪問。這種獨佔訪問是Windows NT 4.0 SP4和更高版本中的新功能 ,並通過使用 SO_EXCLUSIVEADDRUSE選項實現。

+0

謝謝,但我已經找到這個信息。我的代碼使用SocketOptionName.ReuseAddress,我認爲它基本上是SO_EXCLUSIVEADDRUSE,但採用.NET方式。似乎有需要有其他方式來做到這一點,但我似乎無法找到它 – brooc 2012-02-02 22:01:05

+0

我這將是一個很好的謎題..我想我也是在正確的軌道上..我會幫助挖更多,並嘗試代碼以及​​..它可能是如此簡單,即使是最經驗豐富的編碼器會看起來它.. – MethodMan 2012-02-02 22:03:03

+0

謝謝,我一直在尋找這個好幾天...沒有運氣......這應該是一個非常簡單的事情...請嘗試運行此代碼。 – brooc 2012-02-02 22:06:29

0

甚至改變你的代碼,這樣我可以在我得到它看來,你不能綁定到同一個端口,只有一個端口可以在這裏使用 相同的錯誤消息的IP地址傳遞是示例代碼我用你的榜樣和改變它從我的本地機器捕捉我的IP ..

 IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0]; 
     IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); 

     //IPEndPoint localpt = new IPEndPoint(ipLocalEndPoint); 

     UdpClient udpServer = new UdpClient(ipLocalEndPoint); 
     udpServer.Client.SetSocketOption(
      SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
     udpServer.Connect(ipLocalEndPoint); 
     UdpClient udpServer2 = new UdpClient(); 
     udpServer2.Client.SetSocketOption(
      SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 

     udpServer2.Client.Bind(ipLocalEndPoint); // <<---------- Exception here 

這將產生對bind()方法除外..對不起。

+0

有必須要做到這一點。我的目標是創建一個包含UDP打孔的系統。這取決於我可以在同一個端口上收聽我發送數據的事實... – brooc 2012-02-02 22:42:07

0

爲了解決WSAEACCESS 10013(MSDN)在UDP的應用程序異常,你可以嘗試

udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); 
相關問題