Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.DatagramPacket;
  3. import java.net.DatagramSocket;
  4. import java.net.InetAddress;
  5.  
  6.  
  7. public class DatagramServer {
  8.  
  9. public static final int PORT = 8200;
  10. private DatagramSocket socket = null;
  11. DatagramPacket cerere, raspuns = null;
  12.  
  13. public void start() throws IOException {
  14. socket = new DatagramSocket(PORT);
  15. try {
  16. while (true) {
  17.  
  18. byte[] buf = new byte[256];
  19. cerere = new DatagramPacket(buf, buf.length);
  20. System.out.println(" Asteptam un pachet ... ");
  21. socket.receive(cerere);
  22.  
  23. InetAddress adresa = cerere.getAddress();
  24. int port = cerere.getPort();
  25.  
  26. String mesaj = " Hai " + new String(cerere.getData());
  27. buf = mesaj.getBytes();
  28.  
  29.  
  30. raspuns = new DatagramPacket(buf, buf.length, adresa, port);
  31. socket.send(raspuns);
  32. }
  33. } finally {
  34. if (socket != null)
  35. socket.close();
  36. }
  37. }
  38.  
  39. public static void main(String[] args) throws IOException {
  40. new DatagramServer().start();
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement