tcpmessage

This commit is contained in:
LMAGallois 2020-12-07 15:58:43 +01:00
parent 0199595cc8
commit 6bef5f9150
6 changed files with 82 additions and 42 deletions

View file

@ -1,2 +1,4 @@
/UserListeningThreadTCP.class
/UserListeningThreadUDP.class
/UserConnexionthreadTCP.class
/Database.class

View file

@ -0,0 +1,9 @@
public class Database {
private User myUser;
public void Database() {
}
}

View file

@ -194,6 +194,7 @@ public class User{
e.printStackTrace();
}
String input;
System.out.println(this.getPseudo()+" reçoit un message");
try {
while (!(input=in.readLine()).equals("end")) {
System.out.print("client_recoit:"+input);
@ -423,9 +424,9 @@ public class User{
public static void main(String[] args) throws IOException, InterruptedException {
// Création des utilisateurs
User usr2 = new User(12222,portUDPlistening_remoteUsr2,22222,"Mike"); // simulation d'un utilisateur distant n1
User usr3 = new User(12223,portUDPlistening_remoteUsr3,22223,"Alice"); // simulation d'un utilisateur distant n2
User usr1 = new User(12221,20001,22221); // Notre utilisateur local
User usr2 = new User(12226,portUDPlistening_remoteUsr2,22222,"Mike"); // simulation d'un utilisateur distant n1
User usr3 = new User(12224,portUDPlistening_remoteUsr3,22223,"Alice"); // simulation d'un utilisateur distant n2
User usr1 = new User(12225,20001,22221); // Notre utilisateur local
// Fonction appelé par notre utilisateur local
usr1.getOneActiveUser();

View file

@ -0,0 +1,55 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UserConnexionthreadTCP extends Thread{
private User myUser;
private Socket socket;
public UserConnexionthreadTCP(String s,User user,Socket socket) {
super(s);
this.myUser = user;
this.socket=socket;
}
public void run() {
Socket link = this.socket;
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();
}
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(myUser.getPseudo()+" envoie un message");
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();
}
}
}

View file

@ -15,7 +15,7 @@ import java.util.Date;
public class UserListeningThreadTCP extends Thread{
private User myUser;
private DatagramSocket dgramSocket=null;
//private DatagramSocket dgramSocket=null;
/* CONSTRUCTOR OF UserListeningThreadTCP
* $parametres
@ -27,13 +27,20 @@ public class UserListeningThreadTCP extends Thread{
public UserListeningThreadTCP(String s,User user) {
super(s);
this.myUser = user;
try {
/* try {
this.dgramSocket = new DatagramSocket(this.myUser.getPortTCP(),this.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
*/
}
public void accept(ServerSocket servSocket) throws IOException {
UserConnexionthreadTCP threadtcp= new UserConnexionthreadTCP("Chat_with_"+myUser.getPseudo(),myUser,servSocket.accept());
threadtcp.start();
}
@ -55,48 +62,14 @@ public class UserListeningThreadTCP extends Thread{
// 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();
this.accept(servSocket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
servSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@ -108,7 +81,7 @@ public class UserListeningThreadTCP extends Thread{
* interrupt UDP listening threadS
*/
public void close() {
this.dgramSocket.close();
// this.dgramSocket.close();
System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")");
try {
this.interrupt();