Share on Google+Share on Google+

Construct a DatagramSocket on an unspecified port

This page discusses - Construct a DatagramSocket on an unspecified port

Construct a DatagramSocket on an unspecified port


In this section we are going to explain the process to construct a DatagramPacket object in more generic way. Here we provide a complete example based on the method for creating the DatagramSocket object via DatagramSocket(). Firstly create a class DatagramSocketexample and initialize a object Client of DatagramSocket. Then we call DatagramSocket() method with new keyword. After that we use the try and catch block to know whether the code working correctly or not.

Here is the complete code of the Example :


public class DatagramSocketExample {
  public static void main(String[] args) {
  try {
  DatagramSocket Client = new DatagramSocket();
  System.out.println("datagram Socket recive at available port");
  catch (SocketException exp) {

Here is the Output of the Example :


roseindia>java DatagramSocketExample
datagram Socket recive at available port

Download This Form.



Posted on: April 17, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.