Advertisement
Nickpips

Untitled

Mar 26th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. public class prog01RelayTCP {
  9. public static void main(String[] argv) throws Exception {
  10. final int NUM_BYTES = 80;
  11.  
  12. String servip = "147.97.156.237";
  13. int servport = 11234;
  14. int clientport = 1234;
  15.  
  16. if (argv.length >= 4)
  17. {
  18. try {
  19. // Read server port
  20. servport = Integer.parseInt(argv[0]);
  21. } catch (NumberFormatException e) {
  22. System.err.println("First argument must be an integer");
  23. System.exit(1);
  24. }
  25.  
  26. // Read server IP address
  27. servip = argv[1];
  28.  
  29. try {
  30. // Read relay port
  31. clientport = Integer.parseInt(argv[2]);
  32. } catch (NumberFormatException e) {
  33. System.err.println("Third argument must be an integer");
  34. System.exit(1);
  35. }
  36.  
  37. try {
  38. // Read relay port
  39. int tmp = Integer.parseInt(argv[3]);
  40. // Change if argv[2] was actually the server port
  41. if( tmp != servport && clientport == servport )
  42. clientport = tmp;
  43. } catch (NumberFormatException e) {
  44. System.err.println("Fourth argument must be an integer");
  45. System.exit(1);
  46. }
  47. }
  48.  
  49. servip = "localhost";
  50. servport = 11234;
  51. clientport = 1234;
  52. ServerSocket clientSS = null;
  53. byte[] buf = new byte[NUM_BYTES];
  54. try {
  55. // Open socket for client
  56. clientSS = new ServerSocket(clientport);
  57. while (true) {
  58. // Connect to server
  59. Socket serverSocket = new Socket(servip, servport);
  60.  
  61. // Initialize server I/O
  62. Scanner servIn = new Scanner(serverSocket.getInputStream());
  63. PrintWriter servOut = new PrintWriter(serverSocket.getOutputStream(), true);
  64.  
  65. // Connect to client
  66. try (Socket clientSocket = clientSS.accept()) {
  67. System.out.println("Connected to Client");
  68.  
  69. // Initialize client I/O
  70. BufferedInputStream clientIn = new BufferedInputStream(clientSocket.getInputStream());
  71. PrintWriter clientOut = new PrintWriter(clientSocket.getOutputStream(), true);
  72.  
  73. // Read input from client
  74. int nRead = clientIn.read(buf, 0, NUM_BYTES);
  75.  
  76. // If the socket was closed, try again
  77. if (nRead <= 0) {
  78. // Close sockets
  79. clientIn.close();
  80. clientSocket.close();
  81. // Serve next one
  82. continue;
  83. }
  84.  
  85. // Relay client data to server
  86. servOut.print(new String(buf));
  87. servOut.flush();
  88. System.out.println("CLIENT: " + new String(buf));
  89.  
  90. // Relay server data to client
  91. String serverInput = servIn.nextLine();
  92. System.out.println("SERVER: " + serverInput);
  93. clientOut.println(serverInput);
  94. }
  95.  
  96. serverSocket.close();
  97. }
  98. } catch (IOException ioEx) {
  99. System.out.println("Exception");
  100. } finally {
  101. clientSS.close();
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement