Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import java.io.DataOutputStream;
  2. import java.io.BufferedInputStream;
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. import static java.lang.System.exit;
  9.  
  10. //Thread class to handle input -- receiving data from server
  11. class ClientInputThread implements Runnable {
  12. //Instance variables
  13. private Socket socket;
  14. private Thread t;
  15.  
  16. //Constructor.
  17. ClientInputThread(Socket s) {
  18. this.socket = s;
  19. }
  20.  
  21. public void run() {
  22. //Set up connection variables
  23. DataInputStream input = null;
  24. try {
  25. input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  26. } catch (IOException e) {
  27. System.err.println(e);
  28. exit(1);
  29. }
  30.  
  31. //Program loop. Echo text to the console. Loop ends when main thread kills this thread
  32. String text = "a";
  33. while (!text.equals("") && !t.isInterrupted()) {
  34. try {
  35. text = input.readUTF();
  36. System.out.println(text);
  37. } catch (IOException e) {
  38. }
  39. }
  40.  
  41. try {
  42. input.close();
  43. this.socket.close();
  44. } catch (IOException e) {
  45. System.err.println(e);
  46. exit(1);
  47. }
  48. }
  49.  
  50. public void start() {
  51. if(this.t == null) {
  52. t = new Thread(this);
  53. t.start();
  54. }
  55. }
  56.  
  57. public void kill() {
  58. t.interrupt();
  59. }
  60. }
  61.  
  62. public class Client {
  63.  
  64. public static void main(String[] args) {
  65. //Set up connection variables
  66. String hostName = args[0];
  67. int portNumber = Integer.parseInt(args[1]);
  68. Socket socket = null;
  69. Scanner console = new Scanner(System.in);
  70. DataOutputStream output = null;
  71.  
  72. //Establish connection to server
  73. try {
  74. socket = new Socket(hostName, portNumber);
  75. } catch (IOException e) {
  76. System.err.println(e);
  77. exit(1);
  78. }
  79.  
  80. //Get output stream
  81. try {
  82. output = new DataOutputStream(socket.getOutputStream());
  83. } catch (IOException e) {
  84. System.err.println(e);
  85. exit(1);
  86. }
  87.  
  88. //Create a separate thread to handle receiving data from server.
  89. ClientInputThread in = new ClientInputThread(socket);
  90. in.start();
  91.  
  92. //Program loop. Send commands to server.
  93. String line = "a", userName = null, password = null;
  94. while (!line.equals("/logout")) {
  95. try {
  96. line = console.nextLine();
  97. output.writeUTF(line);
  98. } catch (IOException e) {
  99. System.err.println(e);
  100. exit(1);
  101. }
  102. }
  103.  
  104. //Close connection.
  105. try {
  106. console.close();
  107. output.close();
  108. socket.close();
  109. in.kill();
  110. } catch (IOException e) {
  111. System.err.println(e);
  112. exit(1);
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement