Le reseau et java

TCP/UDP et IP

Le package java.net fournit des classes permettant de programmer des applications reseau au niveau des couches 3 (reseau, protocole IP) et 4 (transport, protocoles TCP et UDP) du modele ISO.

En particulier, des classes sont dediees au protocole point a point securise (TCP) et au protocole datagramme non securise (UDP). Il est relativement simple de realiser des prises (socket) liant une application, un port (i.e. une adresse memoire) et une machine (i.e. une adresse IP) a son alter ego sur le reseau, que ce soit en mode TCP ou UDP.

Les ports accessibles sont definis entre 0 et 65536 (bien que ceux entre 0 et 1023 soient deja reserves).

Les URLs

Les classes Java ont la particularite de manipuler directement des URLs (Uniform Resource Locator) ce qui represente une tres grande facilite car on peut ainsi gerer des objets depuis un assez haut niveau d'abstraction (charger une image revient a charger une URL et non aller chercher un fichier d'un type donne, sur une machine donnee, avec un protocole de communication donne). Une URL est formee de la facon suivante :

protocole://machine:numero_de_port/chemin/fichier

par exemple cette page se trouve en :

http://www-lil.univ-littoral.fr/~ris/cours/java/paral/intro.net.html

Java dispose d'une classe URL dont la description est la suivante* :

Constructeurs :

URL(String) Creates a URL object from the String representation.

URL(String, String, int, String) Creates a URL object from the specified protocol, host, port number, and file.

URL(String, String, String) Creates an absolute URL from the specified protocol name, host name, and file name.

URL(URL, String) Creates a URL by parsing the specification spec within a specified context.

Methodes :

equals(Object) Compares two URLs.

getContent() Returns the contents of this URL. getFile() Returns the file name of this URL.

getHost() Returns the host name of this URL, if applicable.

getPort() Returns the port number of this URL.

getProtocol() Returns the protocol name this URL.

getRef() Returns the anchor (also known as the "reference") of this URL.

hashCode() Creates an integer suitable for hash table indexing.

openConnection() Returns a URLConnection object that represents a connection to the remote object referred to by the URL.

openStream() Opens a connection to this URL and returns an InputStream for reading from that connection.

sameFile(URL) Compares two URLs, excluding the "ref" fields. set(String, String, int, String, String) Sets the fields of the URL.

setURLStreamHandlerFactory(URLStreamHandlerFactory) Sets an application's URLStreamHandlerFactory.

toExternalForm() Constructs a string representation of this URL.

toString() Constructs a string representation of this URL.

*extrait de la doc Sun.


Ph. RIS 1997