fork(5) download
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintStream;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. class Server {
  11. public static ArrayList<ArrayList<Object>> connectedServers = new ArrayList<ArrayList<Object>>();
  12.  
  13. public static void main(String[] args) throws IOException {
  14. String host = takeInput("Host");
  15. int port = takeInputInt("Port");
  16.  
  17. Socket a = connectToServer(host, port);
  18. }
  19.  
  20. public static String takeInput(String inputName) throws IOException {
  21. System.out.print(inputName+": ");
  22. String input = br.readLine();
  23. return input;
  24. }
  25.  
  26. public static int takeInputInt(String inputName) throws IOException {
  27. System.out.print(inputName+": ");
  28. int input = 0;
  29.  
  30. Scanner inputInt = new Scanner(System.in);
  31. input = inputInt.nextInt();
  32. return input;
  33. }
  34.  
  35. public static Socket connectToServer(String host, int port) throws IOException {
  36. ArrayList<Object> element = new ArrayList<>();
  37. element.add(host);
  38. element.add(port);
  39.  
  40. Socket fellowServer = null;
  41. if (connectedServers.contains(element) != true) {
  42. fellowServer = new Socket(host, port);
  43. connectedServers.add(element);
  44. element.remove(host);
  45. element.remove(0);
  46. return fellowServer;
  47. }
  48. else{
  49. return null;
  50. }
  51. }
  52.  
  53. }
  54.  
Runtime error #stdin #stdout #stderr 0.1s 380672KB
stdin
Standard input is empty
stdout
Host: Port: 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:907)
	at java.util.Scanner.next(Scanner.java:1530)
	at java.util.Scanner.nextInt(Scanner.java:2160)
	at java.util.Scanner.nextInt(Scanner.java:2119)
	at Server.takeInputInt(Main.java:32)
	at Server.main(Main.java:15)