Correction des exercices de réseau en Java
Date de publication : 26/11/2007 , Date de mise à jour : 26/11/2007
Par
Humbert Florent
Voici la correction des exercices proposés dans le cours Réseau en Java.
I. Solutions des exercices
I-A. Exercice 1 : Obtenir l'adresse IP d'un site web
I-B. Obtenir son adresse IP
I-C. Flux bufferisés ?
I-D. Equivalent de telnet ou netcat
I-E. Serveur écoutant
I-E-1. Suite de l'exercice
I. Solutions des exercices
I-A. Exercice 1 : Obtenir l'adresse IP d'un site web
package developpez;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
@author
public class TestInetAddress {
@param
@throws
@throws
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("www.developpez.com");
System.out.println(address.getHostAddress());
}
}
|
package developpez;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
@author
public class TestInetAddress {
@param
@return
static boolean isValidAddress(String host) {
try {
return InetAddress.getByName(host).isReachable(100);
}
catch(Exception e) {
return false;
}
}
@param
@throws
@throws
public static void main(String[] args) throws Exception {
System.out.println(isValidAddress("www.developpez"));
}
}
|
I-B. Obtenir son adresse IP
|
package developpez;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
@author
public class TestInetAddress {
@param
@throws
@throws
@throws
public static void main(String[] args) throws IOException {
Socket s = new Socket("www.developpez.com", 80);
System.out.println(s.getLocalAddress().getHostAddress());
}
}
|
I-C. Flux bufferisés ?
package developpez;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
@author
public class TestInetAddress {
static void readAll(InputStream iStream, int bufferSize) throws IOException {
if(bufferSize<1)
throw new IllegalArgumentException("Taille du buffer <1");
byte[] buffer = new byte[bufferSize];
int total = 0;
int bitsLus = 0;
while ( (bitsLus = iStream.read(buffer)) > 0) {
total+= bitsLus;
if(total>10000)
break;
}
}
static void test(boolean buffered, int bufferSize) throws UnknownHostException, IOException {
Socket s = null;
OutputStream oStream = null;
InputStream iStream = null;
String g = "GET / HTTP/1.1\n" + "Host: www.yahoo.fr\n\n";
s = new Socket("www.yahoo.fr", 80);
oStream = s.getOutputStream();
iStream = s.getInputStream();
BufferedInputStream bIStream = new BufferedInputStream(iStream, 10);
oStream.write(g.getBytes());
if (buffered)
readAll(bIStream, bufferSize);
else
readAll(iStream, bufferSize);
bIStream.close();
oStream.close();
iStream.close();
s.close();
}
@param
@throws
@throws
@throws
public static void main(String[] args) throws IOException {
for(int size = 1; size<1000; size+=100)
{
System.out.println("Buffer de taille " + size);
long startBuffer = System.currentTimeMillis();
test(true, size);
long dureeBuffer = System.currentTimeMillis() - startBuffer;
System.out.println("BufferedInputStream Temps : " + dureeBuffer);
long startSansBuffer = System.currentTimeMillis();
test(false, size);
long dureeSansBuffer = System.currentTimeMillis() - startSansBuffer;
System.out.println("InputStream Temps : " + dureeSansBuffer);
}
}
}
|
I-D. Equivalent de telnet ou netcat
package developpez;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
@author
class Reader extends Thread {
private Socket socket;
public Reader(Socket s) {
this.socket = s;
}
public void run() {
InputStream iStream = null;
try {
iStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
iStream));
String g;
while ((g = reader.readLine()) != null) {
System.err.println(g);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
iStream.close();
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
@author
public class TestInetAddress {
@param
@throws
@throws
public static void main(String[] args) throws UnknownHostException, IOException {
String host = "www.developpez.com";
int port = 80;
BufferedReader iReader = new BufferedReader(new InputStreamReader(
System.in));
Socket s = new Socket(host, port);;
OutputStream oStream = s.getOutputStream();;
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
oStream));
Reader reader = new Reader(s);
reader.start();
try {
while (true) {
String toSend = iReader.readLine();
toSend += '\n';
writer.write(toSend);
writer.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
s.close();
oStream.close();
iReader.close();
}
}
}
|
I-E. Serveur écoutant
package developpez;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
@author
public class TestInetAddress {
@param
@throws
@throws
@throws
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(300);
Socket client = server.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client
.getInputStream()));
String ligne;
try {
while ((ligne = reader.readLine()) != null) {
System.out.println(ligne);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.close();
client.close();
server.close();
}
}
}
|
I-E-1. Suite de l'exercice
package developpez;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
class Client extends Thread {
private Socket client;
Client(Socket s) {
client = s;
}
public void run() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(client
.getInputStream()));
String ligne;
while ((ligne = reader.readLine()) != null) {
System.out.println(ligne);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
if (client != null)
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@author
public class TestInetAddress {
@param
@throws
@throws
@throws
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(300);
Socket client;
while (true) {
client = server.accept();
Client c = new Client(client);
c.start();
}
}
}
|


Les sources présentées sur cette page sont libres de droits
et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation
constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright ©
2007 Florent HUMBERT. Aucune reproduction, même partielle, ne peut être
faite de ce site ni de l'ensemble de son contenu : textes, documents, images, etc.
sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à
trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.