forked from elmehdilaagad/Mini-application-of-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmetteurClientUDP.java
executable file
·103 lines (77 loc) · 2.64 KB
/
EmetteurClientUDP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class EmetteurClientUDP extends Thread {
private DatagramSocket serverSocket;
private int port = 0;
private String ia = "";
private String chaine = "";
private String pseudo = "" ;
private Scanner sc = new Scanner(System.in);
private String nameGroupe;
private Client client;
private ProprieteClient propriete;
public EmetteurClientUDP(ProprieteClient c , Client cl ){
this.client = cl;
this.propriete = c;
}
public static void JoinEmTCP() throws InterruptedException{
Thread.currentThread().join();
}
public void run(){
pseudo = propriete.getPseudo();
port = propriete.getPort();
ia = propriete.getAddr();
System.out.println("Vous pouvez Žcrire ˆ " + port);
try {
serverSocket = new DatagramSocket();
System.out.print("> ");
while (true){
InetAddress adresse = InetAddress.getByName(ia);
chaine = sc.nextLine();
if(chaine.equals("")){
System.out.print("> ");
continue;
}else if(chaine.equals("stop")){
Thread.sleep(2000);
System.out.println("----------------------- Fin de la conversation ");
Thread.currentThread().join();
System.out.print("> ");
}else if(chaine.startsWith("createG:")){
nameGroupe = chaine.substring(chaine.indexOf(":")+1, chaine.length());
}else if(chaine.startsWith("joinG:")){
nameGroupe = chaine.substring(chaine.indexOf(":")+1, chaine.indexOf("?"));
String user = chaine.substring(chaine.indexOf("?")+1, chaine.length() );
ProprieteClient _c = client.Trouver2(user);
if(_c != null){
System.out.println(user + " vient de rejoindre le groupe " + nameGroupe);
}else {
System.out.println("Utilisateur introuvable !!" + user );
}
/*
}else if(chaine.startsWith("quitG:")){
nameGroupe = chaine.substring(chaine.indexOf(":")+1, chaine.indexOf("?"));
String user = chaine.substring(chaine.indexOf("?")+1, chaine.length() );
ProprieteClient _c = client.getClient().contains(user);
if(_c != null){
System.out.println(user + " vient de quitter le groupe " + nameGroupe);
}else {
System.out.println("Utilisateur introuvable !" + user );
}
*/
}else if(chaine.equals("showG")){
}else if(chaine.equals("showGD")){
}else{
byte[] buffer = chaine.getBytes();
DatagramPacket messagePacket = new DatagramPacket(buffer,
buffer.length, adresse, port);
serverSocket.send(messagePacket);
}
System.out.print("> ");
}
}catch(Exception e){
e.printStackTrace();
}
}
}