|  | @@ -18,66 +18,91 @@ import java.net.UnknownHostException;
 | 
	
		
			
				|  |  |   * @author loquicom
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  public class TcpClient {
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Adresse du serveur
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      protected InetAddress adr;
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Port du serveur
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      protected int port;
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Socket TCP
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      protected Socket socket;
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * Timeout de la socket (en milisecondes)
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    protected int timeout = 0;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Flux d'entrée
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      protected BufferedReader input;
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Flux de sorti
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      protected PrintWriter output;
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /* --- Constructeurs --- */
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Creation d'un client TCP
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @param ip L'ip du serveur
 | 
	
		
			
				|  |  |       * @param port Le port du serveur
 | 
	
		
			
				|  |  | -     * @throws UnknownHostException 
 | 
	
		
			
				|  |  | +     * @throws UnknownHostException
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public TcpClient(String ip, int port) throws UnknownHostException{
 | 
	
		
			
				|  |  | +    public TcpClient(String ip, int port) throws UnknownHostException {
 | 
	
		
			
				|  |  |          this.adr = InetAddress.getByName(ip);
 | 
	
		
			
				|  |  |          this.port = port;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Creation d'un client TCP
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @param adr L'adresse du serveur
 | 
	
		
			
				|  |  |       * @param port Le port du serveur
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public TcpClient(InetAddress adr, int port){
 | 
	
		
			
				|  |  | +    public TcpClient(InetAddress adr, int port) {
 | 
	
		
			
				|  |  |          this.adr = adr;
 | 
	
		
			
				|  |  |          this.port = port;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /* --- Methodes --- */
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * Temps avant timeout du receive (qui retournera false)
 | 
	
		
			
				|  |  | +     * @param second Le temps en senconde
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public void timeout(int second) {
 | 
	
		
			
				|  |  | +        this.timeout = second * 1000;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * Retire un timeout
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public void notimeout() {
 | 
	
		
			
				|  |  | +        this.timeout = 0;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Connexion au serveur
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @return Reussite
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public boolean connect(){
 | 
	
		
			
				|  |  | +    public boolean connect() {
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  |              this.socket = new Socket(this.adr, this.port);
 | 
	
		
			
				|  |  | +            //Si un timeout
 | 
	
		
			
				|  |  | +            if (this.timeout > 0) {
 | 
	
		
			
				|  |  | +                this.socket.setSoTimeout(this.timeout);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //Ouverture flux
 | 
	
		
			
				|  |  |              this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
 | 
	
		
			
				|  |  | -            //this.output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())));
 | 
	
		
			
				|  |  |              this.output = new PrintWriter(this.socket.getOutputStream());
 | 
	
		
			
				|  |  |          } catch (IOException ex) {
 | 
	
		
			
				|  |  |              System.err.println("Impossible de se connecter au serveur : " + ex.getMessage());
 | 
	
	
		
			
				|  | @@ -85,36 +110,42 @@ public class TcpClient {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Envoi message au serveur
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @param msg Le message
 | 
	
		
			
				|  |  |       * @return Reussite
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public boolean send(String msg){
 | 
	
		
			
				|  |  | +    public boolean send(String msg) {
 | 
	
		
			
				|  |  |          output.print(msg);
 | 
	
		
			
				|  |  |          output.flush();
 | 
	
		
			
				|  |  |          return true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Reception d'un message du serveur
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @return Le message ou null en cas d'erreur
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public String receive(){
 | 
	
		
			
				|  |  | +    public String receive() {
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  |              return input.readLine();
 | 
	
		
			
				|  |  |          } catch (IOException ex) {
 | 
	
		
			
				|  |  | -            System.err.println("Impossible de lire : " + ex.getMessage());
 | 
	
		
			
				|  |  | +            //Si pas l'exception du timeout
 | 
	
		
			
				|  |  | +            if (!ex.getMessage().equals("Read timed out")) {
 | 
	
		
			
				|  |  | +                System.err.println("Impossible de lire : " + ex.getMessage() + " ");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              return null;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Ferme la connexion au serveur
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @return Reussite
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public boolean close(){
 | 
	
		
			
				|  |  | +    public boolean close() {
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  |              input.close();
 | 
	
		
			
				|  |  |              output.close();
 | 
	
	
		
			
				|  | @@ -125,9 +156,8 @@ public class TcpClient {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | -    /* --- Getter/Setter --- */
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /* --- Getter/Setter --- */
 | 
	
		
			
				|  |  |      public InetAddress getAdr() {
 | 
	
		
			
				|  |  |          return adr;
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -143,7 +173,5 @@ public class TcpClient {
 | 
	
		
			
				|  |  |      public void setPort(int port) {
 | 
	
		
			
				|  |  |          this.port = port;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |