Skip to content

Commit

Permalink
avant test d'implémentation sur le projet
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyesKhama committed Dec 20, 2017
1 parent 8ccf889 commit 267a29f
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import java.util.Scanner;

public class Main {
Expand All @@ -17,4 +16,4 @@ public User getUser(){
}


}
}
52 changes: 52 additions & 0 deletions src/Main2.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
}
32 changes: 32 additions & 0 deletions src/Main21.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

}
52 changes: 52 additions & 0 deletions src/MainTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}


}
2 changes: 1 addition & 1 deletion src/SendMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public static void sendMessage(InetAddress address, int port) throws IOException

}

}
}
52 changes: 52 additions & 0 deletions src/ThreadISendTCP.java
Original file line number Diff line number Diff line change
@@ -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);
}
}


}
2 changes: 1 addition & 1 deletion src/ThreadMenu.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import static java.lang.Integer.parseInt;
import java.util.Scanner;

Expand Down Expand Up @@ -28,6 +27,7 @@ public void run(){

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


}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ThreadReceive.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ public synchronized static void ajoutUserListe(String pseudo, String ip){
}


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

Expand All @@ -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");
Expand All @@ -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);
}

}
}
}
}
2 changes: 1 addition & 1 deletion src/ThreadSend.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public static void sendMessageBroadcast() throws IOException {
User.getSocketEnvoi().send(packet);
}

}
}
41 changes: 20 additions & 21 deletions src/ThreadSendTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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



}
}
Loading

0 comments on commit 267a29f

Please sign in to comment.