Advertisement
Guest User

BippyGrippy

a guest
Mar 31st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.net.UnknownHostException;
  9. import java.util.Scanner;
  10.  
  11. public class EchoClient {
  12.  
  13. static final int PORT = 9984;
  14. static Scanner kb = new Scanner(System.in);
  15.  
  16. public static void main(String[] args) throws IOException {
  17. System.out.println("Please choose an option:");
  18. int option = 0;
  19. System.out.println("Host [1]");
  20. System.out.println("Connect [2]");
  21. while (true) {
  22. try {
  23. option = kb.nextInt();
  24. break;
  25. } catch (Exception e) {
  26. System.err.println("That option is invalid!");
  27. kb.nextLine();
  28. }
  29. }
  30. switch (option) {
  31. case 1:
  32. System.out.println("Hosting converstation...");
  33. host();
  34. break;
  35. case 2:
  36. System.out.println("Connect to what host?");
  37. String connectHost = "";
  38. while (true) {
  39. try {
  40. connectHost = kb.nextLine();
  41. break;
  42. } catch (Exception e) {
  43. System.err.println("That option is invalid!");
  44. kb.nextLine();
  45. }
  46. }
  47. client(connectHost);
  48. }
  49. }
  50.  
  51. public static void host() {
  52. System.out.println("Running on port " + PORT + ".");
  53. try {
  54. System.out.println(InetAddress.getLocalHost());
  55. } catch (UnknownHostException e1) {
  56. System.out.println("Issue finding IP.");
  57. }
  58.  
  59. while (true) {
  60. try (
  61. ServerSocket serverSocket =
  62. new ServerSocket(PORT);
  63. Socket clientSocket = serverSocket.accept();
  64. PrintWriter out =
  65. new PrintWriter(clientSocket.getOutputStream(), true);
  66. BufferedReader in = new BufferedReader(
  67. new InputStreamReader(clientSocket.getInputStream()));
  68. ) {
  69. String inputLine;
  70. while ((inputLine = in.readLine()) != null) {
  71. out.println(inputLine);
  72. }
  73. } catch (IOException e) {
  74. System.out.println("Exception caught when trying to listen on port "
  75. + PORT + " or listening for a connection.");
  76. System.out.println(e.getMessage());
  77. }
  78. }
  79. }
  80.  
  81. public static void client(String HOST) {
  82. try (
  83. Socket echoSocket = new Socket(HOST, PORT);
  84. PrintWriter out =
  85. new PrintWriter(echoSocket.getOutputStream(), true);
  86. BufferedReader in =
  87. new BufferedReader(
  88. new InputStreamReader(echoSocket.getInputStream()));
  89. BufferedReader stdIn =
  90. new BufferedReader(
  91. new InputStreamReader(System.in))
  92. ) {
  93. String userInput;
  94. while ((userInput = stdIn.readLine()) != null) {
  95. out.println(userInput);
  96. System.out.println(in.readLine());
  97. }
  98. } catch (UnknownHostException e) {
  99. System.err.println("Host " + HOST + " is looking pretty fishy there, bud.");
  100. System.exit(1);
  101. } catch (IOException e) {
  102. System.err.println("Couldn't get I/O for the connection to " + HOST);
  103. System.exit(1);
  104. }
  105.  
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement