Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.net.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class Server implements Runnable {
  8.  
  9. ArrayList<Integer> portslist = new ArrayList<>();
  10. private byte[] buf = new byte[256];
  11. DatagramSocket socket;
  12.  
  13. public Server(ArrayList<Integer> portslist) {
  14.  
  15. this.portslist = portslist;
  16.  
  17. }
  18.  
  19. public static void main(String args[]) throws IOException {
  20.  
  21. for(int i=0; i<args.length; i++) {
  22. if(!args[i].matches("\\d*")) {
  23.  
  24. System.out.println("Program can be started by using list of ports");
  25. System.exit(0);
  26. }
  27. }
  28.  
  29. System.out.println("START");
  30.  
  31.  
  32. }
  33.  
  34. @Override
  35. public void run() {
  36.  
  37. while (true) {
  38.  
  39. try {
  40. socket = new DatagramSocket(2);
  41. } catch (SocketException e) {
  42.  
  43. e.printStackTrace();
  44. }
  45.  
  46. DatagramPacket packet = new DatagramPacket(buf, buf.length);
  47. try {
  48. socket.receive(packet);
  49. } catch (IOException e) {
  50.  
  51. e.printStackTrace();
  52. }
  53.  
  54. InetAddress address = packet.getAddress();
  55. int port = packet.getPort();
  56. packet = new DatagramPacket(buf, buf.length, address, port);
  57. String received = new String(packet.getData(), 0, packet.getLength());
  58.  
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement