From 3d9663608ab05fbab5f0044351501d0bfb9b4e3b Mon Sep 17 00:00:00 2001 From: ElyesKhama Date: Wed, 20 Dec 2017 19:30:18 +0100 Subject: [PATCH] test pr ad --- src/MainTest.java | 34 +----------------- src/ThreadISendTCP.java | 4 +-- src/ThreadReceiveTCP.java | 72 +++++++++++++++++++++------------------ src/ThreadSendTCP.java | 40 +++++++++------------- src/User.java | 5 +-- 5 files changed, 58 insertions(+), 97 deletions(-) diff --git a/src/MainTest.java b/src/MainTest.java index 33fc277..f028fb4 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -13,39 +13,7 @@ public class MainTest { public static void main(String[] args){ - Socket socket; - try { - socket = new Socket("192.168.2.5",45001); - System.out.println("port ou jessaye d'envoyer "+ 45001 ); - - if(socket.isConnected()){ //Envoie des msgs - ThreadTesssst thread = new ThreadTesssst("ee", socket); - thread.start(); - System.out.println("démarrage du thread d'envoie des msgs"); - } - - InputStreamReader stream = new InputStreamReader(socket.getInputStream()); - BufferedReader reader = new BufferedReader(stream); - - //System.out.println("je vais lui rep "); - // System.out.println("je lui ai rep "); - // testMsg(socket); - - - - while(socket.isConnected()){ // Boucle de réception des msgs - String a = reader.readLine(); - if (a != null ){ - System.out.println("le client m'a envoyé : " + a ); - } - } - - - - - } catch (IOException ex) { - Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); - } + } diff --git a/src/ThreadISendTCP.java b/src/ThreadISendTCP.java index cf78005..62c3bf0 100644 --- a/src/ThreadISendTCP.java +++ b/src/ThreadISendTCP.java @@ -46,7 +46,5 @@ public static void main(String[] args){ } catch (IOException ex) { Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); } - } - - + } } diff --git a/src/ThreadReceiveTCP.java b/src/ThreadReceiveTCP.java index 65cbb93..438c6eb 100644 --- a/src/ThreadReceiveTCP.java +++ b/src/ThreadReceiveTCP.java @@ -1,43 +1,47 @@ +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.PrintWriter; -import java.net.ServerSocket; import java.net.Socket; +import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class ThreadReceiveTCP extends Thread { + static public User user; - ServerSocket client1Socket = null; - - public ThreadReceiveTCP(String name){ - super(name); - - } - - public void run(){ - - - try { - System.out.println("Lancement de l'ecoute tcp"); - client1Socket = new ServerSocket(Main.user.getPort()); - } catch (IOException ex) { - Logger.getLogger(ThreadReceiveTCP.class.getName()).log(Level.SEVERE, null, ex); - } - - while(true){ - try { - - System.out.println("port ou jessaye d'ecouter : " + client1Socket.getLocalPort()); - Socket client2Socket = client1Socket.accept(); - System.out.println("Connexion effective"); - PrintWriter writer = new PrintWriter(client2Socket.getOutputStream()); - writer.println("Coucou "); - writer.close(); - - } catch (IOException ex) { - Logger.getLogger(ThreadReceiveTCP.class.getName()).log(Level.SEVERE, null, ex); + public ThreadReceiveTCP(String name){ + } + + public void run(){ + Socket socket; + try { + socket = new Socket("192.168.2.5",45001); + System.out.println("port ou jessaye d'envoyer "+ 45001 ); + + if(socket.isConnected()){ //Envoie des msgs + Main21 thread = new Main21(socket); + thread.start(); + System.out.println("démarrage du thread d'envoie des msgs"); + } + + InputStreamReader stream = new InputStreamReader(socket.getInputStream()); + BufferedReader reader = new BufferedReader(stream); + + //System.out.println("je vais lui rep "); + // System.out.println("je lui ai rep "); + // testMsg(socket); + + + + while(socket.isConnected()){ // Boucle de réception des msgs + String a = reader.readLine(); + if (a != null ){ + System.out.println("le client m'a envoyé : " + a ); + } } - - } - } -} \ No newline at end of file + } catch (IOException ex) { + Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/src/ThreadSendTCP.java b/src/ThreadSendTCP.java index 785e152..7d69169 100644 --- a/src/ThreadSendTCP.java +++ b/src/ThreadSendTCP.java @@ -16,41 +16,35 @@ public ThreadSendTCP(String name, String ip1, int port1){ super(name); ip = ip1; port = port1; - } public void run() { - Socket socket; + Socket socket; try { - socket = new Socket(ip,port); - System.out.println("port ou jessaye d'envoyer "+ port ); + socket = new Socket("192.168.2.5",45001); + System.out.println("port ou jessaye d'envoyer "+ 45001 ); + + if(socket.isConnected()){ //Envoie des msgs + ThreadTesssst thread = new ThreadTesssst("ee", socket); + thread.start(); + System.out.println("démarrage du thread d'envoie des msgs"); + } + InputStreamReader stream = new InputStreamReader(socket.getInputStream()); BufferedReader reader = new BufferedReader(stream); - String message = reader.readLine(); - System.out.println("le client m'a rep : " + message ); - System.out.println("je vais lui rep "); - testMsg(socket); - System.out.println("je lui ai rep "); + + while(socket.isConnected()){ // Boucle de réception des msgs + String a = reader.readLine(); + if (a != null ){ + System.out.println("le client m'a envoyé : " + a ); + } + } } catch (IOException ex) { Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); } - - - } - - public void testMsg(Socket socket){ - PrintWriter writer = null; - try { - writer = new PrintWriter(socket.getOutputStream()); - } catch (IOException ex) { - Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); - } - writer.println("Coucou "); - writer.close(); } - } \ No newline at end of file diff --git a/src/User.java b/src/User.java index ab62b55..010a49c 100644 --- a/src/User.java +++ b/src/User.java @@ -15,7 +15,6 @@ public class User { private HashMap listUser ; - public User(String pseudo){ listUser = new HashMap<>(); @@ -30,8 +29,6 @@ public User(String pseudo){ this.pseudo = pseudo; System.out.println("J'ai crée un utilisateur ; son pseudo est : " + pseudo ); - - } public void startThread(){ @@ -44,7 +41,7 @@ public void startThread(){ ThreadMenu threadMenu = new ThreadMenu("thread menu"); threadMenu.start(); - ThreadReceiveTCP threadReceiveTCP = new ThreadReceiveTCP("name"); + ThreadReceiveTCP threadReceiveTCP = new ThreadReceiveTCP("receive tcp"); threadReceiveTCP.start();