Advertisement
Guest User

chat client

a guest
Jun 2nd, 2011
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class chatClient implements Runnable
  5. { private Socket socket = null;
  6. ChatInputThread n;
  7. BufferedReader console;
  8. PrintWriter streamOut;
  9. public String serverName="";
  10. int port;
  11. //private DataInputStream console = null;
  12. //private DataOutputStream streamOut = null;
  13. Whiteboard www;
  14. // public ChatInputThread chat;
  15.  
  16. public void chatClient(String serverName, int serverPort)
  17. { System.out.println("Establishing connection. Please wait ...");
  18. this.serverName=serverName;
  19. port=serverPort;
  20.  
  21. }
  22. public void run()
  23. {
  24. /*try
  25. {
  26. steamout.writeUTF("as");
  27. steamout.flush();
  28. }
  29. catch(IOException ioe)
  30. { System.out.println("Sending error: " + ioe.getMessage());
  31. }*/
  32. n=new ChatInputThread(www,streamOut);
  33.  
  34. n.start();
  35. while(true)
  36. {
  37. try
  38. {
  39. String line = console.readLine();
  40. String Line=www.ccf.getText();
  41. www.ccf.setText(Line+line);
  42. }
  43. catch(Exception e)
  44. {
  45. System.out.print("chatclient error"+e);
  46. }
  47. }
  48.  
  49.  
  50.  
  51. }
  52. public void Start(Whiteboard ww) throws IOException
  53. {
  54. www=ww;
  55. System.out.println("serverName"+serverName+port);
  56. try
  57. {
  58.  
  59. socket = new Socket(serverName, port);
  60. System.out.println("Connected: " + socket);
  61. console = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  62. streamOut = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
  63.  
  64. }
  65. catch(UnknownHostException uhe)
  66. { System.out.println("Host unknown: " + uhe.getMessage());
  67. }
  68. catch(IOException ioe)
  69. { System.out.println("Unexpected exception: " + ioe.getMessage());
  70. }
  71. //console = new DataInputStream(socket.getInputStream());
  72. new Thread(this).start();
  73.  
  74.  
  75. //console = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  76. //streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
  77.  
  78.  
  79. //chat=new ChatInputThread(ww.cf,console);
  80.  
  81. }
  82. public void stop()
  83. { try
  84. { if (console != null) console.close();
  85. if (streamOut != null) streamOut.close();
  86. if (socket != null) socket.close();
  87. }
  88. catch(IOException ioe)
  89. { System.out.println("Error closing ...");
  90. }
  91. }
  92. public chatClient(String host, String port)
  93. {
  94. chatClient(host, Integer.parseInt(port));
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement