//
// Client-serveur sur Internet - Application CLIENT
//
// <http://cui.unige.ch/java/exemples>
//


import java.io.*;
import java.net.*;

public class Client
{
  protected static final int PORT=45678;

  public static void erreur()
  {
    System.err.println("Client> Usage: java Client <hostname>");
    System.exit(1);
  }  // erreur

  public static void main(String[] args)
  {
    Socket s = null;
    String Machine;
    
    if (args.length == 1) {
      Machine = args[0];
    } else {
      Machine = "127.0.0.1"; // LocalHost
      // erreur();
    }
    
    try {
      s=new Socket( Machine, PORT);
      BufferedReader sin=new BufferedReader(new InputStreamReader(s.getInputStream()));
      BufferedReader console=new BufferedReader(new InputStreamReader(System.in));
      PrintStream sout=new PrintStream(s.getOutputStream());
      System.out.println("Client> Connexion : "+ s.getInetAddress()+ " port : "+s.getPort());

      String ligne;
      
      while (true) {
      	System.out.print("?");
      	System.out.flush();
      	ligne=console.readLine();
      	sout.println(ligne);
      	ligne=sin.readLine();
      	if (ligne==null) {
      	  System.out.println("Connexion stoppee par le serveur");
      	  break;
      	}
      	
      	System.out.println("Client> "+ ligne);
      }
    } catch (IOException e) {
      System.err.println(e);
    } finally {
      try {
        if ( s != null ) s.close();
      } catch (IOException e2){
      }
    }
  } // main
} // class Client

