Search code examples
c#javadatagram

DatagramPacket Equivalent


What is a equivalent for this java code in c#

DatagramPacket dp = new DatagramPacket(output,output.length,pack.getAddress(),pack.getPort());
socket.send(dp);

where pack - DatagramPacket, and socket - DatagramSocket?


Solution

  • System.Net.Sockets.UdpClient provides User Datagram Protocol (UDP) network services.

    The UdpClient class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in one of the following two ways:

    • Create an instance of the UdpClient class using the remote host name and port number as parameters.

    • Create an instance of the UdpClient class and then call the Connect method.

    And:

    Send(Byte[], Int32) Sends a UDP datagram to a remote host.

    Or alternatively:

    Send(Byte[], Int32, IPEndPoint) Sends a UDP datagram to the host at the specified remote endpoint.

    This last one more closely matches your example code.