IFT1025: Programmation 2 - PowerPoint PPT Presentation

About This Presentation
Title:

IFT1025: Programmation 2

Description:

Identifier l'adresse du serveur laquelle envoyer une requ te de connexion ... Envoyer la r ponse l'adresse IP. Pr t recevoir une autre commande ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 22
Provided by: NIE125
Category:

less

Transcript and Presenter's Notes

Title: IFT1025: Programmation 2


1
IFT1025 Programmation 2
  • Internet
  • Jian-Yun Nie

2
Concepts
  • Communication sur lInternet
  • Connexion dans Java

3
Internet
  • Stockage de données (informations)
  • Serveur
  • Client
  • Connexion
  • Connexion entre un client et un serveur
  • Un canal de communication
  • Transmission
  • Protocole
  • définit les commandes
  • le format de données transmises

4
Schéma de communication typique
  • Serveur
  • Il est lancée, en attente de recevoir un message
    (commande)
  • Client
  • Demande à établir une connexion avec le serveur
  • Transmet une commande au serveur
  • Serveur
  • Reçoit la commande
  • Traite la commande
  • Renvoie la réponse
  • Client
  • Reçoit la réponse
  • Continue à traiter , transmet une autre commande,

5
Établir une connexion
  • Identifier ladresse du serveur à laquelle
    envoyer une requête de connexion
  • Adresse
  • Adresse IP (Internet Protocol) 4 octets (4
    entier 0-255)
  • 130.65.86.66
  • Domain Naming Service (DNS) le nom correspondant
    à une adresse IP
  • Ss_domaine. sous_domaine . domaine
  • java.sun.com, www.iro.umontreal.ca
  • Traduction de DNS en adresse IP par un serveur
    DNS
  • Serveur
  • Prêt à recevoir des requêtes des types préétablis
  • E.g. GET

6
Transmission de données
  • Transmettre caractère par caractère
  • Lent, inefficace
  • Par paquet (package)
  • Un tampon pour accumuler les données
  • Quand il est rempli, envoyer (et vider)
  • Plus efficace

7
Contrôle de transmission
  • TCP/IP
  • IP
  • soccupe de découper les données en paquets, les
    envoyer
  • TCP Transmission Control Protocol
  • Contrôle le bon fonctionnement de la transmission
  • Un paquet est-il envoyé et reçu correctement ?
    Sinon, renvoyer le paquet

8
Communication entre client et serveur
  • Server
  • Reçoit une requête
  • Adresse IP du client
  • Commande
  • Déterminer la réponse
  • Envoyer la réponse à ladresse IP
  • Prêt à recevoir une autre commande
  • Client
  • Envoie une requête au serveur
  • Attend à recevoir une réponse (ou un message
    derreur)

9
Protocole
  • Un serveur est établi pour communiquer selon un
    protocole
  • Canal de communication (no. de port)
  • 0 and 65,535
  • HTTP par défaut 80
  • Serveur Web prêt à recevoir les requêtes HTTP
  • Adresse dun document
  • Uniform Resource Locator (URL)
  • java.sun.com/index.html
  • Commande
  • GET /index.html HTTP/1.0 (suivie dune ligne
    blanche)
  • http//java.sun.com/index.html

10
Protocole HTTP
  • Command Meaning
  • GET Return the requested item
  • HEAD Request only the header information of
    an item
  • OPTIONS Request communications options of
    an item
  • POST Supply input to a server-side command
    and return the result
  • PUT Store an item on the server
  • DELETE Delete an item on the server
  • TRACE Trace server communication

11
Interprétation de la donnée
  • HTTP
  • Interprète les commande
  • Ninterprète pas les données
  • Navigateur
  • Interprète les données reçues
  • HTML format de données

12
En Java
  • Établir une connexion avec un serveur Web
  • Créer un socket entre Client et Serveur
  • Socket s new Socket(hostname, portnumber)
  • Socket s new Socket("java.sun.com", 80)

13
En Java
  • Obtenir les streams du Socket
  • InputStream instream s.getInputStream()
  • OutputStream outstream s.getOutputStream()
  • Caster les streams
  • Scanner in new Scanner(instream)
  • PrintWriter out new PrintWriter(outstream)
  • Fermer le socket
  • s.close()

14
Scanner
  • Implente iterator
  • boolean hasNext()
  • String next()

15
Exemple (BigJava)
  • Un programme pour obtenir une page web
  • Établir une connexion avec un serveur
  • Envoyer une requête
  • Recevoir la réponse
  • Fermer
  • java WebGet java.sun.com /
  • Lancer WebGet avec 2 paramètres
  • java.sun.com DNS
  • / page racine
  • Port par défaut 80

16
  • 01 import java.io.InputStream
  • 02 import java.io.IOException
  • 03 import java.io.OutputStream
  • 04 import java.io.PrintWriter
  • 05 import java.net.Socket
  • 06 import java.util.Scanner
  • 07
  • 14 public class WebGet
  • 15
  • 16 public static void main(String args)
    throws IOException
  • 17
  • 19
  • 20 String host
  • 21 String resource
  • 22
  • 23 if (args.length 2)
  • 24
  • 25 host args0
  • 37 final int HTTP_PORT 80
  • 38 Socket s new Socket(host, HTTP_PORT)
  • 39
  • 42 InputStream instream
    s.getInputStream()
  • 43 OutputStream outstream
    s.getOutputStream()
  • 44
  • 47 Scanner in new Scanner(instream)
  • 48 PrintWriter out new
    PrintWriter(outstream)
  • 49
  • 52 String command "GET " resource "
    HTTP/1.0\n\n"
  • 53 out.print(command)
  • 54 out.flush()
  • 55
  • 58 while (in.hasNextLine())
  • 59
  • 60 String input in.nextLine()
  • 61 System.out.println(input)
  • 62
  • 63

17
Résultat java WebGet
  • HTTP/1.1 200 OK
  • Server Sun-Java-System-Web-Server/6.1
  • Date Tue, 28 Mar 2006 200726 GMT
  • Content-type text/htmlcharsetISO-8859-1
  • Set-Cookie SUN_ID132.204.24.63218361143576446
    EXPIRESWednesday, 31-Dec-2025
  • 235959 GMT DOMAIN.sun.com PATH/
  • Set-cookie JSESSIONID519A024C45B4C300DA868D076CA
    33448Path/
  • Connection close
  • lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
    Transitional//EN" "http//www.w3.or
  • g/TR/html4/loose.dtd"gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtJava Technologylt/titlegt
  • ltmeta name"keywords" content"Java, platform" /gt
  • ltmeta name"collection" content"reference"gt
  • ltmeta name"description" content"Java technology
    is a portfolio of products tha
  • t are based on the power of networks and the idea
    that the same software should
  • run on many different kinds of systems and
    devices." /gt

18
Créer un serveur Web
  • Choisir un port non utilisé e.g. 8888
  • Construire un socket serveur
  • ServerSocket server new ServerSocket(8888)
  • Faire accepter les commande
  • Socket s server.accept()
  • BankService service new BankService(s, bank)
  • BankService(s, bank)
  • une classe créée localement pour donner des
    services (répondre aux requêtes reçues sur le
    socket s

19
Exemple BankService
  • BankService carries out the service
  • Implements the Runnable interface
  • Its run method will be executed in each thread
    that serves a client connection
  • run gets a scanner and writer from the socket,
    then executes
  • public void doService() throws IOException
  • while (true)
  • if (!in.hasNext()) return
  • String command in.next()
  • if (command.equals("QUIT")) return
  • executeCommand(command)

20
Exemple
  • 01 import java.io.InputStream
  • 02 import java.io.IOException
  • 03 import java.io.OutputStream
  • 04 import java.io.PrintWriter
  • 05 import java.net.Socket
  • 06 import java.util.Scanner
  • 07
  • 08 /
  • 09 Executes Simple Bank Access Protocol
    commands
  • 10 from a socket.
  • 11 /
  • 12 public class BankService implements Runnable
  • 13
  • 14 /
  • 15 Constructs a service object that
    processes commands
  • 16 from a socket for a bank.
  • 17 _at_param aSocket the socket
  • 18 _at_param aBank the bank
  • 19 /
  • 26 public void run()
  • 27
  • 28 try
  • 29
  • 30 try
  • 31
  • 32 in new Scanner(s.getInputStream(
    ))
  • 33 out new
    PrintWriter(s.getOutputStream())
  • 34 doService()
  • 35
  • 36 finally
  • 37
  • 38 s.close()
  • 39
  • 40
  • 41 catch (IOException exception)
  • 42
  • 43 exception.printStackTrace()
  • 44

21
Exemple
  • 51 public void doService() throws IOException
  • 52
  • 53 while (true)
  • 54
  • 55 if (!in.hasNext()) return
  • 56 String command in.next()
  • 57 if (command.equals("QUIT")) return
  • 58 else executeCommand(command)
  • 59
  • 60
  • 61
  • 89 private Socket s
  • 90 private Scanner in
  • 91 private PrintWriter out
  • 92 private Bank bank
  • 93
  • 63 Executes a single command.
  • 64 _at_param command the command to execute
  • 65 /
  • 66 public void executeCommand(String command)
  • 67
  • 68 int account in.nextInt()
  • 69 if (command.equals("DEPOSIT"))
  • 70
  • 71 double amount in.nextDouble()
  • 72 bank.deposit(account, amount)
  • 73
  • 74 else if (command.equals("WITHDRAW"))
  • 75
  • 76 double amount in.nextDouble()
  • 77 bank.withdraw(account, amount)
  • 78
  • 79 else if (!command.equals("BALANCE"))
  • 80
  • 81 out.println("Invalid command")
Write a Comment
User Comments (0)
About PowerShow.com