TP1 : Correction. Rappels : Stream, Thread et Socket TCP



Similar documents
Creating a Simple, Multithreaded Chat System with Java

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Lesson: All About Sockets

Advanced Network Programming Lab using Java. Angelos Stavrou

Licence Informatique Année Exceptions

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

TP N 10 : Gestion des fichiers Langage JAVA

Network Communication

POB-JAVA Documentation

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Remote Method Invocation

Brazil + JDBC Juin 2001, douin@cnam.fr

Java Network. Slides prepared by : Farzana Rahman

Langages Orientés Objet Java

Network/Socket Programming in Java. Rajkumar Buyya

Division of Informatics, University of Edinburgh

Introduction au BIM. ESEB Seyssinet-Pariset Economie de la construction contact@eseb.fr

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Les fragments. Programmation Mobile Android Master CCI. Une application avec deux fragments. Premier layout : le formulaire

JAVA Program For Processing SMS Messages

TP : Système de messagerie - Fichiers properties - PrepareStatement

Mail User Agent Project

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Network Programming using sockets

Socket Programming in Java

Audit de sécurité avec Backtrack 5

Building a Multi-Threaded Web Server

Annexe - OAuth Introduction. Xavier de Rochefort xderoche@labri.fr - labri.fr/~xderoche 15 mai 2014

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Application Development with TCP/IP. Brian S. Mitchell Drexel University

!"# $ %!&' # &! ())*!$

Assignment 4 Solutions

Programmation RMI Sécurisée

Calcul parallèle avec R

Technical Service Bulletin

Note concernant votre accord de souscription au service «Trusted Certificate Service» (TCS)

Data Communication & Networks G

Thursday, February 7, DOM via PHP

The Need For Speed. leads to PostgreSQL. Dimitri Fontaine 28 Mars 2013

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

private byte[] encryptcommand = {(byte)0xa0, (byte)0xc1, (byte)0x00, (byte)0x00, (byte) 0x00};

Aucune validation n a été faite sur l exemple.

TP #4 b. ClientBourse.java Classe principale du client graphique et fonction main.

Installation troubleshooting guide

Liste d'adresses URL

TP JSP : déployer chaque TP sous forme d'archive war


System Requirements Orion

Archived Content. Contenu archivé

Repris de : Version Debian (de base)

Socket-based Network Communication in J2SE and J2ME

Personnalisez votre intérieur avec les revêtements imprimés ALYOS design

Sun Management Center Change Manager Release Notes

Archived Content. Contenu archivé

Archived Content. Contenu archivé

An Android-based Instant Message Application

CSS : petits compléments

Introduction. GEAL Bibliothèque Java pour écrire des algorithmes évolutionnaires. Objectifs. Simplicité Evolution et coévolution Parallélisme

User Manual Culturethèque Thailand

General Certificate of Education Advanced Level Examination June 2012

Brest. Backup : copy flash:ppe_brest1 running-config

ATP Co C pyr y ight 2013 B l B ue C o C at S y S s y tems I nc. All R i R ghts R e R serve v d. 1

Proposition d intervention

Archived Content. Contenu archivé

Office of the Auditor General / Bureau du vérificateur général FOLLOW-UP TO THE 2010 AUDIT OF COMPRESSED WORK WEEK AGREEMENTS 2012 SUIVI DE LA

Bibliothèque numérique de l enssib

Sun Enterprise Optional Power Sequencer Installation Guide

How To Set Up Vsftpd On A Pc Or Mac Or Mac (For Mac) On A Mac Or Ipa (For Pc Or Ipad) On Pc Or Pc Or Pb (For Ipa) On An Ipa Or Mac

Measuring Policing Complexity: A Research Based Agenda

sept-2002 Computer architecture and software cells for broadband networks Va avec

Today we're going to officially start the program by working on a unit focused on Spoken production.

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

How To Become A Foreign Language Teacher

Threads in der Client/Server-Programmierung mit Java

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

ACP-EU Cooperation Programme in Science and Technology (S&T II) / Programme de Coopération ACP-UE pour la Science et la Technologie

Sun StorEdge A5000 Installation Guide

Il est repris ci-dessous sans aucune complétude - quelques éléments de cet article, dont il est fait des citations (texte entre guillemets).

Introduction ToIP/Asterisk Quelques applications Trixbox/FOP Autres distributions Conclusion. Asterisk et la ToIP. Projet tuteuré

Archived Content. Contenu archivé

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

Socket Programming. Announcement. Lectures moved to

Communicating with a Barco projector over network. Technical note

Covert Channels inside DNS

Solaris 10 Documentation README

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Level 2 French, 2014

Programmation Orientée Objets. et langage Java

Administrer les solutions Citrix XenApp et XenDesktop 7.6 CXD-203

Memory Eye SSTIC Yoann Guillot. Sogeti / ESEC R&D yoann.guillot(at)sogeti.com

INPUT AND OUTPUT STREAMS

Transcription:

