List of usage examples for java.net DatagramPacket DatagramPacket
public DatagramPacket(byte buf[], int length)
From source file:Main.java
public static void main(String args[]) { try {//w w w .ja va 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); // 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 {/* w w w .j a va 2 s .com*/ 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: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 ww w . ja v a 2 s. com*/ 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 {/*from w w w . j a va 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); buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; dp.setData(buffer, 2, 5); } } 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 w w . j ava 2 s. c o 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: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) {//from ww w .ja va2 s. co 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:MainClass.java
public static void main(String[] args) throws Exception { int port = 0; InetAddress group = InetAddress.getByName("127.0.0.1"); MulticastSocket ms = new MulticastSocket(port); ms.joinGroup(group);/*from ww w .j av a 2s . c o m*/ byte[] buffer = new byte[8192]; while (true) { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ms.receive(dp); String s = new String(dp.getData()); System.out.println(s); } // ms.leaveGroup(group); // ms.close(); }
From source file:Main.java
public static void main(String args[]) { try {/*w w 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); dp.setData(buffer); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {/*from w ww . j a va 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); dp.setSocketAddress(InetSocketAddress.createUnresolved("google.com", 8080)); } } catch (Exception e) { e.printStackTrace(); } }
From source file:MulticastSniffer.java
public static void main(String[] args) { InetAddress ia = null;// w ww .j a v a 2 s .co m byte[] buffer = new byte[65535]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length); int port = 0; // read the address from the command line try { try { ia = InetAddress.getByName(args[0]); } catch (UnknownHostException e) { System.err.println(e); } port = Integer.parseInt(args[1]); } catch (Exception e) { System.err.println(e); System.err.println("Usage: java MulticastSniffer mcast-addr port"); System.exit(1); } System.out.println("About to join " + ia); try { MulticastSocket ms = new MulticastSocket(port); ms.joinGroup(ia); while (true) { System.out.println("About to receive..."); ms.receive(dp); String s = new String(dp.getData(), 0, 0, dp.getLength()); System.out.println(s); } } catch (SocketException se) { System.err.println(se); } catch (IOException ie) { System.err.println(ie); } }