2011-05-25 20 views
5

न भेजें यह एक अजीब अनुरोध है।यूडीपी पैकेट कैसे बनाएं लेकिन इसे

मेरे पास एक बाइट सरणी है जिसे मुझे सी # का उपयोग करके किसी अन्य डिवाइस पर सीरियल पोर्ट पर भेजने की आवश्यकता है। हालांकि मुझे पहले एक udp पैकेट के भीतर बाइट सरणी को लपेटने की आवश्यकता है, लेकिन फिर से, यह सीरियल पोर्ट के माध्यम से भेजा जा रहा है, न कि udp के माध्यम से। क्या सीरियल पोर्ट पर भेजने के लिए बाइट्स की सरणी के रूप में एक udp पैकेट बनाने का कोई तरीका है?

मैंने पहले udp का उपयोग करके संदेश भेज और प्राप्त किए हैं लेकिन कभी भी udp पैकेट का निर्माण नहीं किया गया है लेकिन udp के माध्यम से नहीं भेजा गया है।

+0

तो आपको केवल आईपी परत पैकेट की आवश्यकता है, न कि ईथरनेट रैपर? –

+0

@ सैम स्कास इसे आईपी और यूडीपी हेडर दोनों की आवश्यकता होगी, यह सुनिश्चित नहीं है कि आईपी हेडर "ईथरनेट रैपर" है या नहीं, धन्यवाद। – Justin

उत्तर

1

मुझे लगता है कि मुझे UdpClient में .NET में कोई वास्तविक पैकेट-स्तरीय कक्षाएं नहीं दिख रही हैं क्योंकि स्पष्ट रूप से यह मृत सरल है क्योंकि यह एक कनेक्शन रहित/स्टेटलेस प्रोटोकॉल है।

The packet format is crazy simple

bits    0 – 15     16 – 31 
     +-----------------------+-------------------------+ 
0  | Source Port Number | Destination Port Number | 
     +-----------------------+-------------------------+ 
32  | Length    | Checksum    | 
     +-----------------------+-------------------------+ 
64  |             | 
     |      Data      | 
     |             | 
     +-------------------------------------------------+ 

Also, do note that the checksum computation does actually have a little bit of complexity to it

+3

यदि आप पैकेट मैन्युअल रूप से क्राफ्ट कर रहे हैं तो आपको भी खाते में अंतहीनता लेनी होगी। –

1

आप शायद एक यूडीपी वर्ग है कि एक मानक यूडीपी पैकेट में समाहित सभी डेटा शामिल बनाकर अपने यूडीपी पैकेट का निर्माण करना चाहिए।

स्रोत पोर्ट [सपा] (16 बिट):: जब एक कनेक्शन का प्रयास किया है, या आयोजित किया जा रहा है, इस निर्दिष्ट करता है क्या बंदरगाह स्थानीय मशीन से प्रतिक्रिया के लिए सुनने के लिए प्रतीक्षा कर रहा है

डाटा follows के रूप में है गंतव्य मशीन

गंतव्य पोर्ट [डीपी] (16 बिट्स): जब कोई उपयोगकर्ता रिमोट मशीन पर किसी सेवा से कनेक्ट करना चाहता है, तो एप्लिकेशन लेयर प्रोग्राम निर्दिष्ट करता है कि किस पोर्ट प्रारंभिक कनेक्शन का उपयोग करना चाहिए। प्रारंभिक कनेक्शन के हिस्से के रूप में नहीं, यह निर्दिष्ट करता है कि रिमोट मशीन के लिए किस पोर्ट नंबर का उपयोग किया जा रहा है क्योंकि एक पैकेट अपने गंतव्य पर भेजा जा रहा है।

लंबाई [लेन] (16 बिट्स): यह प्राप्तकर्ता स्टेशन को यह जानने के लिए अनुमति देता है कि कितने आने वाले बिट्स को वैध पैकेट का हिस्सा माना जाता है। लम्बाई यह है कि हेडर में बाइट्स सहित यूडीपी पैकेट का कितना बाइट हिस्सा है। चूंकि यूडीपी में हमेशा हेडर में 4 फ़ील्ड होते हैं और प्रत्येक में 16 बिट्स होते हैं, और वैरिएबल लम्बाई का डेटा/पेलोड, हम जानते हैं कि लंबाई 8 + (पेलोड में बाइट्स की संख्या होगी)

यूडीपी चेकसम [यूडीपीसीएस ] (16 बिट्स): यह एक चेकसम है जो यूडीपी पैकेट के हेडर और डेटा हिस्से को कवर करता है ताकि प्राप्तकर्ता मेजबान आने वाले यूडीपी पैकेट की अखंडता को सत्यापित कर सके। यूडीपी पैकेट चेकसम फ़ील्ड में एक पूर्वनिर्धारित संख्या से भरा हुआ है, और फिर जब चेकसम की गणना की जाती है, तो चेकसम पिछले मान पर लिखा जाता है। जब पैकेट गंतव्य पर आता है, तो गंतव्य मशीन का ओएस 4 हेडर फ़ील्ड (16 से 31 बिट्स से बने बाइट्स) को देखता है और उन्हें पैकेट से बाहर खींचता है, फिर चेकसम फ़ील्ड में किसी भी चीज़ के बिना पैकेट पर चेकसम की गणना करता है । फिर ओएस पैकेट में प्रसारित थेटे के साथ गणना की गई चेकसम की तुलना करता है। यदि चेकसम एक जैसा है, तो डेटा ठीक है, और इसे पारित करने की अनुमति है, लेकिन यदि कोई अंतर है, तो यूडीपी पैकेट, और डेटा गिरा दिया गया है, और प्राप्त करने के लिए मशीन प्राप्त करने का कोई प्रयास नहीं किया गया है प्रतिलिपि बनाएँ, और भेजने की मशीन उसी पैकेट को भेजने की कोशिश नहीं करेगी। पैकेट हमेशा के लिए खो गया है। यूडीपी विश्वसनीय नहीं है! एक विश्वसनीय परिवहन परत टीसीपी/आईपी सूट प्रोटोकॉल के लिए टीसीपी पैकेट देखें।

डेटा (परिवर्तनीय बिट्स): जैसा कि आप उम्मीद कर सकते हैं, यह यूडीपी पैकेट का पेलोड या डेटा हिस्सा है। पेलोड प्रोटोकॉल (अक्सर एप्लिकेशन लेयर) की संख्या हो सकती है। कुछ सामान्यतः उपयोग किए जाने वाले यूडीपी प्रोटोकॉल में एनएफएस, डीएनएस, साथ ही साथ मल्टीप्ले ऑडियो और वीडियो स्ट्रीमिंग प्रोटोकॉल शामिल हैं। यदि किसी यूडीपी पैकेट में कोई त्रुटि होती है और एक त्रुटि को ठीक करने के लिए वांछित किया जाता है, तो त्रुटि को खोजने के लिए इसे एप्लिकेशन परत पर छोड़ दिया जाता है और डेटा की "परत" या "खंड" का अनुरोध किया जाता है।

एक कक्षा बनाना जो इस डेटा को रखता है और इसे उचित रूप से भरता है, तो ToString ओवरलोड करता है ताकि आप बाइट सरणी में परिवर्तित हो सकें।

उम्मीद है कि इससे मदद मिलती है।

2

मैं योचई के उत्तर को स्वीकार करने जा रहा हूं, क्योंकि उस लिंक (और उस साइट के अन्य पृष्ठों) ने कोड को यूडप पैकेट और आईपी हेडर दोनों बनाने के लिए कोड प्रदान किया है। इसे पूरा करने की कोशिश कर दूसरों के लिए, यहाँ कोड है:

यह कैसे कॉल करने के लिए:

var udpPacketBytes = UDPPacket.Construct(IPAddress.Parse("1.1.1.1"), 1000, IPAddress.Parse("2.2.2.2"), 6100, payloadBytes); 

UDPPacket वर्ग:

