Example usage for java.net DatagramSocket send

List of usage examples for java.net DatagramSocket send

Introduction

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

Prototype

public void send(DatagramPacket p) throws IOException 

Source Link

Document

Sends a datagram packet from this socket.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int mcPort = 12345;
    String mcIPStr = "230.1.1.1";
    DatagramSocket udpSocket = new DatagramSocket();

    InetAddress mcIPAddress = InetAddress.getByName(mcIPStr);
    byte[] msg = "Hello".getBytes();
    DatagramPacket packet = new DatagramPacket(msg, msg.length);
    packet.setAddress(mcIPAddress);//from  w w  w  .  ja va2  s .co m
    packet.setPort(mcPort);
    udpSocket.send(packet);

    System.out.println("Sent a  multicast message.");
    System.out.println("Exiting application");
    udpSocket.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {// w  w  w  .j  ava 2 s  .  com

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

        DatagramSocket ds = new DatagramSocket(InetSocketAddress.createUnresolved("google.com", 8080));

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    InetAddress server = InetAddress.getByName("localhost");
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    DatagramSocket theSocket = new DatagramSocket();
    while (true) {
        String theLine = userInput.readLine();
        if (theLine.equals("."))
            break;
        byte[] data = theLine.getBytes();
        DatagramPacket theOutput = new DatagramPacket(data, data.length, server, 99999);
        theSocket.send(theOutput);
    }//ww w .j  a va  2 s. c  o m

}

From source file:Main.java

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

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

        DatagramSocket ds = new DatagramSocket(8080, ia);

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));

        DatagramChannel channel = ds.getChannel();

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

From source file:WriteServer.java

public static void main(String args[]) throws Exception {
    int serverPort = 998;

    int buffer_size = 1024;

    byte buffer[] = new byte[buffer_size];

    DatagramSocket ds = new DatagramSocket(serverPort);
    int pos = 0;/*www . j a  v a2 s. co m*/
    while (true) {
        int c = System.in.read();
        switch (c) {
        case -1:
            System.out.println("Server Quits.");
            return;
        case '\r':
            break;
        case '\n':
            ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost(), 999));
            pos = 0;
            break;
        default:
            buffer[pos++] = (byte) c;
        }
    }

}

From source file:UdpEchoServer.java

public static void main(String[] args) {
    DatagramSocket sock;
    DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
    try {/*from w w  w .  j  av a 2 s.com*/
        sock = new DatagramSocket(7);
    } catch (SocketException e) {
        System.out.println(e);
        return;
    }
    // echo back everything
    while (true) {
        try {
            sock.receive(pack);
            sock.send(pack);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramSocket udpSocket = new DatagramSocket();
    String msg = null;//from   w w w  . j  av  a  2  s  . c  o  m
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter a  message  (Bye  to quit):");
    while ((msg = br.readLine()) != null) {
        if (msg.equalsIgnoreCase("bye")) {
            break;
        }
        DatagramPacket packet = Main.getPacket(msg);
        udpSocket.send(packet);
        udpSocket.receive(packet);
        displayPacketDetails(packet);
        System.out.print("Please enter a  message  (Bye  to quit):");
    }
    udpSocket.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);

    // read/*w ww .j ava2 s .  c o  m*/
    DatagramSocket sock = new DatagramSocket(1111);
    sock.receive(pack);
    String word = new String(pack.getData());
    System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
    System.out.println(word);
    sock.close();
    // write
    sock = new DatagramSocket();
    pack.setAddress(InetAddress.getByName(args[1]));
    pack.setData(args[2].getBytes());
    pack.setPort(1111);
    sock.send(pack);
    sock.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    final int LOCAL_PORT = 12345;
    final String SERVER_NAME = "localhost";
    DatagramSocket udpSocket = new DatagramSocket(LOCAL_PORT, InetAddress.getByName(SERVER_NAME));

    System.out.println("Created UDP  server socket at " + udpSocket.getLocalSocketAddress() + "...");
    while (true) {
        System.out.println("Waiting for a  UDP  packet...");
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        udpSocket.receive(packet);// w  ww  .  j  a v a2 s  .c o m
        displayPacketDetails(packet);
        udpSocket.send(packet);
    }
}

From source file:be.error.rpi.test.UdpTest.java

public static void main(String[] args) throws Exception {
    final InetAddress IPAddress = InetAddress.getByName("192.168.0.10");
    final DatagramSocket clientSocket = new DatagramSocket();

    new Thread() {
        @Override//from   ww  w  .ja v a  2s  . c o m
        public void run() {
            try {
                while (true) {
                    String s = "0:0:0:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

    new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    String s = "1:1:1:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

}