Exercice 1 :
Écrire un code en java qui permet à un client d’envoyer un code à un serveur, ce dernier recherche si le code existe dans un fichier nommé « codes.txt » et répond le client par un message.
NB : Pour implémenter la recherche de code dans un fichier, vous pouvez utiliser les classes de fichiers Java.
La solution ci-dessous qui utilise FileReader
et BufferedReader
pour lire le contenu d’un fichier ligne par ligne et rechercher le code correspondant
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
String code = "CODE";
String host = "localhost";
int port = 12345;
Socket socket = new Socket(host, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(code);
String response = in.readLine();
System.out.println("Response from server: " + response);
socket.close();
}
}
import java.io.*;
import java.net.*;
public class Server {
public static boolean searchCode(String fileName, String code) {
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals(code)) {
return true;
}
}
bufferedReader.close();
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + fileName);
} catch (IOException e) {
System.out.println("Error reading file: " + fileName);
}
return false;
}
public static void main(String[] args) throws IOException {
int port = 12345;
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String code = in.readLine();
// Rechercher le code dans le fichier
boolean codeExists = searchCode("codes.txt",code);
if (codeExists) {
out.println("EXISTS");
} else {
out.println("NOT_FOUND");
}
socket.close();
serverSocket.close();
}
}
Exercice 2 :
Créez un programme en Java où le client envoie un code. Le serveur vérifie si ce code n’est pas déjà présent dans un fichier nommé « codes.txt ». Si c’est le cas, il l’ajoute. Dans le cas contraire, le serveur informe le client que ce code existe déjà.
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
System.out.println("Server started");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String code = in.readLine();
System.out.println("Received code: " + code);
if (checkCode(code)) {
out.println("Code already exists");
} else {
out.println("Code added");
addCode(code);
}
clientSocket.close();
} catch (IOException e) {
System.err.println("Could not listen on port: 4444");
System.exit(1);
} finally {
serverSocket.close();
}
}
public static boolean checkCode(String code) {
try (Scanner sc = new Scanner(new File("codes.txt"))) {
while (sc.hasNextLine()) {
if (sc.nextLine().equals(code)) {
return true;
}
}
} catch (FileNotFoundException e) {
System.err.println("File not found");
}
return false;
}
public static void addCode(String code) {
try (PrintWriter pw = new PrintWriter(new FileWriter("codes.txt", true))) {
pw.println(code);
} catch (IOException e) {
System.err.println("Could not add code to file");
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket clientSocket = null;
try {
clientSocket = new Socket("localhost", 4444);
System.out.println("Connected to server");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
Scanner sc = new Scanner(System.in);
System.out.print("Enter code: ");
String code = sc.nextLine();
out.println(code);
System.out.println(in.readLine());
clientSocket.close();
} catch (UnknownHostException e) {
System.err.println("Unknown host");
System.exit(1);
} catch (IOException e) {
System.err.println("Could not connect to server");
System.exit(1);
}
}
}
Exercice 3 :
Créer un programme où le client envoie son code Massar au serveur. Ce dernier examine le fichier « notes.txt » pour localiser la ligne contenant ce code, extrait la note correspondante et l’envoie au client. Voici un aperçu du fichier « notes.txt
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
while (true) {
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String codeMassar = in.readLine();
String note = findNote(codeMassar);
out.println(note);
}
}
public static String findNote(String codeMassar) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader("notes.txt"));
String line;
while ((line = fileReader.readLine()) != null) {
String[] parts = line.split(" ");
if (parts[0].equals(codeMassar)) {
return parts[1];
}
}
return "Code Massar introuvable";
}
}
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 9999);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String codeMassar = "Entrez votre code Massar ici";
out.println(codeMassar);
String note = in.readLine();
System.out.println("Note: " + note);
}
}