Skip to content

Commit

Permalink
test pr ad
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyesKhama committed Dec 20, 2017
1 parent 267a29f commit 3d96636
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 97 deletions.
34 changes: 1 addition & 33 deletions src/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}


Expand Down
4 changes: 1 addition & 3 deletions src/ThreadISendTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,5 @@ public static void main(String[] args){
} catch (IOException ex) {
Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex);
}
}


}
}
72 changes: 38 additions & 34 deletions src/ThreadReceiveTCP.java
Original file line number Diff line number Diff line change
@@ -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 );
}
}

}
}
}
} catch (IOException ex) {
Logger.getLogger(ThreadSendTCP.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
40 changes: 17 additions & 23 deletions src/ThreadSendTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}



}
5 changes: 1 addition & 4 deletions src/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class User {

private HashMap<String, String> listUser ;


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

Expand All @@ -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(){
Expand All @@ -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();


Expand Down

0 comments on commit 3d96636

Please sign in to comment.