Skip to content

Commit

Permalink
17:27
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyesKhama committed Dec 21, 2017
1 parent 109406f commit fb08d6b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/SourcePackage/SetThreadCommunication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
3 changes: 2 additions & 1 deletion src/SourcePackage/ThreadAcceptTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 5 additions & 7 deletions src/SourcePackage/ThreadSendTCPFinal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ");

Expand Down Expand Up @@ -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();
}
Expand Down
18 changes: 15 additions & 3 deletions src/SourcePackage/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer,Socket> listSocket;


private HashMap<String, String> listUser ;

public User(String pseudo){
listUser = new HashMap<>();

listSocket = new HashMap();

try {
this.socketEnvoi = new DatagramSocket();
this.socketEcoute = new DatagramSocket(45047);
Expand Down Expand Up @@ -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();

}

Expand Down

0 comments on commit fb08d6b

Please sign in to comment.