Java API Tutorial - Java DatagramSocket.disconnect()








Syntax

DatagramSocket.disconnect() has the following syntax.

public void disconnect()

Example

In the following code shows how to use DatagramSocket.disconnect() method.

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/* w w  w . j a  v  a 2  s  .  co m*/
public class Main {

  public static void main(String args[]) {
    try {

      InetAddress ia = InetAddress.getByName("www.java2s.com");

      DatagramSocket ds = new DatagramSocket();

      byte buffer[] = "hello".getBytes();
      DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

      ds.send(dp);
      ds.disconnect();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}