UDP Packet self-construction?

  • Thread starter Thread starter Jonathan
  • Start date Start date
J

Jonathan

Is there any Class in C# which provide access to
construct a UDP packet
myself???

Currently I am using the UdpClient class in C#....
however, its method send(byte[] , int length, IPAddress
dest, int port) only
allow me to send some bytes to the other side......but
what i need is to
construct a packet myself.......i need to explicitly
construct the payload
of a UDP packet byte by byte by myself.....

Anyone knows the solution to my problem?~ I can't found
some relevant class
in the System.Net namespace....
 
The byte[] parameter is the payload of the UDP packet. If you want to craft
the header, you'll have to drop to the raw socket level at
System.Net.Sockets.Socket and build the whole thing by hand.

I haven't done this on the .NET Framework, but if it's anything like typical
BSD sockets you'll probably use something like this:

IPHostEntry iphe = Dns.Resolve("server");

IPEndPoint ipEndPoint = new IPEndPoint(iphe.AddressList[0], port);

Socket socket = new Socket(ipEndPoint.AddressFamily, SocketType.Raw,
ProtocolType.Udp);

After that you can use the Send method on the socket object where the byte[]
is the entire packet (including header). You can find the UDP RFC at
http://www.faqs.org/rfcs/rfc768.html.
 
Hi,

Many thanks to your answer, just 1 more question...

Is that the original byte[] parameter in the send method of UdpClient
object be constructed into one single UDP packet if the length of the data
is less than 65536 byte? ie. each call of udpclient.send(byte[] payload,
.....) will generate 1 UDP packet if payload.Length < 65536, otherwise the
payload will be splitted into multiple UDP packets??

Thanks!~
 
Back
Top