Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. try {
  2. int port = 58452;
  3.  
  4. // Create a socket to listen on the port.
  5. DatagramSocket dsocket = new DatagramSocket(port);
  6.  
  7. // Create a buffer to read datagrams into. If a
  8. // packet is larger than this buffer, the
  9. // excess will simply be discarded!
  10. byte[] buffer = new byte[2048];
  11.  
  12. // Create a packet to receive data into the buffer
  13. DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  14.  
  15. // Now loop forever, waiting to receive packets and printing them.
  16. while (true) {
  17. // Wait to receive a datagram
  18. dsocket.receive(packet);
  19.  
  20. // Convert the contents to a string, and display them
  21. String msg = new String(buffer, 0, packet.getLength());
  22. System.out.println(packet.getAddress().getHostName() + ": "
  23. + msg);
  24.  
  25. // Reset the length of the packet before reusing it.
  26. packet.setLength(buffer.length);
  27. }
  28. } catch (Exception e) {
  29. System.err.println(e);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement