Skip to content

Commit

Permalink
mercredi avant de dormir ca marche
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyesKhama committed Dec 20, 2017
1 parent 3d96636 commit 11df437
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 109 deletions.
2 changes: 2 additions & 0 deletions src/Main2.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public static void main(String[] args){
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();

Expand Down
30 changes: 29 additions & 1 deletion src/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,35 @@ 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);
}
}


Expand Down
37 changes: 37 additions & 0 deletions src/ThreadAcceptTCP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ThreadAcceptTCP extends Thread{

private ThreadReceiveTCPFinal threadReceiveTCP = null;
private ThreadSendTCPFinal threadSendTCP = null;


public ThreadAcceptTCP(String name) {
super(name);
}

public void run() {
Socket client2Socket = null;
ServerSocket client1Socket;
try {
client1Socket = new ServerSocket(Main.user.getPort());
System.out.println("port ou jessaye d'ecouter : " + client1Socket.getLocalPort());
System.out.println(" " + client1Socket.isClosed());
if(!client1Socket.isClosed()) {

client2Socket = client1Socket.accept();
threadReceiveTCP = new ThreadReceiveTCPFinal(client2Socket);
threadReceiveTCP.start();
threadSendTCP = new ThreadSendTCPFinal("Send",client2Socket.getInetAddress().toString(),client2Socket.getPort(),client2Socket,false);
threadSendTCP.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
50 changes: 0 additions & 50 deletions src/ThreadISendTCP.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/ThreadMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ public void run(){

switch(choix){
case 1: System.out.println(Main.user.getListUser().toString());
choix = 9 ;

break;
case 2: System.out.println("A qui veux tu parler ? entre son pseudo ");
String pseudo = sc.next();
System.out.println("pseudo entré: " + pseudo);
String[] info = Main.user.getListUser().get(pseudo).toString().split("-");

Main.user.startThreadTCP(info[0], parseInt(info[1],10));
choix = 9 ;
break;



}
Expand Down
72 changes: 34 additions & 38 deletions src/ThreadReceiveTCP.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
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;

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 );
}
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);
}
} catch (IOException ex) {
Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

}
}
}
34 changes: 34 additions & 0 deletions src/ThreadReceiveTCPFinal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ThreadReceiveTCPFinal extends Thread {

private Socket socket;

public ThreadReceiveTCPFinal(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()) {
System.out.println("Je lis ");

String a = reader.readLine();
if(a != null)
System.out.println(a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
40 changes: 23 additions & 17 deletions src/ThreadSendTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,41 @@ 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("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");
}

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);

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



}
58 changes: 58 additions & 0 deletions src/ThreadSendTCPFinal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

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 ThreadSendTCPFinal extends Thread {

private Socket socket;
private boolean lancementOuPas;
private String ip;
private int port;

public ThreadSendTCPFinal(String name,String ip, int port, Socket socket1,boolean lancementOuPas){
super(name);
socket = socket1;
this.lancementOuPas = lancementOuPas;
this.ip = ip;
this.port = port;
}

@Override
public void run(){
try {
if(lancementOuPas) {
System.out.println("je passe mauvais delir ");
socket = new Socket(ip,port);
ThreadReceiveTCPFinal threadReceive = new ThreadReceiveTCPFinal( socket);
threadReceive.start();
}
PrintWriter writer = null;
Scanner sc = new Scanner(System.in);
System.out.println("Possibilité d'envoyer des msgs ");
writer = new PrintWriter(socket.getOutputStream());
while(socket.isConnected()){
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(ThreadSendTCPFinal.class.getName()).log(Level.SEVERE, null, ex);
}

}
}
Loading

0 comments on commit 11df437

Please sign in to comment.