List of usage examples for java.net DatagramPacket getLength
public synchronized int getLength()
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 . ja v a2 s . co 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[] argv) throws Exception { byte[] inbuf = new byte[256]; // default size DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); socket.receive(packet);/*w w w.ja v a2 s . co m*/ int numBytesReceived = packet.getLength(); System.out.println(numBytesReceived); }
From source file:WriteServer.java
public static void main(String args[]) throws Exception { int clientPort = 999; int buffer_size = 1024; byte buffer[] = new byte[buffer_size]; DatagramSocket ds = new DatagramSocket(clientPort); while (true) { DatagramPacket p = new DatagramPacket(buffer, buffer.length); ds.receive(p);/*from ww w . j a v a2 s . c o m*/ System.out.println(new String(p.getData(), 0, p.getLength())); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { MulticastSocket msocket = new MulticastSocket(9999); byte[] inbuf = new byte[1024]; DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); msocket.receive(packet);//from ww w . j a v a 2 s . c om // Data is now in inbuf int numBytesReceived = packet.getLength(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int mcPort = 12345; String mcIPStr = "230.1.1.1"; MulticastSocket mcSocket = null; InetAddress mcIPAddress = null; mcIPAddress = InetAddress.getByName(mcIPStr); mcSocket = new MulticastSocket(mcPort); System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress()); mcSocket.joinGroup(mcIPAddress);/*from www .jav a2 s .c o m*/ DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); System.out.println("Waiting for a multicast message..."); mcSocket.receive(packet); String msg = new String(packet.getData(), packet.getOffset(), packet.getLength()); System.out.println("[Multicast Receiver] Received:" + msg); mcSocket.leaveGroup(mcIPAddress); mcSocket.close(); }
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;// w ww . j av a2s .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:UDPReceive.java
public static void main(String args[]) { try {/*from w w w . j ava2 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:UDPReceive.java
public static void main(String args[]) { try {/*from ww w . j a va 2 s. c o m*/ if (args.length != 1) throw new IllegalArgumentException("Wrong number of args"); // Get the port from the command line int port = Integer.parseInt(args[0]); // Create a socket to listen on the port. DatagramSocket dsocket = new DatagramSocket(port); // Create a buffer to read datagrams into. If anyone sends us a // packet containing more than will fit into 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. for (;;) { // Wait to receive a datagram dsocket.receive(packet); // Decode the bytes of the packet to characters, using the // UTF-8 encoding, and then display those characters. String msg = new String(buffer, 0, packet.getLength(), "UTF-8"); System.out.println(packet.getAddress().getHostName() + ": " + msg); // Reset the length of the packet before reusing it. // Prior to Java 1.1, we'd just create a new packet each time. packet.setLength(buffer.length); } } catch (Exception e) { System.err.println(e); System.err.println(usage); } }
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) {// w ww .jav a 2s . 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:MulticastSniffer.java
public static void main(String[] args) { InetAddress ia = null;// w w w.ja va 2s . co m byte[] buffer = new byte[65509]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length); int port = 0; try { try { ia = InetAddress.getByName(args[0]); } catch (UnknownHostException e) { // } port = Integer.parseInt(args[1]); } // end try catch (Exception e) { System.err.println(e); System.err.println("Usage: java MulticastSniffer MulticastAddress port"); System.exit(1); } try { MulticastSocket ms = new MulticastSocket(port); ms.joinGroup(ia); while (true) { 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); } }