List of usage examples for java.net DatagramSocket receive
public synchronized void receive(DatagramPacket p) throws IOException
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); System.out.println(dp.getOffset()); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {/* w w w. j a v a2s.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); dp.setData(buffer); } } catch (Exception e) { e.printStackTrace(); } }
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.setSocketAddress(InetSocketAddress.createUnresolved("google.com", 8080)); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {//from www . 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(); } }
From source file:UdpEchoClient.java
public static void main(String[] args) { InetAddress address;//from w ww . ja v a 2 s. com try { address = InetAddress.getByName(args[0]); } catch (UnknownHostException host) { System.out.println(host); return; } DatagramPacket pack = new DatagramPacket(testString.getBytes(), testString.length(), address, 7); DatagramPacket incoming = new DatagramPacket(new byte[256], 256); DatagramSocket sock = null; try { Calendar start, end; sock = new DatagramSocket(); start = Calendar.getInstance(); sock.send(pack); sock.setSoTimeout(5000); sock.receive(incoming); end = Calendar.getInstance(); String reply = new String(incoming.getData()); reply = reply.substring(0, testString.length()); if (reply.equals(testString)) { System.out.println("Success"); System.out.println("Time = " + (end.getTime().getTime() - start.getTime().getTime()) + "mS"); } else System.out.println("Reply data did not match"); } catch (SocketException socke) { System.out.println(socke); } catch (IOException ioe) { System.out.println(ioe); } finally { sock.close(); } }
From source file:Main.java
public static void main(String args[]) { try {//from w w w .ja 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:GetDate.java
public static void main(String args[]) throws Exception { byte msg[] = new byte[256]; DatagramSocket dgSocket = new DatagramSocket(); InetAddress destination = InetAddress.getByName("web.mit.edu"); DatagramPacket datagram = new DatagramPacket(msg, msg.length, destination, PortDayTime); dgSocket.send(datagram);/*from w w w . j a v a 2s. c o m*/ datagram = new DatagramPacket(msg, msg.length); dgSocket.receive(datagram); String received = new String(datagram.getData()); System.out.println("The time in Cambridge is now: " + received); dgSocket.close(); }
From source file:Main.java
public static void main(String args[]) { try {//from w w w. j av a2 s. c o m 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 {// ww w .j a v a 2 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:UdpEchoServer.java
public static void main(String[] args) { DatagramSocket sock; DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE); try {/*from w ww.j ava 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); } } }