Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class chatClient implements Runnable
- { private Socket socket = null;
- ChatInputThread n;
- BufferedReader console;
- PrintWriter streamOut;
- public String serverName="";
- int port;
- //private DataInputStream console = null;
- //private DataOutputStream streamOut = null;
- Whiteboard www;
- // public ChatInputThread chat;
- public void chatClient(String serverName, int serverPort)
- { System.out.println("Establishing connection. Please wait ...");
- this.serverName=serverName;
- port=serverPort;
- }
- public void run()
- {
- /*try
- {
- steamout.writeUTF("as");
- steamout.flush();
- }
- catch(IOException ioe)
- { System.out.println("Sending error: " + ioe.getMessage());
- }*/
- n=new ChatInputThread(www,streamOut);
- n.start();
- while(true)
- {
- try
- {
- String line = console.readLine();
- String Line=www.ccf.getText();
- www.ccf.setText(Line+line);
- }
- catch(Exception e)
- {
- System.out.print("chatclient error"+e);
- }
- }
- }
- public void Start(Whiteboard ww) throws IOException
- {
- www=ww;
- System.out.println("serverName"+serverName+port);
- try
- {
- socket = new Socket(serverName, port);
- System.out.println("Connected: " + socket);
- console = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- streamOut = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
- }
- catch(UnknownHostException uhe)
- { System.out.println("Host unknown: " + uhe.getMessage());
- }
- catch(IOException ioe)
- { System.out.println("Unexpected exception: " + ioe.getMessage());
- }
- //console = new DataInputStream(socket.getInputStream());
- new Thread(this).start();
- //console = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
- //streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
- //chat=new ChatInputThread(ww.cf,console);
- }
- public void stop()
- { try
- { if (console != null) console.close();
- if (streamOut != null) streamOut.close();
- if (socket != null) socket.close();
- }
- catch(IOException ioe)
- { System.out.println("Error closing ...");
- }
- }
- public chatClient(String host, String port)
- {
- chatClient(host, Integer.parseInt(port));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement