Example usage for java.net DatagramSocket DatagramSocket

List of usage examples for java.net DatagramSocket DatagramSocket

Introduction

In this page you can find the example usage for java.net DatagramSocket DatagramSocket.

Prototype

public DatagramSocket(int port) throws SocketException 

Source Link

Document

Constructs a datagram socket and binds it to the specified port on the local host machine.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {//from   ww  w .  j  a va2s  .  c  o  m

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

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

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("google.com"),
                    8080);

            ds.receive(dp);

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

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

From source file:Main.java

public static void main(String args[]) {
    try {/*from   w w w .  j  a va2s .  c  om*/

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

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

            DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length,
                    InetAddress.getByName("google.com"), 8080);

            ds.receive(dp);

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

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

From source file:Main.java

public static void main(String args[]) {
    try {/*www  .j  a  v  a  2  s.  c  om*/

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

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

            dp.setSocketAddress(InetSocketAddress.createUnresolved("google.com", 8080));

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {// ww w . j a v a  2s . co  m

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            // Receive data
            ds.receive(dp);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);

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

From source file:Main.java

public static void main(String args[]) {
    try {//from   ww w . j  a  v a 2  s.  c  o  m

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            // Receive data
            ds.receive(dp);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);
            // Display port from the datagram packet
            System.out.println(dp.getPort());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {// ww w  . ja va 2s. c  om

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            // Receive data
            ds.receive(dp);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);

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

From source file:Main.java

public static void main(String args[]) {
    try {/*from  w  w  w.  ja v  a2s . c  o  m*/

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            // Receive data
            ds.receive(dp);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);

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

From source file:UDPReceive.java

public static void main(String args[]) {
    try {//  w ww  .ja  v a  2 s  .  c o m
        int port = 90;

        // Create a socket to listen on the port.
        DatagramSocket dsocket = new DatagramSocket(port);

        // Create a buffer to read datagrams into. If a
        // packet is larger than this buffer, the
        // excess will simply be discarded!
        byte[] buffer = new byte[2048];

        // Create a packet to receive data into the buffer
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        // Now loop forever, waiting to receive packets and printing them.
        while (true) {
            // Wait to receive a datagram
            dsocket.receive(packet);

            // Convert the contents to a string, and display them
            String msg = new String(buffer, 0, packet.getLength());
            System.out.println(packet.getAddress().getHostName() + ": " + msg);

            // Reset the length of the packet before reusing it.
            packet.setLength(buffer.length);
        }
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  ww .  j a  v a2  s .c  om

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

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

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length,
                    InetSocketAddress.createUnresolved("google.com", 8080));

            ds.receive(dp);

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

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

From source file:Main.java

public static void main(String args[]) {
    try {// ww w  . j  a  v  a 2 s . c  o  m

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            // Receive data
            ds.receive(dp);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);

            buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            dp.setData(buffer, 2, 5);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}