diff --git a/src/Main.java b/src/Main.java index bd052ea..f310827 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,3 @@ - import java.util.Scanner; public class Main { @@ -17,4 +16,4 @@ public User getUser(){ } -} +} \ No newline at end of file diff --git a/src/Main2.java b/src/Main2.java new file mode 100644 index 0000000..0ddf589 --- /dev/null +++ b/src/Main2.java @@ -0,0 +1,52 @@ +import java.io.IOException; +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 Main2 { + + public static void main(String[] args){ + int i = 0; + while(true){ + try { + Socket client2Socket = null; + ServerSocket client1Socket = new ServerSocket(45001); + System.out.println("port ou jessaye d'ecouter : " + client1Socket.getLocalPort()); + System.out.println(" " + client1Socket.isClosed()); + Main21 threadSendTCP = null; + if(!client1Socket.isClosed()) { + + client2Socket = client1Socket.accept(); + threadSendTCP = new Main21(client2Socket); + threadSendTCP.start(); + } + System.out.println("Connexion effective"); + PrintWriter writer = new PrintWriter(client2Socket.getOutputStream()); + i++; + Scanner sc = new Scanner(System.in); + while(!client2Socket.isClosed()) { + String msg = sc.next(); + + writer.println(msg); + writer.flush(); + if(msg.equals("quit")) { + writer.close(); + client2Socket.close(); + threadSendTCP.stop(); + } + + } + client1Socket.close(); + System.out.println("closed"); + + + } catch (IOException ex) { + Logger.getLogger(ThreadReceiveTCP.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} +} diff --git a/src/Main21.java b/src/Main21.java new file mode 100644 index 0000000..053d006 --- /dev/null +++ b/src/Main21.java @@ -0,0 +1,32 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.Socket; + +public class Main21 extends Thread { + + private Socket socket; + + public Main21(Socket socket) { + this.socket = socket; + } + + public void run() { + InputStreamReader stream; + try { + stream = new InputStreamReader(socket.getInputStream()); + BufferedReader reader = new BufferedReader(stream); + System.out.println("Je vais lire1"); + while(socket.isConnected()) { + String a = reader.readLine(); + if(a != null) + System.out.println(a); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +} diff --git a/src/MainTest.java b/src/MainTest.java new file mode 100644 index 0000000..33fc277 --- /dev/null +++ b/src/MainTest.java @@ -0,0 +1,52 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class MainTest { + static public User user; + + 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/SendMessage.java b/src/SendMessage.java index f217393..3285869 100644 --- a/src/SendMessage.java +++ b/src/SendMessage.java @@ -27,4 +27,4 @@ public static void sendMessage(InetAddress address, int port) throws IOException } -} +} \ No newline at end of file diff --git a/src/ThreadISendTCP.java b/src/ThreadISendTCP.java new file mode 100644 index 0000000..cf78005 --- /dev/null +++ b/src/ThreadISendTCP.java @@ -0,0 +1,52 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ThreadISendTCP { + static public User user; + + 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 + 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 ); + } + } + + + + + } catch (IOException ex) { + Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); + } + } + + +} diff --git a/src/ThreadMenu.java b/src/ThreadMenu.java index 943dae6..ef37bda 100644 --- a/src/ThreadMenu.java +++ b/src/ThreadMenu.java @@ -1,4 +1,3 @@ - import static java.lang.Integer.parseInt; import java.util.Scanner; @@ -28,6 +27,7 @@ public void run(){ Main.user.startThreadTCP(info[0], parseInt(info[1],10)); break; + } } diff --git a/src/ThreadReceive.java b/src/ThreadReceive.java index 2e4ed45..8b0e6d6 100644 --- a/src/ThreadReceive.java +++ b/src/ThreadReceive.java @@ -61,4 +61,4 @@ public synchronized static void ajoutUserListe(String pseudo, String ip){ } -} +} \ No newline at end of file diff --git a/src/ThreadReceiveTCP.java b/src/ThreadReceiveTCP.java index ff706f5..65cbb93 100644 --- a/src/ThreadReceiveTCP.java +++ b/src/ThreadReceiveTCP.java @@ -1,9 +1,7 @@ - import java.io.IOException; 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; @@ -17,7 +15,7 @@ public ThreadReceiveTCP(String name){ } public void run(){ - Scanner sc = new Scanner(System.in); + try { System.out.println("Lancement de l'ecoute tcp"); @@ -28,21 +26,18 @@ public void run(){ while(true){ try { + System.out.println("port ou jessaye d'ecouter : " + client1Socket.getLocalPort()); Socket client2Socket = client1Socket.accept(); - System.out.println("Connexion effective"); + System.out.println("Connexion effective"); PrintWriter writer = new PrintWriter(client2Socket.getOutputStream()); - while (client1Socket.isBound()){ - //System.out.println("message a envoyer : "); - //writer.println(sc.next()); - // writer.println("coucou"); - } - writer.println("coucou"); - writer.close(); + writer.println("Coucou "); + writer.close(); + } catch (IOException ex) { Logger.getLogger(ThreadReceiveTCP.class.getName()).log(Level.SEVERE, null, ex); } } } -} +} \ No newline at end of file diff --git a/src/ThreadSend.java b/src/ThreadSend.java index d4cca22..c13b7e6 100644 --- a/src/ThreadSend.java +++ b/src/ThreadSend.java @@ -34,4 +34,4 @@ public static void sendMessageBroadcast() throws IOException { User.getSocketEnvoi().send(packet); } -} +} \ No newline at end of file diff --git a/src/ThreadSendTCP.java b/src/ThreadSendTCP.java index c5deff7..785e152 100644 --- a/src/ThreadSendTCP.java +++ b/src/ThreadSendTCP.java @@ -4,7 +4,6 @@ import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; -import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; @@ -21,37 +20,37 @@ public ThreadSendTCP(String name, String ip1, int port1){ } public void run() { + Socket socket; - try { socket = new Socket(ip,port); System.out.println("port ou jessaye d'envoyer "+ port ); InputStreamReader stream = new InputStreamReader(socket.getInputStream()); BufferedReader reader = new BufferedReader(stream); String message = reader.readLine(); - System.out.println("le client m'a rep : " + message ); - loopMsg(socket); - + 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 "); } catch (IOException ex) { Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex); - } - } + } + + + + } - public void loopMsg(Socket socket) throws IOException{ - while(true){ - InputStreamReader stream = new InputStreamReader(socket.getInputStream()); - BufferedReader reader = new BufferedReader(stream); - String message; - try { - message = reader.readLine(); - System.out.println(message); - } 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/ThreadTesssst.java b/src/ThreadTesssst.java new file mode 100644 index 0000000..a6d0490 --- /dev/null +++ b/src/ThreadTesssst.java @@ -0,0 +1,54 @@ + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ThreadTesssst extends Thread { + + private Socket socket; + + public ThreadTesssst(String name, Socket socket1){ + super(); + socket = socket1; + } + + @Override + public void run(){ + + try { + PrintWriter writer = null; + Scanner sc = new Scanner(System.in); + System.out.println("Possibilité d'envoyer des msgs "); + while(socket.isConnected()){ + try { + writer = new PrintWriter(socket.getOutputStream()); + } catch (IOException ex) { + Logger.getLogger(ThreadTesssst.class.getName()).log(Level.SEVERE, null, ex); + } + + String msg = sc.next(); + + if (msg.equals("quit")){ + this.stop(); + socket.close(); + writer.close(); + System.out.println("il est parti"); + return ; + } + + + writer.println(msg); + writer.flush(); + + + } + + } catch (IOException ex) { + Logger.getLogger(ThreadTesssst.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} diff --git a/src/User.java b/src/User.java index 4881c79..ab62b55 100644 --- a/src/User.java +++ b/src/User.java @@ -53,6 +53,7 @@ public void startThread(){ public void startThreadTCP(String ip , int port){ ThreadSendTCP threadSendTCP = new ThreadSendTCP("name",ip,port); threadSendTCP.start(); + } public User getUser(){