Send a Datagram in Java

Description

The following code shows how to send a Datagram.

Example


  //w ww .j a va  2  s  .  c o  m



import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Main {
  public static void main(String[] argv) throws Exception {
    InetAddress dst = InetAddress.getLocalHost();
    int port = 8080;
    byte[] outbuf = new byte[1024];
    int len = 1024;

    DatagramPacket request = new DatagramPacket(outbuf, len, dst, port);
    DatagramSocket socket = new DatagramSocket();
    socket.send(request);
  }
}




















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode