44
Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel [email protected]

Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel [email protected]

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

Programmation Client / ServeurLicence Professionnelle CMSII

Cours 1

Luiz Angelo [email protected]

Page 2: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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

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

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

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

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

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

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

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

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

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

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
Page 14: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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

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
Page 17: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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

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

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

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

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

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

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

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

É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

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
Page 28: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

É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

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
Page 31: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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
Page 33: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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

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
Page 36: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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

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

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
Page 40: Programmation Client / Serveurlsteffenel/cours/LPro/... · Programmation Client / Serveur Licence Professionnelle CMSII Cours 1 Luiz Angelo Steffenel luiz-angelo.steffenel@univ-reims.fr

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

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

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

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

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