From fb08d6ba59bc1f7382bef5d053572874cb130844 Mon Sep 17 00:00:00 2001 From: ElyesKhama Date: Thu, 21 Dec 2017 17:27:33 +0100 Subject: [PATCH] 17:27 --- src/SourcePackage/SetThreadCommunication.java | 2 +- src/SourcePackage/ThreadAcceptTCP.java | 3 ++- src/SourcePackage/ThreadSendTCPFinal.java | 12 +++++------- src/SourcePackage/User.java | 18 +++++++++++++++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/SourcePackage/SetThreadCommunication.java b/src/SourcePackage/SetThreadCommunication.java index 49736ef..2b52f08 100644 --- a/src/SourcePackage/SetThreadCommunication.java +++ b/src/SourcePackage/SetThreadCommunication.java @@ -8,7 +8,7 @@ public class SetThreadCommunication { public SetThreadCommunication(Socket client2Socket) { threadReceiveTCP = new ThreadReceiveTCPFinal(client2Socket); - threadSendTCP = new ThreadSendTCPFinal("Send",client2Socket.getInetAddress().toString(),client2Socket.getPort(),client2Socket,false); + threadSendTCP = new ThreadSendTCPFinal("Send",client2Socket,false); } diff --git a/src/SourcePackage/ThreadAcceptTCP.java b/src/SourcePackage/ThreadAcceptTCP.java index e82dd00..3236009 100644 --- a/src/SourcePackage/ThreadAcceptTCP.java +++ b/src/SourcePackage/ThreadAcceptTCP.java @@ -32,12 +32,13 @@ public void run() { Socket client2Socket = null; try { System.out.println("port ou jessaye d'ecouter : " + client1Socket.getLocalPort()); - + while(true){ if(!client1Socket.isClosed()) { client2Socket = client1Socket.accept(); listSocket.put(client2Socket.getPort(), client2Socket); // dans l'hash map des socket on va rajouter un socket du client qui se connecte listThreadToSocket.put(client2Socket, new SetThreadCommunication(client2Socket)); //liste des threads } + } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/SourcePackage/ThreadSendTCPFinal.java b/src/SourcePackage/ThreadSendTCPFinal.java index bcf65b6..3e2066a 100644 --- a/src/SourcePackage/ThreadSendTCPFinal.java +++ b/src/SourcePackage/ThreadSendTCPFinal.java @@ -5,6 +5,7 @@ import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; +import java.util.HashMap; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; @@ -19,19 +20,17 @@ public class ThreadSendTCPFinal extends Thread { PrintWriter writer = null; ThreadReceiveTCPFinal threadReceive; - public ThreadSendTCPFinal(String name,String ip, int port, Socket socket1,boolean lancementOuPas){ + public ThreadSendTCPFinal(String name, Socket socket1,boolean lancementOuPas){ super(name); socket = socket1; this.lancementOuPas = lancementOuPas; - this.ip = ip; - this.port = port; } @Override public void run(){ try { - if(lancementOuPas) - socketReceiveIniationThread(); + if(lancementOuPas) + socketReceiveIniationThread(socket); System.out.println("Possibilité d'envoyer des msgs "); @@ -62,8 +61,7 @@ public void run(){ } - private void socketReceiveIniationThread() throws UnknownHostException, IOException { - socket = new Socket(ip,port); + private void socketReceiveIniationThread(Socket socket) throws UnknownHostException, IOException { threadReceive = new ThreadReceiveTCPFinal(socket); threadReceive.start(); } diff --git a/src/SourcePackage/User.java b/src/SourcePackage/User.java index 29e7f7c..0c7affb 100644 --- a/src/SourcePackage/User.java +++ b/src/SourcePackage/User.java @@ -8,18 +8,23 @@ import static java.lang.Thread.sleep; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; public class User { private String pseudo; private static DatagramSocket socketEnvoi; private static DatagramSocket socketEcoute; private static boolean firstMsg = false; + private HashMap listSocket; + private HashMap listUser ; public User(String pseudo){ listUser = new HashMap<>(); - + listSocket = new HashMap(); + try { this.socketEnvoi = new DatagramSocket(); this.socketEcoute = new DatagramSocket(45047); @@ -48,9 +53,16 @@ public void startThread(){ } public void startThreadTCP(String ip , int port){ - ThreadSendTCPFinal threadSendTCP = new ThreadSendTCPFinal("name",ip,port,null,true); - threadSendTCP.start(); + + try { + listSocket.put(port, new Socket(ip,port)); + } catch (IOException ex) { + Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); + } + + ThreadSendTCPFinal threadSendTCP = new ThreadSendTCPFinal("name",listSocket.get(port),true); + threadSendTCP.start(); }