Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintStream;
  7. import java.net.Socket;
  8. import java.net.UnknownHostException;
  9.  
  10. public class Client implements Runnable {
  11. static Socket communicationSocket = null;
  12. static BufferedReader serverInputStream = null;
  13. static PrintStream serverOutputStream = null;
  14. static BufferedReader consoleInput = null;
  15.  
  16. public static void main(String[] args) {
  17.  
  18. try {
  19. communicationSocket = new Socket("localhost", 9000);
  20. serverOutputStream = new PrintStream(communicationSocket.getOutputStream());
  21. serverInputStream = new BufferedReader(new InputStreamReader(communicationSocket.getInputStream()));
  22. consoleInput = new BufferedReader(new InputStreamReader(System.in));
  23. new Thread(new Client()).start();
  24.  
  25. String input;
  26. while(true) {
  27. input = serverInputStream.readLine();
  28. System.out.println(input);
  29. if(input.startsWith(">>>Dovidjenja ")) {
  30. break;
  31. }
  32. }
  33.  
  34. communicationSocket.close();
  35.  
  36.  
  37. } catch (UnknownHostException e) {
  38. System.out.println("UNKNOWN HOST");
  39. e.printStackTrace();
  40. } catch (IOException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44.  
  45. }
  46.  
  47. @Override
  48. public void run() {
  49.  
  50. try {
  51. String msg;
  52. while(true) {
  53. msg = consoleInput.readLine();
  54.  
  55. if(msg.equals("//quit")) {
  56. break;
  57. }
  58.  
  59. serverOutputStream.println(msg);
  60. }
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64.  
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement