DatagramSocket: send(DatagramPacket p) : DatagramSocket « java.net « Java by API






DatagramSocket: send(DatagramPacket p)

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

import javax.swing.JPanel;

public class MainClass extends JPanel {

  public static void main(String[] args) {

    try {
      String data = "data in UDP";
      byte [] buffer = data.getBytes();

      DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
                new InetSocketAddress("localhost", 5002));

      DatagramSocket socket = new DatagramSocket(5003);

      System.out.println("Sending a packet...");
      socket.send(packet);
    }catch(IOException e)
    {
      e.printStackTrace();
    }
  }
}

           
       








Related examples in the same category

1.new DatagramSocket()
2.new DatagramSocket(int port)
3.DatagramSocket: receive(DatagramPacket packet)