import java.io.*; import java.net.*; public class Client extends Thread { Socket s; public Client(String serverhost, int port, ClientBehavior cb) { try { s = new Socket(serverhost, port); cb.setStreams(s.getOutputStream(), s.getInputStream()); cb.setSocket(s); (new Thread(cb)).start(); } catch (Exception e) { e.printStackTrace(); } } public static interface ClientBehavior extends Runnable { public void setStreams(OutputStream os, InputStream is); public void setSocket(Socket s); public void closeSocket(); } // just for convenience... public static class ClientBehaviorAdapter implements ClientBehavior { protected OutputStream os; protected InputStream is; protected Socket socket; public void setStreams(OutputStream os, InputStream is) { this.os = os; this.is = is; } public void setSocket(Socket s) { socket = s; } public void closeSocket() { try { socket.close(); } catch (Exception e) { e.printStackTrace(); } } public void run() { } } }