public static class UDPPacket 
    { 
     public static byte[] Construct(IPAddress sourceAddress, ushort sourcePort, IPAddress destinationAddress, ushort destinationPort, byte[] payload) 
     { 
      var bindAddress = IPAddress.Any; 

      // Make sure parameters are consistent 
      //if ((sourceAddress.AddressFamily != destinationAddress.AddressFamily) || (sourceAddress.AddressFamily != bindAddress.AddressFamily)) 
      //{ 
      // throw new Exception("Source and destination address families don't match!"); 
      //} 

      // Start building the headers 
      byte[] builtPacket; 
      UdpHeader udpPacket = new UdpHeader(); 
      ArrayList headerList = new ArrayList(); 
      //Socket rawSocket = null; 
      //SocketOptionLevel socketLevel = SocketOptionLevel.IP; 

      // Fill out the UDP header first 
      Console.WriteLine("Filling out the UDP header..."); 
      udpPacket.SourcePort = sourcePort; 
      udpPacket.DestinationPort = destinationPort; 
      udpPacket.Length = (ushort)(UdpHeader.UdpHeaderLength + payload.Length); 
      udpPacket.Checksum = 0; 

      if (sourceAddress.AddressFamily == AddressFamily.InterNetwork) 
      { 
       Ipv4Header ipv4Packet = new Ipv4Header(); 

       // Build the IPv4 header 
       Console.WriteLine("Building the IPv4 header..."); 
       ipv4Packet.Version = 4; 
       ipv4Packet.Protocol = (byte)ProtocolType.Udp; 
       ipv4Packet.Ttl = 2; 
       ipv4Packet.Offset = 0; 
       ipv4Packet.Length = (byte)Ipv4Header.Ipv4HeaderLength; 
       ipv4Packet.TotalLength = (ushort)System.Convert.ToUInt16(Ipv4Header.Ipv4HeaderLength + UdpHeader.UdpHeaderLength + payload.Length); 
       ipv4Packet.SourceAddress = sourceAddress; 
       ipv4Packet.DestinationAddress = destinationAddress; 

       // Set the IPv4 header in the UDP header since it is required to calculate the 
       // pseudo header checksum 
       Console.WriteLine("Setting the IPv4 header for pseudo header checksum..."); 
       udpPacket.ipv4PacketHeader = ipv4Packet; 

       // Add IPv4 header to list of headers -- headers should be added in th order 
       // they appear in the packet (i.e. IP first then UDP) 
       Console.WriteLine("Adding the IPv4 header to the list of header, encapsulating packet..."); 
       headerList.Add(ipv4Packet); 
       //socketLevel = SocketOptionLevel.IP; 
      } 
      else if (sourceAddress.AddressFamily == AddressFamily.InterNetworkV6) 
      { 
       Ipv6Header ipv6Packet = new Ipv6Header(); 

       // Build the IPv6 header 
       Console.WriteLine("Building the IPv6 header..."); 
       ipv6Packet.Version = 6; 
       ipv6Packet.TrafficClass = 1; 
       ipv6Packet.Flow = 2; 
       ipv6Packet.HopLimit = 2; 
       ipv6Packet.NextHeader = (byte)ProtocolType.Udp; 
       ipv6Packet.PayloadLength = (ushort)(UdpHeader.UdpHeaderLength + payload.Length); 
       ipv6Packet.SourceAddress = sourceAddress; 
       ipv6Packet.DestinationAddress = destinationAddress; 

       // Set the IPv6 header in the UDP header since it is required to calculate the 
       // pseudo header checksum 
       Console.WriteLine("Setting the IPv6 header for pseudo header checksum..."); 
       udpPacket.ipv6PacketHeader = ipv6Packet; 

       // Add the IPv6 header to the list of headers - headers should be added in the order 
       // they appear in the packet (i.e. IP first then UDP) 
       Console.WriteLine("Adding the IPv6 header to the list of header, encapsulating packet..."); 
       headerList.Add(ipv6Packet); 
       //socketLevel = SocketOptionLevel.IPv6; 
      } 

      // Add the UDP header to list of headers after the IP header has been added 
      Console.WriteLine("Adding the UDP header to the list of header, after IP header..."); 
      headerList.Add(udpPacket); 

      // Convert the header classes into the binary on-the-wire representation 
      Console.WriteLine("Converting the header classes into the binary..."); 
      builtPacket = udpPacket.BuildPacket(headerList, payload); 

      /* 
      // Create the raw socket for this packet 
      Console.WriteLine("Creating the raw socket using Socket()..."); 
      rawSocket = new Socket(sourceAddress.AddressFamily, SocketType.Raw, ProtocolType.Udp); 

      // Bind the socket to the interface specified 
      Console.WriteLine("Binding the socket to the specified interface using Bind()..."); 
      rawSocket.Bind(new IPEndPoint(bindAddress, 0)); 

      // Set the HeaderIncluded option since we include the IP header 
      Console.WriteLine("Setting the HeaderIncluded option for IP header..."); 
      rawSocket.SetSocketOption(socketLevel, SocketOptionName.HeaderIncluded, 1); 

      try 
      { 
       // Send the packet! 
       Console.WriteLine("Sending the packet..."); 
       int rc = rawSocket.SendTo(builtPacket, new IPEndPoint(destinationAddress, destinationPort)); 
       Console.WriteLine("send {0} bytes to {1}", rc, destinationAddress.ToString()); 
      } 
      catch (SocketException err) 
      { 
       Console.WriteLine("Socket error occurred: {0}", err.Message); 
       // http://msdn.microsoft.com/en-us/library/ms740668.aspx 
      } 
      finally 
      { 
       // Close the socket 
       Console.WriteLine("Closing the socket..."); 
       rawSocket.Close(); 
      } 
      */ 

      return builtPacket; 
     } 
    } 

प्रोटोकॉल कक्षाएं:

(बहुत लंबा यहाँ पोस्ट करने के लिए)

Protocol Class Code

संबंधित मुद्दे