這實際上比聽起來更復雜,因爲如果你有多個接口,廣播不會總是出現在所有的接口上。爲了解決這個問題,我創建了這個類。
public class MyUdpClient : UdpClient
{
public MyUdpClient() : base()
{
//Calls the protected Client property belonging to the UdpClient base class.
Socket s = this.Client;
//Uses the Socket returned by Client to set an option that is not available using UdpClient.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
}
public MyUdpClient(IPEndPoint ipLocalEndPoint) : base(ipLocalEndPoint)
{
//Calls the protected Client property belonging to the UdpClient base class.
Socket s = this.Client;
//Uses the Socket returned by Client to set an option that is not available using UdpClient.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
}
}
然後通過廣播發送UDP數據包,我使用類似下面的內容。我正在使用IPAddress.Broadcast
和MyUdpClient
,這與您的代碼不同。
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(LocalIP), 0);
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Broadcast, iTargetPort);
MyUdpClient sendUdpClient = new MyUdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
此外,你應該注意,當您使用特定ipaddress
代替廣播的路由表只發送它的地址相匹配的接口。
所以在你的例子中,使用單播。您需要將LocalIP
設置爲您要發送到的本地接口的IP。有三個接口,你將有三個本地IP,你需要選擇正確的一個使用。
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(LocalIP), 0);
IPEndPoint targetEndPoint = new IPEndPoint(TargetIP, iTargetPort);
MyUdpClient sendUdpClient = new MyUdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
由於路由被關閉,您可能會在所有接口上看到它,但您需要測試這個單播情況。
如果您不關心發送IP或端口,您可以使用以下代碼。
IPEndPoint targetEndPoint = new IPEndPoint(TargetIP, iTargetPort);
MyUdpClient sendUdpClient = new MyUdpClient();
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
或廣播
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Broadcast, iTargetPort);
MyUdpClient sendUdpClient = new MyUdpClient();
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
與IPAddress.Broadcast
的問題是,他們不會通過任何網關路由。爲了解決這個問題,你可以創建一個IPAddresses
的列表,然後循環併發送。此外,由於發送可能會失敗,您無法控制的網絡問題,您應該也有一個try/catch塊。
ArrayList ip_addr_acq = new ArrayList();
ip_addr_acq.Add(IPAddress.Parse("10.1.1.1")); // add to list of address to send to
try
{
foreach (IPAddress curAdd in ip_addr_acq)
{
IPEndPoint targetEndPoint = new IPEndPoint(curAdd , iTargetPort);
MyUdpClient sendUdpClient = new MyUdpClient();
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
Thread.Sleep(40); //small delay between each message
}
}
catch
{
// handle any exceptions
}
編輯:見上述變化具有多個接口的單播,也Problem Trying to unicast packets to available networks.
你能澄清你越是想嘗試,做什麼。 – 2009-07-08 04:47:58
我的應用程序將向10個以上系統中的應用程序發送消息。其中所有系統都在三個不同的網卡中。像10.1.x.x/10.2.x.x/10.4.x.x一樣,我只能在一個網卡10.1.x.x中收到消息,但不能在其他兩個網卡中收到消息。所以我想檢查網卡的可用性,然後發送消息。謝謝。 – Anuya 2009-07-08 04:54:59
所以消息只是要檢查網絡可用性,還是它有一些其他的有效載荷/含義? – 2009-07-08 05:10:49