• Aucun résultat trouvé

Socket client

N/A
N/A
Protected

Academic year: 2022

Partager "Socket client"

Copied!
2
0
0

Texte intégral

(1)

Fichier SingleThreadEchoServer.java

import java.io.*;

import java.net.*;

public class SingleThreadEchoServer {

public final static int DEFAULT_PORT = 6789;

public static void main (String[] args) throws IOException {

int port; // no de port du service

String line = null;

ServerSocket server;

Socket client;

DataInputStream in;

PrintStream out;

// Tests d’arguments switch(args.length) {

case 0 : port = DEFAULT_PORT;

break;

case 1 : try {

port = Integer.parseInt (args[0]);

} catch (NumberFormatException e) { port = DEFAULT_PORT;

} break;

default :

throw new IllegalArgumentException ("Syntaxe : STServer [<port>]");

}

// Creation de socket serveur

System.out.println ("Demarrage sur le port " + port);

server = new ServerSocket (port);

System.out.println ("En ecoute ...");

// Boucle generale et service while (true) {

// Acceptation de connexion client = server.accept();

System.out.println ("Connexion a " + client.getInetAddress() +

" acceptee");

System.out.flush();

// Capture des flux de reception et d’envoi

in = new DataInputStream(client.getInputStream());

out = new PrintStream(client.getOutputStream());

//--- 1

(2)

// TRAITEMENT DU SERVICE D’ECHO //--- while (true) {

// lire une ligne line = in.readLine();

if ((line == null) || (line.equals("exit"))) break;

// ecrire la ligne out.println(line);

}

if (line == null) {

// Sortie du service : le client a termine

System.out.println ("Fermeture de socket client");

client.close ();

} else if (line.equals("exit")) { // Fermeture de socket serveur System.out.println ("Fermeture des socket client & serveur");

client.close ();

server.close ();

break;

}

}// while(true) }/* main() */

}/* class STServer */

2

Références

Documents relatifs

public static void main(String[] args) throws IOException { InputStream in=new FileInputStream(args[0]);.. Et avec

public static void main(String[] args) throws Exception { NotePad notes = new NotePad();. Invoker invoke = new

public static void main(String [] args) throws IOException{. ServerSocket serveur =

public static void main(String [] args) throws IOException{. ServerSocket serveur =

public static void main(String[] args) throws Exception { NotePad notes = new NotePad();. Invoker invoke = new

public static void main (String args []) throws Exception { // Création d'une Connection globale pour l'application UsineConnection uneUsineConnection =

// methode main() : point d’entree du programme public static void main(String[] args) {. // pour les entrees de donnees

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