Fichier EchoClient.java
import java.io.*;
import java.net.*;
public class EchoClient {
// Variables de classe constantes : port et machine par defaut public static final int DEFAULT_PORT = 6789;
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_LINGER_TIME = 5;
// Variables d’instance (etat de EchoClient)
String host = null; // Machine du serveur
int port; // No de port du serveur (no de service) int lingerTime; // Tps d’expiration de socket apres fermeture Socket s = null; // Reference a la socket client
DataInputStream sin = null; // Flux en provenance du serveur PrintStream sout = null; // Flux a destination du serveur DataInputStream in = null; // Flux associe au clavier
//--- // METHODE main() : POINT D’ENTREE DU PROGRAMME CLIENT //--- public static void main(String[] args) {
// Le client
EchoClient client = null;
// Tests d’arguments et appel de constructeurs switch(args.length) {
case 0 : client = new EchoClient();
break;
case 1 : if (args[0].equals("?")) { usage();
System.exit(0);
}
client = new EchoClient(args[0]);
break;
case 2 : client = new EchoClient(args[0], args[1]);
break;
case 3 : client = new EchoClient(args[0], args[1], args[2]);
break;
default : usage();
throw new IllegalArgumentException();
}// switch()
// Creation d’objets utilitaires et traitement du service 1
client.initSocketAndStreams();
client.service();
}// main()
//--- // CONSTRUCTEURS //--- public EchoClient() {
host = new String(DEFAULT_HOST);
port = DEFAULT_PORT;
lingerTime = DEFAULT_LINGER_TIME; // Temps d’expiration }
public EchoClient(String theHost) { host = new String(theHost);
try {
// Test de validite de l’adresse
InetAddress dummy = InetAddress.getByName(theHost);
} catch (UnknownHostException e) {
System.err.println("Machine " + theHost + " inconnue.");
System.err.println("Essai avec " + DEFAULT_HOST);
host = new String(DEFAULT_HOST);
}
port = DEFAULT_PORT;
lingerTime = DEFAULT_LINGER_TIME;
}
public EchoClient(String theHost, String thePortString) { this(theHost);
try {
port = Integer.parseInt (thePortString);
} catch (NumberFormatException e) {
System.err.println("Numero de port invalide. " + DEFAULT_PORT + " Utilise.");
port = DEFAULT_PORT;
}
lingerTime = DEFAULT_LINGER_TIME;
}
protected EchoClient(String theHost, String thePortString, String theLingerTimeString) { this(theHost, thePortString);
try {
lingerTime = Integer.parseInt (theLingerTimeString);
} catch (NumberFormatException e) { lingerTime = DEFAULT_LINGER_TIME;
2
} }
//--- // FONCTIONS D’UTILISATION ET D’INITIALISATION //--- static void usage() {
System.out.println("Utilisation : " +
"java EchoClient <hostname> [<port>] [<tempsExpir>]");
System.exit(0);
}
public void initSocketAndStreams() {
try {
// Creer une socket pour se connecter a la machine et au port s = new Socket(host, port);
// Mise du temps d’expiration a la valeur specifiee // Voir la mise en garde dans le polycopie en cas reel if (lingerTime > 0)
s.setSoLinger(true, lingerTime);
else
s.setSoLinger(false, 0);
// Creer les flux pour lire et ecrire des lignes de texte // de et vers cette socket.
sin = new DataInputStream(s.getInputStream());
sout = new PrintStream(s.getOutputStream());
// Creer un flux pour lire des lignes de l’entree standard // (par defaut le clavier)
in = new DataInputStream(System.in);
// Signaler que l’on est connecte
System.out.println("Connecte a " + s.getInetAddress() + ":"+ s.getPort());
} catch (IOException e) { System.err.println(e);
try { if (s != null) s.close(); } catch (IOException e2) { ; } }
}
//--- // TRAITEMENT DU SERVICE //--- public void service() {
try {
String line;
3
while(true) {
// afficher une invite
System.out.print("EchoClient > ");
System.out.flush();
// lire une ligne de stdin; verifier l’EOF line = in.readLine();
if ((line == null) || (line.equals("quit"))) break;
// L’envoyer au serveur sout.println(line);
// Lire une ligne du serveur line = sin.readLine();
// Verifier si la connexion est fermee (par EOF) if (line == null) {
System.out.println("Connection fermee par le serveur.");
break;
}
// Puis ecrire la ligne sur stdout System.out.println(line);
}// while(true)
} catch (IOException e) { System.err.println(e); } // Etre sur de toujours fermer la socket
finally {
try { if (s != null) s.close(); } catch (IOException e2) { ; } }
}// service() }// class EchoClient
4