यह वास्तव में यह लगता है कि यह एक से अधिक इंटरफ़ेस है, तो प्रसारण वास्तव में सभी इंटरफ़ेस पर नहीं जाएंगे। इस के आसपास पाने के लिए मैंने इस वर्ग को बनाया।
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);
}
}
फिर प्रसारण के माध्यम से यूडीपी पैकेट भेजने के लिए, मैं निम्नलिखित की तरह कुछ उपयोग करता हूं। मैं 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
सेट करने की आवश्यकता है, जिसे आप भेजना चाहते हैं। तीन इंटरफेस के साथ, आपके पास तीन स्थानीय आईपी होंगे और आपको उपयोग करने के लिए सही चुनना होगा।
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);
क्योंकि मार्ग बंद है क्योंकि आप इसे सभी इंटरफेस पर देख सकते हैं लेकिन आपको यूनिकास्ट केस के लिए इसका परीक्षण करने की आवश्यकता होगी।
यदि आपको आईपी या पोर्ट भेजने की परवाह नहीं है तो आप निम्न कोड का उपयोग कर सकते हैं।
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
की एक सूची बना सकते हैं और फिर लूप के माध्यम से भेज सकते हैं और भेज सकते हैं। चूंकि प्रेषण उन नेटवर्क समस्याओं के लिए असफल हो सकता है जिन्हें आप नियंत्रित नहीं कर सकते हैं, आपको एक कोशिश/पकड़ ब्लॉक भी होना चाहिए।
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.
आप और अधिक स्पष्ट कर सकते हैं कि आप क्या करते हैं की कोशिश कर चाहते हैं। –
मेरा आवेदन 10+ सिस्टम में किसी एप्लिकेशन को संदेश भेजेगा। जिसमें सभी सिस्टम तीन अलग-अलग नेटवर्क कार्ड में हैं। 10.1.x.x/10.2.x.x/10.4.x.x की तरह मैं केवल एक नेटवर्क कार्ड 10.1.x.x में संदेश प्राप्त करने में सक्षम हूं लेकिन अन्य दो नेटवर्क कार्डों में नहीं। तो मैं नेटवर्क कार्ड की उपलब्धि की जांच करना चाहता हूं और फिर संदेश भेजना चाहता हूं। धन्यवाद। – Anuya
क्या संदेश सिर्फ नेटवर्क उपलब्धता की जांच करने के लिए है, या क्या उसके पास कुछ अन्य पेलोड/अर्थ है? –