Mise en commun modules server et client

This commit is contained in:
Marino Benassai 2020-11-23 16:25:52 +01:00
parent 34ebf718e1
commit 297bf91044
4 changed files with 229 additions and 0 deletions

View file

@ -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");
}
}
}

View file

@ -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();
}
}

View file

@ -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();
}
}
}

View file

@ -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();
}
}