Java DatagramSocket(int port) Constructor

Syntax

DatagramSocket(int port) constructor from DatagramSocket has the following syntax.

public DatagramSocket(int port)    throws SocketException

Example

In the following code shows how to use DatagramSocket.DatagramSocket(int port) constructor.


//w w w. ja  v a  2s . c  o  m

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

public class Main {
  private final static int BUFSIZE = 20;

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

      int port = 80;

      DatagramSocket ds = new DatagramSocket(port);

      while (true) {
        byte buffer[] = new byte[BUFSIZE];

        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

        ds.receive(dp);

        String str = new String(dp.getData());

        System.out.println(str);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}




















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder