Réseau avec Java: M2 informatique Protocoles et réseaux I) Introduction  Les couches M2 internet H. Fauconnier Couche Internet  Datagramme IPv4 M2 internet H.

Download Report

Transcript Réseau avec Java: M2 informatique Protocoles et réseaux I) Introduction  Les couches M2 internet H. Fauconnier Couche Internet  Datagramme IPv4 M2 internet H.

Réseau avec Java:
M2 informatique
Protocoles et réseaux
I) Introduction

Les couches
M2 internet
H. Fauconnier
2
Couche Internet

Datagramme IPv4
M2 internet
H. Fauconnier
3
Couche transport

TCP


Mode connecté, flot bidirectionnel, sûr,
contrôle de la congestion
 Téléphone
UDP

Mode non connecté, messages, sans
garantie, déséquencement

M2 internet
H. Fauconnier
Poste
4
Adresses internet


Adresse IP: adresse réseau + site sur
le réseau
Exemple:
M2 internet
H. Fauconnier
5
Classe d’adresses Internet
M2 internet
H. Fauconnier
6
Connexion



Adresse IP +port
Ports réservés
Ports libres
M2 internet
H. Fauconnier
7
Quelques ports
Protocol
Port
Protocol
echo
7
TCP/UDP
discard
9
TCP/UDP
daytime
13
TCP/UDP
FTP data
20
FTP
Protocol
Port Protocol
whois
43
TCP
finger
79
TCP
TCP
HTTP
80
TCP
21
TCP
POP3
110
TCP
SSH
22
TCP
NNTP
119
TCP
telnet
23
TCP
smtp
25
TCP
IMAP
143
TCP
time
37
TCP/UDP
M2 internet
RMI Registry
H. Fauconnier
109
9
TCP
8
Proxys
M2 internet
H. Fauconnier
9
Comment travailler avec un
proxy?



Régler le navigateur… les applets du navigateur
utilisent ces réglages
Pour une application java il faut préciser les
propriétés: socksProxyHo socksProxyPor
(SOCKS proxy server), http.proxySet,
http.proxyHost, http.proxyPort,
https.proxySet, https.proxyHost,
https.proxyPort, ftpProxySet,
ftpProxyHost, ftpProxyPort,
gopherProxySet, gopherProxyHost,
gopherProxyPort (pour les autres protocoles).
Pour cela:
java -DsocksProxyHost= socks.cloud9.net DsocksProxyPort= 1080 MyClass
M2 internet
H. Fauconnier
10
Client-serveur
M2 internet
H. Fauconnier
11
Classes


java.net.InetAddress (implements java.io.Serializable)
java.net.Inet4Address

java.net.Inet6Address

java.net.DatagramPacket

java.net.DatagramSocket


java.net.ServerSocket


javax.net.ssl.SSLServerSocket
java.net.Socket


java.net.MulticastSocket
javax.net.ssl.SSLSocket
java.net.SocketAddress (implements java.io.Serializable)

M2 internet
java.net.InetSocketAddress
H. Fauconnier
12
Classes
Channel (lien local)

java.nio.channels.spi.AbstractInterruptibleChannel
(implements java.nio.channels.Channel,
java.nio.channels.InterruptibleChannel)

java.nio.channels.SelectableChannel (implements
java.nio.channels.Channel)

java.nio.channels.spi.AbstractSelectableChannel


M2 internet
java.nio.channels.DatagramChannel (implements
java.nio.channels.ByteChannel,
java.nio.channels.GatheringByteChannel,
java.nio.channels.ScatteringByteChannel)
java.nio.channels.SocketChannel (implements
java.nio.channels.ByteChannel,
java.nio.channels.GatheringByteChannel,
java.nio.channels.ScatteringByteChannel)
H. Fauconnier
15
II) Adresses internet


Classe InetAddress:
Ontenir une InetAddress:

En utilisant le DNS)




Sans DNS


M2 internet
public static InetAddress getByName(String
hostName) throws UnknownHostException
public static InetAddress[] getAllByName(String
hostName) throws UnknownHostException
public static InetAddress getLocalHost( ) throws
UnknownHostException
public static InetAddress getByAddress(byte[]
address) throws UnknownHostException
public static InetAddress getByAddress(String
hostName, byte[] address) throws
UnknownHostException
H. Fauconnier
17
Exemples
import java.net.*;
/...
public static void main (String[] args){
try {
InetAddress adresse =
InetAddress.getByName("liafa.jussieu.fr");
System.out.println(adresse);
} catch (UnknownHostException ex) {
System.out.println("liafa.jussieu.fr ??");
}
}
M2 internet
H. Fauconnier
18
Exemples
public static void main (String[] args){
try {
InetAddress ad =
InetAddress.getByName("192.227.93.1");
System.out.println(ad);
} catch (UnknownHostException ex) {
System.out.println(« 192.227.93.1 ??");
}
}
M2 internet
H. Fauconnier
19
Toutes les adresses…
public static void AllAdresses(String st) {
try {
InetAddress[] addresses =
InetAddress.getAllByName(st);
for (int i = 0; i < addresses.length; i++) {
System.out.println(addresses[i]);
}
} catch (UnknownHostException ex) {
System.out.println(st+"est inconnu");
}
}
M2 internet
H. Fauconnier
20
Mon adresse
public static String MonAdresse() {
try {
InetAddress moi =
InetAddress.getLocalHost();
return( moi.getHostAddress());
} catch (UnknownHostException ex) {
return("Mon adresse est inconnue");
}
}
M2 internet
H. Fauconnier
21
InetAddress méthodes…
public String getHostName( )
public byte[] getAddress( )
public String getHostAddress( )
Exemple:
public static void main (String[] args) {
try {
InetAddress ia= InetAddress.getByName("192.168.22.1");
System.out.println(ia.getHostName( ));
} catch (Exception ex)
{ System.err.println(ex); }
}
M2 internet
H. Fauconnier
22
Divers…

public boolean isAnyLocalAddress( )
« wildcard »?

public boolean isLoopbackAddress( )

public boolean isMulticastAddress( )

Java 1.5



public boolean isReachable(int timeout) throws
IOException
public boolean isReachable(NetworkInterface
interface, int ttl, int timeout) throws IOException
IPV4 et IPV6:


M2 internet
public final class Inet4Address extends
InetAddress
public final class Inet6Address extends
InetAddress
H. Fauconnier
23
NetworkInterface

Exemple:
try {
NetworkInterface ni =
NetworkInterface.getByName("eth0");
if (ni == null) {
System.err.println(" pas de: eth0" );
}
} catch (SocketException ex) { }
M2 internet
H. Fauconnier
24
Exemple
public static String lookup(String host) {
InetAddress node;
// récupérer l'adresse par getByName
try {
node = InetAddress.getByName(host);
} catch (UnknownHostException ex) {
return "hôte inconnu " + host;
}
if (isHostname(host)) {
return node.getHostAddress();
} else {
return node.getHostName();
}
}
M2 internet
H. Fauconnier
25
II) sockets (client)
Note

Dans ce chapitre et le suivant on
s’intéresse aux sockets TCP, on verra
les sockets UDP plus tard.
M2 internet
H. Fauconnier
27
Généralités
Une connexion:

(IP adresse+port, IP adresse +port)
On peut lire et écrire sur la socket



Serveur:
Associer une socket à une adresse connue (IP+port)
Ecoute sur la socket
Quand une connexion arrive accept : une nouvelle socket est créée
(nouveau port)




Rendre le service envoyer/recevoir


Client:






M2 internet
(en général dans une thread)
Continuer à écouter
Crée une socket
Demande connexion sur adresse +port du serveur
Connexion
Envoyer/recevoir
Fin de la connexion
H. Fauconnier
28
Socket en Java

Serveur

Classe SocketServer






(bind (mais en général par constructeur)
listen)
Accept
getInputStream, getOutputStream
close
Client

Classe Socket




M2 internet
(bind)
connect (mais en général par constructeur)
getInputStream, getOutputStream
close
H. Fauconnier
29
Attention!



L’accès aux ports est souvent restreint
Des firewall peuvent empêcher les
connexions
Il faut être root pour utiliser des ports
réservés…
M2 internet
H. Fauconnier
30
Côté client



Création:
public Socket(InetAddress address, int port)
throws IOException
Crée une socket + une connexion avec IP adresse et port

En fait:



M2 internet
Création d’une socket locale attachée à un port + une adresse locale
Etablissement de la connexion
IOException en cas d’échec
H. Fauconnier
31
Exemple
public static void regarderPortBas(String host) {
for (int i = 1; i < 1024; i++) {
try {
Socket s = new Socket(host, i);
System.out.println("Il y a un serveur
sur " + i + " de "+ host);
} catch (UnknownHostException ex) {
System.err.println(ex);
break;
} catch (IOException ex) {
// exception s'il n'y a pas de serveur
}
}
}
M2 internet
H. Fauconnier
32
Attention

Cet exemple peut ne pas bien
fonctionner…

M2 internet
Pour des raisons de sécurité certains la
tentative de connexion peut être
bloquante
H. Fauconnier
33
Autres constructeurs
try {
InetAddress inward =
InetAddress.getByName("router");
Socket socket = new Socket("mail", 25, inward, 0);
// work with the sockets...
}
catch (UnknownHostException ex) {
System.err.println(ex);
}

Connexion à partir de interface réseau et du port spécifié,

‘0’ signifie n’importe quel port
M2 internet
H. Fauconnier
34
Avec un proxy
SocetAddress proxyAddress = new
InetSocketAddress("myproxy.example.com", 1080);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddress)
Socket s = new Socket(proxy);
SocketAddress remote = new
InetSocketAddress("login.ibiblio.org", 25);
s.connect(remote);
M2 internet
H. Fauconnier
35
Obtenir des infos…




public InetAddress getInetAddress( )
public int getPort( )
public InetAddress getLocalAddress( )
public int getLocalPort( )
M2 internet
H. Fauconnier
36
Exemple
public static void socketInfo(String ... args) {
for (int i = 0; i < args.length; i++) {
try {
Socket theSocket = new Socket(args[i], 80);
System.out.println("Connecté sur " +
theSocket.getInetAddress()
+ " port " + theSocket.getPort() + " depuis port "
+ theSocket.getLocalPort() + " de "
+ theSocket.getLocalAddress());
}
catch (UnknownHostException ex) {
System.err.println("Hôte inconnu " + args[i]);
} catch (SocketException ex) {
System.err.println("Connection impossible " + args[i]);
} catch (IOException ex) {
System.err.println(ex);
}
}
}
M2 internet
H. Fauconnier
37
Communiquer…


public InputStream getInputStream( )
throws IOException
public OutputStream getOutputStream( )
throws IOException
M2 internet
H. Fauconnier
38
Exemple: dayTime
public static void time(String ... hlist) {
for (int i=0;i<hlist.length;i++){
try {
Socket theSocket = new Socket(hlist[i], 13);
InputStream timeStream = theSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while ((c = timeStream.read()) != -1) time.append((char) c);
String timeString = time.toString().trim();
System.out.println("Il est " + timeString + " à " + hlist[i]);
}
catch (UnknownHostException ex) {
System.err.println(ex);
} catch (IOException ex) {
System.err.println(ex);
}
}
}
M2 internet
H. Fauconnier
39
Exemple: echo
public static void echo(String hostname, int port) {
PrintWriter out = null;
BufferedReader networkIn = null;
try {
Socket theSocket = new Socket(hostname, port);
networkIn = new BufferedReader(
new InputStreamReader(theSocket.getInputStream()));
BufferedReader userIn = new BufferedReader(
new InputStreamReader(System.in));
out = new PrintWriter(theSocket.getOutputStream());
System.out.println("Client: Connecté au serveur d'echo "+ theSocket);
while (true) {
String theLine = userIn.readLine();
out.println(theLine);
out.flush();
if (theLine.equals(".")){out.close(); break;}
System.out.println(networkIn.readLine());
}
}
catch (IOException ex) {System.err.println(ex);
} finally {
try {
if (networkIn != null) networkIn.close();
if (out != null) out.close();
} catch (IOException ex) {}
}
}
}
M2 internet
H. Fauconnier
40
Echo suite
catch (IOException ex) {
System.err.println(ex);
} finally {
try {
if (networkIn != null)
networkIn.close();
if (out != null) out.close();
} catch (IOException ex) {}
}
M2 internet
H. Fauconnier
41
Fermeture


public void close( ) throws IOException
Fermeture de la socket:



M2 internet
Automatique si une des parties fait un
close
garbage collector
(le réseau utilise des ressources systèmes
qui sont par définition partagées et
limitées)
H. Fauconnier
42
Chapitre III
ServerSocket
Principe
1.
2.
3.
Création d’un ServerSocket par
constructeur
Association (bind) de la socket à une
adresse et un port ((1) et (2) peuvent être
simultanés)
Écoute et connexion par accept
1.
2.
4.
Communication getInputStream et
getOutputStream
close (par le client ou le serveur ou les deux)
Aller en (2)
(en général 3 est dans une thread)
M2 internet
H. Fauconnier
44
Constructeurs




public ServerSocket(int port) throws
BindException, IOException
public ServerSocket(int port, int
queueLength) throws BindException, IOException
public ServerSocket(int port, int queueLength,
InetAddress bindAddress) throws IOException
Ces constructeurs associe un port et une adresse au
ServerSocket l’usage du port est exclusif et si le
port est déjà occupé une exception est lancée

M2 internet
public ServerSocket( ) throws IOException //
Java 1.4
H. Fauconnier
45
Exemple
public static void portsLibres() {
for (int port = 1; port <= 65535; port++) {
try {
// exception si le port est utilisé
ServerSocket server = new
ServerSocket(port);
} catch (IOException ex) {
System.out.println("serveur sur port"
+ port );
}
}
}
M2 internet
H. Fauconnier
46
Remarques




port 0: choisi par le système
on peut donner une taille sur la file des
connexions en attente
on peut choisir une adresse particulière sur
la machine locale
En java >1.4 on peut faire un "bind" explicite:


M2 internet
public void bind(SocketAddress endpoint)
throws IOException
public void bind(SocketAddress endpoint,
int queueLength) throws IOException
H. Fauconnier
47
Exemple
public static void portQuelconque() {
try {
ServerSocket server = new ServerSocket(0);
System.out.println("Le port obtenu est "
+ server.getLocalPort());
} catch (IOException ex) {
System.err.println(ex);
}
}
M2 internet
H. Fauconnier
48
Connexion accept()

crée et retourne une nouvelle socket
pour la connexion associée (IP, port)(IP,
port)
M2 internet
H. Fauconnier
49
Exemple
ServerSocket server = new
ServerSocket(5776);
while (true) {
Socket connection = server.accept( );
OutputStreamWriter out = new
OutputStreamWriter(
connection.getOutputStream( ));
out.write("Connecté:" +connection+"\r\n");
connection.close( );
}
M2 internet
H. Fauconnier
50
Exemple plus complet
public final static int DEFAULT_PORT = 13;
public static void dayTime(){
dayTime(DEFAULT_PORT);
}
public static void dayTime(int port) {
if (port < 0 || port >= 65536) {
System.out.println("Erreur port:");
return;
}
try {
ServerSocket server = new ServerSocket(port);
Socket connection = null;
M2 internet
H. Fauconnier
51
Exemple suite
while (true) {
try {
connection = server.accept();
Writer out = new OutputStreamWriter(
connection.getOutputStream());
Date now = new Date();
out.write(now.toString() +"\r\n");
out.flush();
connection.close();
} catch (IOException ex) {} finally {
try {
if (connection != null) connection.close();
} catch (IOException ex) {}
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}internet
M2
H. Fauconnier
52
Fermeture
public void close( ) throws IOException
Ferme le ServerSocket et toutes les
connexions créées par accept sur la
ServerSocket
M2 internet
H. Fauconnier
53
Serveur echo
public static void serveurEcho(int port) {
try {
ServerSocket server = new ServerSocket(port,100);
System.out.println("Serveur:"+server+" en écoute sur le port: "
+ server.getLocalPort()+" est lancé");
while (true) {
Socket connection = server.accept();
System.out.println("Serveur connexion avec: "
+ connection);
Thread echo=new EchoThread(connection);
echo.start();
} catch (IOException ex) {
System.out.println("le port" + port + " est occupé");
System.out.println("On suppose donc que le service estlancé");
}
}
}
M2 internet
H. Fauconnier
54
serveur echo: EchoThread
class EchoThread extends Thread {
BufferedReader in;
PrintWriter out;
Socket connection;
public EchoThread(Socket connection) {
try{
this.connection=connection;
InputStream in=connection.getInputStream();
OutputStream out=connection.getOutputStream();
this.in = new BufferedReader(new
InputStreamReader(in));
this.out = new PrintWriter(out);
} catch (IOException ex) {
System.err.println(ex);
}
}
M2 internet
H. Fauconnier
55
run
public void run() {
try {
while (true) {
String st;
st = in.readLine();
if (st.equals("."))
in.close();
out.close();
break;
}
System.out.println("Serveur a reçu:"+st+" de "+connection);
out.println(st);
out.flush();
}
} catch (SocketException ex) { ex.printStackTrace();
} catch (IOException ex) { System.err.println(ex);
}
try {
in.close();
out.close();
} catch (IOException ex) { ex.printStackTrace();}
}
}
M2 internet
H. Fauconnier
56
Remarques


utilisation des threads pour traiter le
service et éviter de faire attendre les
clients
on peut aussi utiliser des
entrées/sorties non bloquantes
M2 internet
H. Fauconnier
57
Autres méthodes


public InetAddress getInetAddress( )
public int getLocalPort( )
M2 internet
H. Fauconnier
58
Options




SO_TIMEOUT
SO_REUSEADDR
SO_RCVBUF
public void setPerformancePreferences(int
connectionTime, int latency, int
bandwidth
M2 internet
H. Fauconnier
59
IV) Socket UDP
UDP
M2 internet
H. Fauconnier
61
DatagramPacket


Un paquet contient au plus 65,507 bytes
Pour construire les paquet



public DatagramPacket(byte[] buffer, int length)
public DatagramPacket(byte[] buffer, int offset, int length)
Pour construire et envoyer




M2 internet
public DatagramPacket(byte[] data, int length,
InetAddress destination, int port)
public DatagramPacket(byte[] data, int offset, int
length, InetAddress destination, int port) // Java
1.2
public DatagramPacket(byte[] data, int length,
SocketAddress destination, int port) // Java 1.4
public DatagramPacket(byte[] data, int offset, int
length, SocketAddress destination, int port) //
Java 1.4
H. Fauconnier
62
Exemple
String s = "On essaie…";
byte[] data = s.getBytes("ASCII");
try {
InetAddress ia =
InetAddress.getByName("www.jussieu.fr");
int port = 7;// existe-t-il?
DatagramPacket dp = new DatagramPacket(data,
data.length, ia, port);
// envoi
}
catch (IOException ex)
}
M2 internet
H. Fauconnier
63
Méthodes

Adresses






M2 internet
public InetAddress getAddress( )
public int getPort( )
public SocketAddress
getSocketAddress( )
public void setAddress(InetAddress
remote)
public void setPort(int port)
public void setAddress(SocketAddress
remote)
H. Fauconnier
64
Méthodes (suite)

Manipulation des données:






M2 internet
public byte[] getData( )
public int getLength( )
public int getOffset( )
public void setData(byte[] data)
public void setData(byte[] data,
int offset, int length )
public void setLength(int length)
H. Fauconnier
65
Exemple
import java.net.*;
public class DatagramExample {
public static void main(String[] args) {
String s = "Essayons.";
byte[] data = s.getBytes( );
try {
InetAddress ia = InetAddress.getByName("www.liafa.jussieu.fr");
int port =7;
DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);
System.out.println(" Un packet pour" + dp.getAddress( ) + " poirt " +
dp.getPort( ));
System.out.println("il y a " + dp.getLength( ) + " bytes dans le
packet");
System.out.println(
new String(dp.getData( ), dp.getOffset( ), dp.getLength( )));
}
catch (UnknownHostException e) {
System.err.println(e);
}
}
}
M2 internet
H. Fauconnier
66
DatagramSocket

Constructeurs





M2 internet
public DatagramSocket( ) throws
SocketException
public DatagramSocket(int port) throws
SocketException
public DatagramSocket(int port, InetAddress
interface) throws SocketException
public DatagramSocket(SocketAddress interface)
throws SocketException
(protected DatagramSocket(DatagramSocketImpl
impl) throws SocketException)
H. Fauconnier
67
Exemple
java.net.*;
public class UDPPortScanner {
public static void main(String[] args) {
for (int port = 1024; port <= 65535; port++) {
try {
// exception si utilisé
DatagramSocket server = new DatagramSocket(port);
server.close( );
}
catch (SocketException ex) {
System.out.println("Port occupé" + port + ".");
} // end try
} // end for
}
}
M2 internet
H. Fauconnier
68
Envoyer et recevoir


public void send(DatagramPacket dp)
throws IOException
public void receive(DatagramPacket dp)
throws IOException
M2 internet
H. Fauconnier
69
Un exemple: Echo

UDPServeur


UDPEchoServeur
UDPEchoClient


M2 internet
SenderThread
ReceiverThread
H. Fauconnier
70
Echo: UDPServeur
import java.net.*;
import java.io.*;
public abstract class UDPServeur extends Thread {
private int bufferSize;
protected DatagramSocket sock;
public UDPServeur(int port, int bufferSize)
throws SocketException {
this.bufferSize = bufferSize;
this.sock = new DatagramSocket(port);
}
public UDPServeur(int port) throws SocketException {
this(port, 8192);
}
public void run() {
byte[] buffer = new byte[bufferSize];
while (true) {
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
try {
sock.receive(incoming);
this.respond(incoming);
}
catch (IOException e) {
System.err.println(e);
}
} // end while
}
public abstract void respond(DatagramPacket request);
}
M2 internet
H. Fauconnier
71
UDPEchoServeur
public class UDPEchoServeur extends UDPServeur {
public final static int DEFAULT_PORT = 2222;
public UDPEchoServeur() throws SocketException {
super(DEFAULT_PORT);
}
public void respond(DatagramPacket packet) {
try {
byte[] data = new byte[packet.getLength()];
System.arraycopy(packet.getData(), 0, data, 0, packet.getLength());
try {
String s = new String(data, "8859_1");
System.out.println(packet.getAddress() + " port "
+ packet.getPort() + " reçu " + s);
} catch (java.io.UnsupportedEncodingException ex) {}
DatagramPacket outgoing = new DatagramPacket(packet.getData(),
packet.getLength(), packet.getAddress(), packet.getPort());
sock.send(outgoing);
} catch (IOException ex) {
System.err.println(ex);
}
}
}
M2 internet
H. Fauconnier
72
Client: UDPEchoClient
public class UDPEchoClient {
public static void lancer(String hostname, int port) {
try {
InetAddress ia = InetAddress.getByName(hostname);
SenderThread sender = new SenderThread(ia, port);
sender.start();
Thread receiver = new ReceiverThread(sender.getSocket());
receiver.start();
}
catch (UnknownHostException ex) {
System.err.println(ex);
}
catch (SocketException ex) {
System.err.println(ex);
}
}
// end lancer
}
M2 internet
H. Fauconnier
73
ReceiverThread
class ReceiverThread extends Thread {
DatagramSocket socket;
private boolean stopped = false;
public ReceiverThread(DatagramSocket ds) throws SocketException {
this.socket = ds;
}
public void halt() {
this.stopped = true;
}
public DatagramSocket getSocket(){
return socket;
}
public void run() {
byte[] buffer = new byte[65507];
while (true) {
if (stopped) return;
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(dp);
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println(s);
Thread.yield();
} catch (IOException ex) {System.err.println(ex); }
}
}
M2 internet
H. Fauconnier
}
74
SenderThread
public class SenderThread extends Thread {
private InetAddress server;
private DatagramSocket socket;
private boolean stopped = false;
private int port;
public SenderThread(InetAddress address, int port)
throws SocketException {
this.server = address;
this.port = port;
this.socket = new DatagramSocket();
this.socket.connect(server, port);
}
public void halt() {
this.stopped = true;
}
//…
M2 internet
H. Fauconnier
75
SenderThread
//…
public DatagramSocket getSocket() {
return this.socket;
}
public void run() {
}
try {
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
while (true) {
if (stopped) return;
String theLine = userInput.readLine();
if (theLine.equals(".")) break;
byte[] data = theLine.getBytes();
DatagramPacket output
= new DatagramPacket(data, data.length, server, port);
socket.send(output);
Thread.yield();
}
} // end try
catch (IOException ex) {System.err.println(ex); }
} // end run
M2 internet
H. Fauconnier
76
Autres méthodes










public
public
public
public
public
public
public
public
public
public
M2 internet
void close( )
int getLocalPort( )
InetAddress getLocalAddress( )
SocketAddress getLocalSocketAddress( )
void connect(InetAddress host, int port)
void disconnect( )
void disconnect( )
int getPort( )
InetAddress getInetAddress( )
InetAddress getRemoteSocketAddress( )
H. Fauconnier
77
Options

SO_TIMEOUT



SO_RCVBUF




public void setSendBufferSize(int size) throws SocketException
int getSendBufferSize( ) throws SocketException
SO_REUSEADDR (plusieurs sockets sur la même adresse)



public void setReceiveBufferSize(int size) throws SocketException
public int getReceiveBufferSize( ) throws SocketException
SO_SNDBUF


public synchronized void setSoTimeout(int timeout) throws
SocketException
public synchronized int getSoTimeout( ) throws IOException
public void setReuseAddress(boolean on) throws SocketException
boolean getReuseAddress( ) throws SocketException
SO_BROADCAST


M2 internet
public void setBroadcast(boolean on) throws SocketException
public boolean getBroadcast( ) throws SocketException
H. Fauconnier
78