import java.net.*; import java.io.*; public class MajClient extends Thread { private static final int PORT = 45678; private static final String HOST = "leo"; protected Socket _socket; protected BufferedReader _console; protected BufferedReader _in; protected PrintWriter _out; public MajClient(String host, int num) { try { _socket = new Socket(host, num); System.out.println("Client: connexion établie sur " + _socket.getInetAddress() + "(port = " + _socket.getPort() + ")"); _console = new BufferedReader(new InputStreamReader(System.in)); _in = new BufferedReader(new InputStreamReader(_socket.getInputStream())); _out = new PrintWriter(_socket.getOutputStream()); } catch(IOException e) { System.err.println("Client: " + e.getMessage()); try{_socket.close();} catch(IOException e1){} System.exit(1); } this.start(); } public void run() { try { while(true) { System.out.print("Client? "); System.out.flush(); _out.println(_console.readLine()); _out.flush(); String réponse = _in.readLine(); if(réponse == null) break; System.out.println("Client> " + réponse); } } catch(IOException e) { System.err.println("Client: " + e.getMessage()); } finally { try{_socket.close();} catch(IOException e){} System.out.println("Client: connexion terminée"); } } public static void main(String args[]) { int num; try { if(args.length == 0) { new MajClient(HOST, PORT); } else if(args.length == 1) { new MajClient(args[0], PORT); } else if((args.length == 2) && ((num = Integer.parseInt(args[1])) > 0)) { new MajClient(args[0], num); } } catch( NullPointerException e ) { System.out.println("\nUsage: java MajClient [] \n" ); System.exit(1); } } }