Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence...
Transcript of Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence...
![Page 2: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/2.jpg)
Plan du Cours
● Présentation du module● Les entrées/sorties en Java● Échanges de données sur le réseau
![Page 3: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/3.jpg)
Plan du Cours
● Présentation du module● Les entrées/sorties en Java● Échanges de données sur le réseau
![Page 4: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/4.jpg)
Objectifs
● Mettre en œuvre la programmation réseau en Java● Les bases de la communication réseau - Sockets● Connexion aux bases de données● Les appels aux méthodes distants - XML-RPC● Les Objets Distribués – RMI● Introduction aux web services - JAX-WS
● Tous les cours seront mis en ligne● e-bureau● Site http://cosy.univ-reims.fr/~lsteffenel
![Page 5: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/5.jpg)
Format de l'évaluation
● Contrôle continu● Notation de l'avancement sur chaque journée
● Projet de programmation● Énoncé à partir du 2ème cours● Évolution des sujets au fur et à mesure des cours● Rapport + code
![Page 6: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/6.jpg)
Plan du Cours
● Présentation du module● Les entrées/sorties en Java● Échanges de données sur le réseau
![Page 7: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/7.jpg)
E/S : une histoire de flux
● Lecture / écriture des fichiers, des flux réseaux ● Différentes type de stream sont disponibles (lecture
de bytes, de caractères, d’objets…)
● Au départ, deux super classes :● InputStream : lecture d’une séquence de bytes● OutputStream : écriture d’une séquence de bytes
● Après, une jungle de streams● FileInputStream, ObjectInputStream,
StringBufferInputStream, ByteArrayInputStream…
![Page 8: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/8.jpg)
E/S : une histoire de flux
● Toute manipulation d'entrée et sortie se fait à travers des flux de données● En sortie (écran) le format par défaut est le
caractère● En entrée (clavier) le format est l'octet (Byte)● Avec les fichiers et les flux réseau, on trouve
plusieurs formats
![Page 9: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/9.jpg)
Le package java.io
● Opérations d'entrées/sorties● Lecture de données séquentielles● Écriture de données séquentielles
● Flux définis● Objet InputStream
– System.in : flux d'entrée – le clavier
● Objets PrintStream– System.out : flux de sortie – l'écran
– System.err : flux de sortie d'erreurs – l'écran
![Page 10: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/10.jpg)
Affichage à l'écran
● Affichage simple● System.out.println()
● System.out.print()
● Affichage formatée● System.out.format()
● System.out.format("%f, %1$+020.10f \n", Math.PI);
3.141593, +00000003.1415926536
![Page 11: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/11.jpg)
Entrée à partir du clavier
● La classe System.in● Ne peut pas être utilisée directement (car c'est un
flux d'octets, pas de caractères)
● La solution : associer System.in● à une classe InputStreamReader/BufferedReader
– Opérations de bas niveau
● à la classe Scanner
● à la classe Console– Services supplémentaires
![Page 12: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/12.jpg)
La classe InputStreamReader
● Gestion d'un flux de caractères en entrée● Utilise un flux d'entrée en octets comme source● Méthodes
● InputStreamReader(java.io.InputStream)
– Constructeur● int read () throws IOException
– Lecture d'un caractère● void close() throws IOException
– Fermeture du flot
![Page 13: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/13.jpg)
![Page 14: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/14.jpg)
Mise en tampon - buffering
● Principe : stockage d'informations dans une zone provisoire (tampon)● Gain de performance
● Applications● Lecture d'une chaîne de caractères● Écriture d'une chaîne de caractères
![Page 15: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/15.jpg)
La classe BufferedReader
● Mise en tampon d'un flux de caractères● Méthodes
● BufferedReader(Reader in)
– Constructeur● int read () throws IOException
– Lecture d'un caractère● String readLine () throws IOException
– Lecture d'une chaîne de caractères● void close() throws IOException
– Fermeture du flot
![Page 16: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/16.jpg)
![Page 17: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/17.jpg)
Les Scanners
● Objectif : simplifier la lecture depuis les flux● Lecture d'éléments depuis le clavier● Lecture d'éléments depuis un fichier
● Classe java.util.Scanner
● Utilisation possible des expressions régulières pour définir des séparateurs
![Page 18: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/18.jpg)
Construction d'objets Scanner
● Constructeurs● Scanner(InputStream src)
– Depuis un flux d'entrée● Scanner (File src)
– Depuis un fichier● Scanner (Readable src)
– Depuis une source implémentant Readable
![Page 19: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/19.jpg)
Accès aux Données
● Lecture en fonction du type● Schéma global : XXX nextXXX()
● int nextInt()
– Lecture d'un entier● double nextDouble()
– Lecture d'un double● String nextLine()
– Lecture d'une chaîne
![Page 20: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/20.jpg)
Méthodes de Test
● Test de la prochaine donnée● Test de l'existence d'une donnée
● boolean hasNext()
● Test du type de la prochaine donnée● boolean hasNextXXX()
● boolean hasNextInt()
● boolean hasNextDouble()
● boolean hasNextString()
![Page 21: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/21.jpg)
La classe Console
● La classe Scanner ne convient pas à la lecture d'un mot de passe
● Utilisation de la classe Console (Java SE 6)
Console cons = System.console();
String username = cons.readLine("User name: ");
Char[] passwd = cons.readPassword("Password: ");
![Page 22: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/22.jpg)
Entrée/sortie vers les fichiers
● Gestion du système de fichiers● Copie● Suppression● Gestion de droits● ...
● Gestion des contenus● Écriture dans un fichier● Lecture dans un fichier● ...
![Page 23: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/23.jpg)
Gestion du Système de Fichiers
● La classe java.io.File
● Pas d'opération sur les flux● Pas de lecture● Pas d'écriture
● Gestion indépendante de la plate-forme
![Page 24: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/24.jpg)
La classe File
● Méthodes● Constructeur
– Public File(String pathname)
● Manipulation du nom– String getName() // num du fichier seul
– String getPath() // nom et chemin
● Test des propriétés– boolean canRead()
– boolean canWrite()
– boolean isDirectory()
– boolean ifFile()
![Page 25: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/25.jpg)
Écriture de fichiers de caractères
● La classe FileWriter
● Sous-classe de OutputStreamWriter
● Constructeurs● FileWriter (File) throws IOException
● FileWriter (String) throws IOException
● FileWriter (String, boolean) throws IOException
![Page 26: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/26.jpg)
Lecture de fichiers de caractères
● La classe FileReader
● Sous-classe de InputStreamReader
● Constructeurs● FileReader (File) throws IOException
● FileReader (String) throws IOException
![Page 27: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/27.jpg)
![Page 28: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/28.jpg)
Écriture de fichiers d'octets
● La classe FileOutputStream
● Sous-classe de OutputStream
● Constructeurs● FileOutputStream (File) throws FileNotFoundException
● FileOutputStream (String) throws FileNotFoundException
● FileOutputStream (String, boolean) throws FileNotFoundException
![Page 29: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/29.jpg)
Lecture de fichiers d'octets
● La classe FileInputStream
● Sous-classe de InputStream
● Constructeurs● FileInputStream (File) throws FileNotFoundException
● FileInputStream (String) throws FileNotFoundExceptionException
![Page 30: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/30.jpg)
![Page 31: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/31.jpg)
Sérialisation
● Mécanisme permettant de lire ou enregistrer des objets entiers ● Format binaire propre à Java● Interface Serializable
● Usage des streams ObjectOutputStream et ObjectInputStream
![Page 32: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/32.jpg)
![Page 33: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/33.jpg)
Plan du Cours
● Les entrées/sorties en Java● Échanges de données sur le réseau
![Page 34: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/34.jpg)
La classe InetAddress
● Représentation des adresses IP● Aucun constructeur public● Création● public static InetAddress InetAddress.getByName (String nomHote)
throws UnknownHostException
● public static InetAddress InetAddress.getLocalHost (String nomHote) throws UnknownHostException
● Méthodes● public String getHostName()
● public byte[] getAddress()
● public String getHostAddress()
![Page 35: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/35.jpg)
![Page 36: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/36.jpg)
Les Sockets
● Un socket est un canal de communication entre deux applications. ● Un socket est lié à un port de manière à être
identifiable par la couche TCP.
ouverture par leclient
établissement d'une connexion
![Page 37: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/37.jpg)
Les Sockets côté client
● La classe Socket● création et gestion de socket côté client● création des flux associés
● Méthodes● Constructeurs
Socket(String host, int port) throws IOExceptionSocket(InetAdress a, int port) throws IOException
● Fermetureclose() throws IOException
![Page 38: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/38.jpg)
Les Sockets côté client
● Méthodes (continuation)● Flux associés
OutputStream getOutputStream() throws IOExceptionInputStream getInputStream() throws IOException
● Récupération d'informations localesInetAddress getLocalAddress()int getLocalPort()
● Récupération d'informations distantesInetAddress getInetAddress()int getPort()
![Page 39: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/39.jpg)
![Page 40: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/40.jpg)
Sockets côté Serveur
● La classe ServerSocket
● création et gestion de socket côté serveur● création des flux associés
● Méthodes● constructeur
– ServerSocket(int port) throws IOException
● fermeture– void close() throws IOException
● attente bloquante de connexion – Socket accept() throws IOException
![Page 41: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/41.jpg)
Le code côté serveur
![Page 42: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/42.jpg)
Le code côté client
![Page 43: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/43.jpg)
Remarque
● Le code serveur ne permet qu'une connexion client à la fois● Utilisation de threads pour permettre des
connexions multiples● Structuration logique d'un serveur multiconnexions
while (true) { accepter une connexion; Créer une thread pour traiter l'appel du client; }
● On verra ça la semaine prochaine
![Page 44: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr.](https://reader035.fdocuments.net/reader035/viewer/2022062415/5fd8904fbb914a547e315680/html5/thumbnails/44.jpg)
Exercices
● Création d'une application client/serveur simple● Définition d'un protocole de communication● Création du serveur
– Traitement d'une seule requête à la fois– Log des accès stocké dans un fichier
● Création d'un client– Version 1 : appel par ligne de commande– Version 2 : interface graphique