All pastes #34001 Raw Edit

Stuff

public java v1 · immutable
#34001 ·published 2005-12-17 20:14 UTC
rendered paste body
import java.io.*;import java.net.*;public class Client implements Runnable{  protected String host;  protected int port;  String yourname;  Thread t1;  public Client(){  }  public Client (String host, int port, String yourname) {    this.host = host;    this.port = port;    this.yourname=yourname;  }  protected DataInputStream dataIn;  protected DataOutputStream dataOut;  protected Thread listener;  public synchronized void start () throws IOException {    if (listener == null) {      Socket socket = new Socket (host, port);      try {        dataIn = new DataInputStream          (new BufferedInputStream (socket.getInputStream ()));        dataOut = new DataOutputStream          (new BufferedOutputStream (socket.getOutputStream ()));        dataOut.writeUTF (yourname+" s'est connecté\n ");      } catch (IOException ex) {        socket.close ();        throw ex;      }      listener = new Thread (this);      listener.start ();    }  }  public synchronized void stop () throws IOException {    //window.setVisible (false);    if (listener != null) {      listener.interrupt ();       listener = null;      dataOut.close ();    }  }  public void run() {    try {      while (!Thread.interrupted ()) {        String line = dataIn.readUTF ();        System.out.println(line);      }    } catch (IOException ex) {      handleIOException (ex);    }  }  protected synchronized void handleIOException (IOException ex) {    if (listener != null) {      System.out.println(ex);      if (listener != Thread.currentThread ())        listener.interrupt ();      listener = null;      try {        dataOut.close ();      } catch (IOException ignored) {      }    }  }/*  want to replace that by the initThread  public void actionPerformed (ActionEvent event) {    try {      dataOut.writeUTF (yourname+" dit:\n " +event.getActionCommand ());      dataOut.flush ();    } catch (IOException ex) {      handleIOException (ex);    }  }*/  void initThread() {    this.t1 = new Thread(new Runnable() {     public void run() {      try {        String userInput;        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));        while((userInput = stdIn.readLine()) != null){          dataOut.writeUTF (yourname+" dit:\n " + userInput);          dataOut.flush ();        }      } catch (IOException ex) {        handleIOException (ex);      }    }    });    this.t1.start();  }  public static void main(String[] args){        Client client = new Client("127.0.0.1",4444,"tony");    }}