From 297bf9104466f1295f095de2c3e2853671ebc03d Mon Sep 17 00:00:00 2001 From: benassai Date: Mon, 23 Nov 2020 16:25:52 +0100 Subject: [PATCH] Mise en commun modules server et client --- Projet_POO/src/Réseau/TCPServer.java | 67 ++++++++++++++ Projet_POO/src/Réseau/TCPServerThread.java | 39 ++++++++ .../src/Réseau/TCPServerWithThreads.java | 32 +++++++ Projet_POO/src/Réseau/tcpClient.java | 91 +++++++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 Projet_POO/src/Réseau/TCPServer.java create mode 100644 Projet_POO/src/Réseau/TCPServerThread.java create mode 100644 Projet_POO/src/Réseau/TCPServerWithThreads.java create mode 100644 Projet_POO/src/Réseau/tcpClient.java diff --git a/Projet_POO/src/Réseau/TCPServer.java b/Projet_POO/src/Réseau/TCPServer.java new file mode 100644 index 0000000..bcd599e --- /dev/null +++ b/Projet_POO/src/Réseau/TCPServer.java @@ -0,0 +1,67 @@ +package Réseau; +import java.net.*; +import java.io.*; + + + +public class TCPServer { + ServerSocket socket; + private String host; + private int port; + private InetAddress address; + + public TCPServer (String host, int backlog, int port) { + this.port = port; + this.host = host; + try { + this.address = InetAddress.getByName(host); + socket = new ServerSocket(port, backlog, this.address); + } + catch (UnknownHostException e) { + System.out.print("Could not find " + host); + this.address = null; + this.socket = null; + } + catch (IOException e) { + System.out.print("Could not create socket"); + this.socket = null; + } + + } + + + public Socket accept() throws IOException{ + return this.socket.accept(); + } + + + public static void main(String[] args) { + TCPServer server = new TCPServer("localhost", 5, 1999); + Socket link = null; + BufferedReader input = null; + PrintWriter output = null; + try { + link = server.accept(); + input = new BufferedReader(new InputStreamReader(link.getInputStream())); + output = new PrintWriter(link.getOutputStream(),true); + } + catch (IOException e) { + System.out.print("Error while connecting to client"); + } + try { + String message = input.readLine(); + output.print(message); + output.flush(); + } + catch (IOException e) { + System.out.print("Error while communicating with client"); + } + try { + link.close(); + } + catch (IOException e) { + System.out.print("Error while closing connection"); + } + } + +} diff --git a/Projet_POO/src/Réseau/TCPServerThread.java b/Projet_POO/src/Réseau/TCPServerThread.java new file mode 100644 index 0000000..6e3d7a1 --- /dev/null +++ b/Projet_POO/src/Réseau/TCPServerThread.java @@ -0,0 +1,39 @@ +package Réseau; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.net.UnknownHostException; + +public class TCPServerThread extends tcpClient implements Runnable{ + + Thread thread; + + public TCPServerThread(Socket socket) { + this.host = socket.getInetAddress().getHostName(); + this.port = socket.getPort(); + this.socket = socket; + try { + this.input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + } + catch (IOException e) { + System.out.print("Error while reading input stream"); + } + try { + this.output = new PrintWriter(socket.getOutputStream(),true); + } + catch (IOException e) { + System.out.print("Error while reading output stream"); + } + thread = new Thread(this); + thread.start(); + } + + public void run() { + String message = this.receive(); + this.send(message); + this.stop(); + } +} diff --git a/Projet_POO/src/Réseau/TCPServerWithThreads.java b/Projet_POO/src/Réseau/TCPServerWithThreads.java new file mode 100644 index 0000000..8c7a974 --- /dev/null +++ b/Projet_POO/src/Réseau/TCPServerWithThreads.java @@ -0,0 +1,32 @@ +package Réseau; +import java.io.IOException; +import java.net.*; + +public class TCPServerWithThreads extends TCPServer{ + private TCPServerThread[] clientThreads; + + public TCPServerWithThreads (String host, int backlog, int port) { + super(host, backlog, port); + } + + public Socket accept() { + Socket ssocket = null; + try { + ssocket = this.socket.accept(); + } + catch (IOException e) { + + } + new TCPServerThread(ssocket); + return ssocket; + } + + public static void main(String[] args) { + + TCPServerWithThreads server = new TCPServerWithThreads("localhost", 5, 19999); + + while(true) { + server.accept(); + } + } +} diff --git a/Projet_POO/src/Réseau/tcpClient.java b/Projet_POO/src/Réseau/tcpClient.java new file mode 100644 index 0000000..ef98e90 --- /dev/null +++ b/Projet_POO/src/Réseau/tcpClient.java @@ -0,0 +1,91 @@ +package Réseau; +import java.net.*; +import java.io.*; + +public class tcpClient { + String host = null; + int port = 0; + Socket socket = null; + BufferedReader input = null; + PrintWriter output = null; + + public tcpClient() { + this.host = null; + this.port = 0; + this.socket = null; + this.input = null; + this.output = null; + } + + public tcpClient(String host, int port) { + this.host = host; + this.port = port; + try { + this.socket = new Socket(host, port); + } + catch (UnknownHostException e) { + System.out.print("Could not find host"); + } + catch (IOException e) { + System.out.print("Could not create socket"); + } + try { + this.input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + } + catch (IOException e) { + System.out.print("Error while reading input stream"); + } + try { + this.output = new PrintWriter(socket.getOutputStream(),true); + } + catch (IOException e) { + System.out.print("Error while reading output stream"); + } + + } + + public void send(String message) { + this.output.print(message); + this.output.flush(); + } + + public String receive() { + String message = null; + try { + message = input.readLine(); + } + catch (IOException e) { + System.out.print("Error while reading buffer"); + } + return message; + } + + public void stop() { + try { + this.socket.close(); + } + catch (IOException e) { + System.out.print("Error while closing connection"); + } + + + } + + public void changePort(int port) throws IOException { + this.stop(); + this.port = port; + try { + this.socket = new Socket(host, port); + } + catch (UnknownHostException e) { + System.out.print("Could not find host"); + } + } + + public static void main(String[] args) { + tcpClient client = new tcpClient("localhost", 19999); + client.send("Bonjour\n"); + System.out.print(client.receive()); + client.stop(); + } +}