Université Paris 7 M1 II Protocoles réseaux TP1 : Correction Rappels : Stream, Thread et Socket TCP Tous les programmes seront écrits en Java. 1. (a) Ecrire une application qui lit des chaines au clavier et les affiche à l écran (utiliser la classe Scanner). L arrêt de cette application se fait à la lecture de "stop" au clavier. public class Main { public static void main(string[] args) { // l arret se fait par "stop" System.out.println("des lignes, stop pour finir:"); while(!(sc.hasnext("stop"))){ nom=sc.nextline(); System.out.println("question pour:"+nom); (b) Ecrire une application qui lit deux chaines au clavier (soient in et out ces chaines). L application copie le contenu du fichier in dans le fichier out (utiliser la classe PrintStream). import java.io.*; public class Exo { public static void main(string[] args) { System.out.println("nom du fichier, stop pour finir:"); nom=sc.next(); System.out.println("demande:"+nom); try{ BufferedReader din= new BufferedReader( new InputStreamReader( new FileInputStream("data.txt"))); PrintStream dout = new PrintStream(new FileOutputStream(nom)); while ((nom = din.readline())!= null){ dout.println(nom); 1

System.out.println("copier:"+nom); catch (IOException ex) { System.out.println("pb fichier" ); 2. On considère 2 processus connectés via une socket TCP (port 1027)(soient A et B ces 2 processus). A lit des chaines au clavier et les transmet à B. B affiche les chaines transmises. (a) A et B terminent lorsque A lit stop au clavier. Quels protocoles suivent A et B pour terminer? A lit stop et ferme la socket. B arrete quand il ne peut plus lire. (b) Donner le code correspondant lorsque A et B sont sur la même machine import java.net.*; import java.util.*; import java.io.*; class paira { ServerSocket server; int port=1027; try { // exception si le port est utilise server = new ServerSocket(port); catch (IOException ex) { System.out.println("port utilise" + port ); server=null; try{ Socket connection = server.accept( ); //creation du buffer en emission DataOutputStream outtoclient = new DataOutputStream(connection.getOutputStream()); //lecture au clavier et envoie au client System.out.println("des lignes stop pour finir"); while(!(sc.hasnext("stop"))){ nom=sc.nextline(); outtoclient.writebytes(nom+"\r\n"); connection.close( ); server.close(); 2

catch (IOException ex) { System.out.println("port utilise" + port ); server=null; ----------- import java.net.*; import java.io.*; public class pairb { // se connecte a A lit les chaines et les affiche BufferedReader networkin = null; BufferedReader userin=null; try { // connexion avec le serveur Socket s = new Socket("localhost", 1027); networkin = new BufferedReader(new InputStreamReader(s.getInputStre String theline ; /* The only way to detect that the remote host has closed the connec to attempt to read or write from the connection. If the rem host properly closed the connection, read() will return -1. If the connection was not terminated normally, read() and w will throw an exception. */ while((theline = networkin.readline())!=null){ System.out.println("B:"+theLine); catch (UnknownHostException ex) { System.err.println(ex); catch (IOException ex) { System.err.println(ex); (c) Donner le code correspondant lorsque A et B sont sur des machines différentes. Remplacer "localhost" (d) A et B terminent lorsque B lit stop au clavier. Quels protocoles suivent A et B pour terminer (utiliser 2 threads)? 3

B lit a la fois les donnees en provenance de A et le clavier. Quand il lit stop il ferme la socket (e) Donner le code correspondant pour A et B. import java.io.dataoutputstream; import java.io.ioexception; import java.net.serversocket; import java.net.socket; public class paira2 { ServerSocket server; int port=1027; try { // exception si le port est utilise server = new ServerSocket(port); Socket connection = server.accept( ); DataOutputStream outtoclient =new DataOutputStream(connection.getOutput //lecture au clavier et envoie a pair B System.out.println("des lignes"); while(true) {nom=sc.nextline(); outtoclient.writebytes(nom+"\r\n"); catch (IOException ex) { System.err.println("sortie car la socket est ferme"); ----------- import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.socket; import java.net.unknownhostexception; public class pairb2 { BufferedReader networkin = null; try { // connexion avec le serveur Socket connection = new Socket("localhost", 1027); 4

LectServeurThread lect= new LectServeurThread(connection); networkin = new BufferedReader(new InputStreamReader(connection.get String theline; while((theline = networkin.readline())!=null){ System.out.println("B:"+theLine); catch (UnknownHostException ex) { System.err.println("sortie"); catch (IOException ex) { System.err.println("sortie car la socket a ete fermee"); ---------------- import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.ioexception; import java.net.socket; public class LectServeurThread extends Thread { private Socket s; public LectServeurThread(Socket s ) { this.s = s; this.start(); public void run() { try{ System.out.println("stop pour finir"); while (!(sc.hasnext("stop"))){ System.out.println("stop pour finir"); sc.nextline(); s.close( ); System.out.println("terminer"); catch (IOException ex) { System.out.println("erreur"); 5

static public void main(string argv[]) { System.out.println("blabla"); 6