Академический Документы
Профессиональный Документы
Культура Документы
(Programation en java)
Rushed Kanawati
1 Introduction
Objectifs
Communication inter-processus
Les tubes :
communication unidirectionnelle
entrée processus pèere/fils ou processus frères.
Les sockets
communication bidirectionnelle
gestion similaire aux sockets interUnix.
Communiquer = écrire dans un fichier
Numéro de Port
1
Internet Assigned Numbers Authority, http://www.iana.org
R. Kanawati (LIPN) Systèmes d’exploitation January 31, 2017 10 / 38
Introduction
Le concept de socket
La classe InetAddress
InetAddress a ,b , c ;
...
try {
} catch ( U n k n o w n H o s t E x c e p t i o n e ) { ... }
La classe DatagramPacket
InetAddress getAddress()
Retourne l’adresse IP de la machine distante.
int getPort()
Retourne le numéro de port distant.
byte[] getData()
Retourne la partie donnée du paquet.
Classe DatagramSocket
Constructeurs :
public DatagramSocket() throws SocketException
Création d’un socket UDP associé à un port libre (privé)
public DatagramSocket(int port) throws SocketException
Création d’un socket UDP associé au port port
/* Le message à envoyer */
String s = new String ( " Bonjour Paris 13 " );
byte [] message = new byte [1024];
DatagramPacket paquet =
new DatagramPacket ( message , longueur , address , port );
/* envoi du paquet */
socket . send ( paquet );
try {
DatagramSocket socket = new DatagramSocket (8888);
byte [] buffer = new byte [1024];
// On associe un paquet à un buffer vide pour la réception
DatagramPacket paquet = new
DatagramPacket ( buffer , buffer . length );
/* attente de réception */
socket . receive ( paquet );
while ( true ) {
private DatagramPacket p ;
Communication Multicast
La classe MulticastSocket
Sous-classe de DatagramSocket
Constructeurs :
public MulticastSocket () throws IOException
/* Création d ’ un socket multicast associé
à un port UDP libre */
La classe MulticastSocket
Multicast : exemple
public class Multicast {
try {
adr = InetAddress . getByName ( " 228.5.6.7 " );
soc = new MulticastSocket ();
soc . joinGroup ( adr );
msg = new String ( " Salut !! " ). getBytes ();
p = new DatagramPacket ( msg , msg . length , ad
soc . send ( p );
Multicast : exemple
public class MCClient {
public static void main ( String [] args ){
MulticastSocket soc ;
InetAddress adr ;
DatagramPacket p ;
byte [] msg ;
try {
adr = InetAddress . getByName ( " 228.5.6.7 " );
soc = new MulticastSocket (8888);
soc . joinGroup ( adr );
msg = new byte [1024];
p = new DatagramPacket ( msg , msg . length );
soc . receive ( p );
System . out . println ( " Message reçu " +
new String ( p . getDat
} catch ( Exception ex ){}
}}
R. Kanawati (LIPN) Systèmes d’exploitation January 31, 2017 31 / 38
Classes Java pour la programmation réseaux: TCP/IP Communication TCP
Communication TCP
La classe ServerSocket
La classe Socket
Constructeur :
public Socket ( InetAddress server , int port )
throws IOException ;
/* Demande de connexion au serveur TCP
dont l ’ adresse est server et qui est
à l ’ écoute sur port */
try {
while ( true ) {
soc = serSoc . accept ();
in = new DataInputStream ( soc . getInputStream ());
out = new PrintStream ( soc . getOutputStream ());
while ( true ) {
String ligne = in . readLine ();
out . println ( " ECHO : " + ligne );
}
}
} catch ( Exception e ) { }
} }
Socket soc ;
DataInputStream in ;
DataInputStream userInput ;
PrintStream out ;
String ligne ;
try {
soc = new Socket ( args [0] , Integer . parseInt ( args [1]));
in = new DataInputStream ( soc . getInputStream ());
out = new PrintStream ( soc . getOutputStream ());
while ( true ) {
ligne = userInput . readLine ();
if ( ligne . equals ( " . " ))
break ;
out . println ( lingo );
System . out . println ( in . readLine ());
}
}
catch ( Un k n o w n H o s t E x c e p t i o n e ) { }
catch ( IOException e ) { } } }