15/11/2022

This commit is contained in:
Lucàs
2022-11-15 15:08:16 +01:00
parent 313ee067e7
commit 3e70054728
5 changed files with 183 additions and 12 deletions
-5
View File
@@ -1,5 +0,0 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
+42
View File
@@ -0,0 +1,42 @@
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.rmi.UnknownHostException;
public class Client {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("localhost", 4444);
System.out.println("Connecté au serveur");
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println("Destination unknown");
System.exit(-1);
} catch (IOException e) {
System.out.println("now to investigate this IO issue");
System.exit(-1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + userInput);
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
+27
View File
@@ -0,0 +1,27 @@
package reseau;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ResolutionDeNom {
public static InetAddress getAddress(String host) {
InetAddress address;
try {
address = InetAddress.getByName(host);
return address;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
// String input = args[0];
String input = "www.google.com";
InetAddress address = getAddress(input);
if (address != null) {
System.out.printf("%s : %s\n", input, address.getHostAddress());
}
}
}
+58
View File
@@ -0,0 +1,58 @@
package serveur;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Serveur {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.out.println("Accept failed on port 4444");
System.exit(-1);
}
System.out.println("Client connecté");
boolean quitte = false;
boolean reception = true;
DataOutputStream envoi = null;
BufferedReader recevoir = null;
try {
OutputStream outputStream = clientSocket.getOutputStream();
envoi = new DataOutputStream(outputStream);
recevoir = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (!quitte) {
if (reception) {
String message = null;
while (message == null){
message = recevoir.readLine();
}
while(message != null){
System.out.println(message);
message= recevoir.readLine();
}
}
reception = !reception;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}