Example usage for java.net DatagramPacket getPort

List of usage examples for java.net DatagramPacket getPort

Introduction

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

Prototype

public synchronized int getPort() 

Source Link

Document

Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramSocket ds = new DatagramSocket(3000);
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, 1024);
    ds.receive(dp);//from  ww w .  j  ava 2 s  .c o m
    String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress()
            + ":" + dp.getPort();
    System.out.println(strRecv);
    ds.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//from  ww w  .ja  va  2 s  .  co 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[]) {
    try {// w  w w. ja va 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[]) throws IOException {
    DatagramSocket socket = new DatagramSocket(DAYTIME_PORT);
    while (true) {
        byte buffer[] = new byte[256];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);/*from w ww. j  a v  a 2s . co m*/
        String date = new Date().toString();
        buffer = date.getBytes();
        // Get response address/port for client from packet
        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        packet = new DatagramPacket(buffer, buffer.length, address, port);
        socket.send(packet);
    }
}

From source file:UDP0.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);
    if (args[0].charAt(0) == 'r') {
        // read//w w  w . j  a v  a 2s  .  co  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();
    } else { // write
        DatagramSocket 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[]) {
    try {//from w w  w  .  j  a  v a 2 s  . 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);
            // Display port from the datagram packet
            System.out.println(dp.getPort());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ChatClient.java

public static void main(String[] args) throws Exception {
        int PORT = 4000;
        byte[] buf = new byte[1000];
        DatagramPacket dgp = new DatagramPacket(buf, buf.length);
        DatagramSocket sk;/*from w  w  w  .j  a v a 2s.c  om*/

        sk = new DatagramSocket(PORT);
        System.out.println("Server started");
        while (true) {
            sk.receive(dgp);
            String rcvd = new String(dgp.getData(), 0, dgp.getLength()) + ", from address: " + dgp.getAddress()
                    + ", port: " + dgp.getPort();
            System.out.println(rcvd);

            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            String outMessage = stdin.readLine();
            buf = ("Server say: " + outMessage).getBytes();
            DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort());
            sk.send(out);
        }
    }

From source file:ChatClient.java

  public static void main(String[] args) throws Exception {
  DatagramSocket s = new DatagramSocket();
  byte[] buf = new byte[1000];
  DatagramPacket dp = new DatagramPacket(buf, buf.length);

  InetAddress hostAddress = InetAddress.getByName("localhost");
  while (true) {// www .  j  a va2 s .c o m
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    String outMessage = stdin.readLine();

    if (outMessage.equals("bye"))
      break;
    String outString = "Client say: " + outMessage;
    buf = outString.getBytes();

    DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999);
    s.send(out);

    s.receive(dp);
    String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": "
        + new String(dp.getData(), 0, dp.getLength());
    System.out.println(rcvd);
  }
}

From source file:Main.java

public static void displayPacketDetails(DatagramPacket packet) {
    byte[] msgBuffer = packet.getData();
    int length = packet.getLength();
    int offset = packet.getOffset();

    int remotePort = packet.getPort();
    InetAddress remoteAddr = packet.getAddress();
    String msg = new String(msgBuffer, offset, length);

    System.out.println(/*w w w. j  av  a  2  s  .  co  m*/
            "Received a  packet:[IP Address=" + remoteAddr + ", port=" + remotePort + ", message=" + msg + "]");
}

From source file:Main.java

public static void displayPacketDetails(DatagramPacket packet) {
    byte[] msgBuffer = packet.getData();
    int length = packet.getLength();
    int offset = packet.getOffset();
    int remotePort = packet.getPort();
    InetAddress remoteAddr = packet.getAddress();
    String msg = new String(msgBuffer, offset, length);
    System.out.println("[Server at IP  Address=" + remoteAddr + ", port=" + remotePort + "]: " + msg);
}