Advertisement
Nickpips

Untitled

Sep 16th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package im;
  2.  
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.net.InetAddress;
  7. import java.net.Socket;
  8. import java.util.Scanner;
  9.  
  10. public class ClientDebug {
  11.  
  12. static class MsgHandler implements Runnable {
  13. static public ObjectInputStream in;
  14.  
  15. MsgHandler(Socket connection) {
  16. try {
  17. System.out.println("Initializing input stream...");
  18. in = new ObjectInputStream(connection.getInputStream());
  19. System.out.println("Input stream initialized, now chatting.");
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25. public void run() {
  26. try {
  27. while (true) {
  28. Message msg = (Message) in.readObject();
  29. System.out.println("[" + msg.username + "]: " + msg.text);
  30. }
  31. } catch (ClassNotFoundException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. public Socket connection;
  42. public ObjectOutputStream out;
  43. public String ip;
  44. public int port;
  45.  
  46. public ClientDebug(String ip, int port) {
  47. this.ip = ip;
  48. this.port = port;
  49. try {
  50. System.out.println("Looking for server...");
  51. connection = new Socket(InetAddress.getByName(ip), port);
  52. System.out.println("Connected to server " + InetAddress.getByName(ip));
  53.  
  54. System.out.println("Initializing output stream...");
  55. out = new ObjectOutputStream(connection.getOutputStream());
  56. System.out.println("Output stream has been initialized.");
  57.  
  58. new Thread(new MsgHandler(connection)).start();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public void run() {
  65.  
  66. System.out.println("Creating thread to handle the sending of the messages");
  67. // Runnable send = () -> {
  68. //
  69. // System.out.println("Inside the send thread");
  70. // try (Scanner in = new Scanner(System.in)) {
  71. // while (true) {
  72. // out.writeObject(new Message(in.nextLine()));
  73. // System.out.println("Message hs been recognized and sent");
  74. // }
  75. // } catch (IOException e) {
  76. // e.printStackTrace();
  77. // }
  78. // };
  79. //
  80. // System.out.println("Send thread created, making a thread to read in
  81. // input");
  82. // Runnable read = () -> {
  83. //
  84. // System.out.println("Inside the read thread");
  85. // try {
  86. // while (true) {
  87. // System.out.println((Message) in.readObject());
  88. // System.out.println("Message has been read and displayed");
  89. // }
  90. // } catch (ClassNotFoundException e) {
  91. // e.printStackTrace();
  92. // } catch (IOException e) {
  93. // e.printStackTrace();
  94. // }
  95. // };
  96. // System.out.println("Sending and reading thread have been created, now
  97. // running the threads");
  98. // send.run();
  99. // read.run();
  100.  
  101. try (Scanner scan = new Scanner(System.in)) {
  102. Message msg;
  103.  
  104. while (true) {
  105.  
  106. System.out.println("Enter message: ");
  107. String text = scan.nextLine();
  108. msg = new Message(text);
  109. msg.username = "Pipitone";
  110. out.writeObject(msg);
  111. }
  112.  
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. }
  117.  
  118. public static void main(String[] args) {
  119. Scanner in = new Scanner("192.168.1.24 6789");
  120. new ClientDebug(in.next(), in.nextInt()).run();
  121. in.close();
  122. }
  123.  
  124. public static void debug(Object o) {
  125. System.out.println("\tDEBUG: " + o.toString().replaceAll("\n", "\n\tDEBUG: "));
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement