messageTcp

This commit is contained in:
LMAGallois 2020-12-04 17:14:19 +01:00
parent f1898738c1
commit b7a0265261
3 changed files with 125 additions and 3 deletions

View file

@ -1,5 +1,10 @@
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*; import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Scanner; // Import the Scanner class import java.util.Scanner; // Import the Scanner class
import java.util.ArrayList; // import the ArrayList class import java.util.ArrayList; // import the ArrayList class
@ -119,6 +124,9 @@ public class User{
* Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations * Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations
* On regarde si le pseudo est bien différent de l'ancien * On regarde si le pseudo est bien différent de l'ancien
*/ */
private void setPseudo() throws IOException { private void setPseudo() throws IOException {
String oldPseudo = this.pseudo; //Saves the old one for comparison String oldPseudo = this.pseudo; //Saves the old one for comparison
@ -161,6 +169,53 @@ public class User{
System.out.println("Your nickname : " + tmpPseudo + " is valid !"); System.out.println("Your nickname : " + tmpPseudo + " is valid !");
} }
public void TCPmessage(int index) throws IOException {
Socket link=null;
try {
link=new Socket(this.userChatList.get(index).addIP,this.portTCP);
System.out.println("Server is listening on port"+this.portTCP+"of localhost");
}catch(IOException e) {
System.out.println("Server is not listening on port"+this.portTCP+" of localhost");
}
BufferedReader in=null;
try {
in = new BufferedReader(new InputStreamReader(link.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
PrintWriter out = new PrintWriter(link.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String input;
try {
while (!(input=in.readLine()).equals("end")) {
System.out.print("client_recoit:"+input);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*or (int i=0; i<4; i++) {
System.out.println("client envoie");
out.println("coucou \n");
}
out.println("end");*/
try {
link.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* validatePseudo /* validatePseudo
* $parametres * $parametres
* *tmpPseudo : String => Le pseudo à valider * *tmpPseudo : String => Le pseudo à valider
@ -342,7 +397,7 @@ public class User{
* $description * $description
* Laisse l'utilisateur choisir parmis la liste d'utilisateur actif celui avec lequel il souhaite échangé via un chat * Laisse l'utilisateur choisir parmis la liste d'utilisateur actif celui avec lequel il souhaite échangé via un chat
*/ */
public void getOneActiveUser() throws UnknownHostException { public void getOneActiveUser() throws IOException {
this.printRemoteUserList(); this.printRemoteUserList();
Scanner sc2= new Scanner(System.in); Scanner sc2= new Scanner(System.in);
System.out.println("Please, enter index of one active user that you saw on the list to start a conversation with:"); System.out.println("Please, enter index of one active user that you saw on the list to start a conversation with:");
@ -360,6 +415,8 @@ public class User{
else { else {
System.out.println("Wrong index (no active at index number "+index+" )"); System.out.println("Wrong index (no active at index number "+index+" )");
} }
this.TCPmessage(index);
//sc2.close(); //sc2.close();
} }
@ -378,6 +435,9 @@ public class User{
System.out.println("Sleep mode for 5 seconds"); System.out.println("Sleep mode for 5 seconds");
Thread.sleep(5000); Thread.sleep(5000);
// On ferme les différents threads et socket d'écoute // On ferme les différents threads et socket d'écoute
usr1.threadListeningUDP.close(); usr1.threadListeningUDP.close();
usr2.threadListeningUDP.close(); usr2.threadListeningUDP.close();
usr3.threadListeningUDP.close(); usr3.threadListeningUDP.close();

View file

@ -1,8 +1,16 @@
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UserListeningThreadTCP extends Thread{ public class UserListeningThreadTCP extends Thread{
@ -24,6 +32,9 @@ public class UserListeningThreadTCP extends Thread{
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/* run /* run
@ -37,6 +48,56 @@ public class UserListeningThreadTCP extends Thread{
// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation // Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
while(true) { while(true) {
ServerSocket servSocket=null;
try {
servSocket = new ServerSocket(myUser.portTCP);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket link=null;
try {
link = servSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter out=null;
try {
out = new PrintWriter(link.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("awaiting data...");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
out.println(dateFormat.format(date));
out.println("end");
String input;
/*while (!(input=in.readLine()).equals("end")) {
System.out.print("server_recoit:"+input);
}*/
try {
link.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
servSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }
@ -55,4 +116,5 @@ public class UserListeningThreadTCP extends Thread{
e.printStackTrace(); e.printStackTrace();
} }
} }
} }