forked from elmehdilaagad/Mini-application-of-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
executable file
·195 lines (152 loc) · 4.88 KB
/
Client.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Vector;
import javax.crypto.Cipher;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
public class Client {
Socket socket;
BufferedReader in;
PrintWriter out;
private String addr = "172.28.174.181"; //l'adresse par dŽfaut
private int port = 1027;
private String pseudo = "";
private int localport;
public static Vector<ProprieteClient> clientConnected = new Vector<ProprieteClient>() ;
public static Vector<ProprieteGroup> groupConnected = new Vector<ProprieteGroup>();
//private Vector<Groupe> groupes = new Vector<Groupe>();
public Client(){
}
public static int port() throws Exception{
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
System.out.println(" PORT CHOISI :"+port);
return port;
}
public ProprieteClient Trouver2(String pseudo){
for(int i=0;i<clientConnected.size();i++){
ProprieteClient data = clientConnected.get(i);
if(data.getPseudo().equals(pseudo) ){
return data;
}
}
return null;
}
public static Vector<ProprieteClient> getClient(){return clientConnected;}
public Vector<ProprieteGroup> getGroup() { return groupConnected;}
public int getPort() { return port;}
public String getAddr() {return addr;}
public String getPseudo() {return pseudo;}
public int getLocalPort() {return localport;}
public Socket getSocket() {return socket;}
//public Vector<Groupe> getGoupes() {return groupes; }
public void setLocalport(int l) {this.localport = l;}
public void setAddr(String addr) {this.addr = addr;}
public void setPseudo(String pseudo) {this.pseudo = pseudo;}
public void ClientConnected (int LocalPort){
if(clientConnected.size()==0){
System.out.print("0 clients connectes");
return;
}
System.out.println("Liste Client ConnectŽs :");
for(int i=0;i<clientConnected.size();i++){
ProprieteClient c = clientConnected.get(i);
String pseudo = c.getPseudo();
String adr = c.getAddr();
int p = c.getPort();
if(LocalPort != p ){
// && !this.getAddr().equals(addr)
System.out.println("n¡" + i +" : "+ pseudo +
" | Adresse : " + adr +
" | Port : " + p);
}
}
}
public void GroupConnected (){
if(groupConnected.size()==0){
System.out.print("0 Groupes prŽsents");
return;
}
System.out.println("Liste Groupes PrŽsents :");
for(int i=0;i<groupConnected.size();i++){
ProprieteGroup c = groupConnected.get(i);
String nom = c.getNom();
String adr = c.getAddressMulticast();
int p = c.getPortMulticast();
System.out.println("n¡" + i +" : "+ nom +
" | Adresse : " + adr +
" | Port : " + p);
}
}
public static void main(String[] args){
Socket socket = null;
Client client = null ;
DatagramSocket ds = null ;
//Usage java TP1.Client [ipServeur] pseudo
if(args.length == 2 && isValidPseudo(args[1]) ){
client = new Client() ;
client.setPseudo(args[1]);
}else{
System.out.println("Usage Incorrect ! java TP1.Client ipServeur pseudo (6 caractres) ");
System.exit(0);
}
try {
// Initialiser la socket client sur l'addr et le port 1024
//int p = port();
//client.setLocalport(p);
socket = new Socket(args[0], client.getPort());
System.out.println("Pseudo :"+ client.getPseudo() + "\n" +
"\tHost => "+socket.getInetAddress().getHostAddress() + "\n" +
"\tPort => "+socket.getLocalPort());
client.setLocalport(socket.getLocalPort());
ds = new DatagramSocket(client.getLocalPort());
} catch (Exception e){
System.out.println("le serveur n'est pas lancŽ [Connection refused] !!");
}
//recevoir la reponse from server
RecepteurClientTCP recep = new RecepteurClientTCP(socket,client);
recep.setName("RecepeteurClientTCP");
//envoie une requete to serveur
EmetteurClientTCP emetteur = new EmetteurClientTCP(socket,client);
emetteur.setName("EmetteurClientTCP");
RecepteurClientUDP recepteur = new RecepteurClientUDP(ds);
recepteur.start();
emetteur.start();
recep.start();
}
private static boolean isValidPseudo(String p) {
if(p.length() != 6) return false;
return true;
}
/*
public static Vector<ClientProperties> getClients() {
return vectClients;
}
public ClientProperties getClientByPseudo(String psd){
for(int i=0;i<getClients().size();i++){
ClientProperties c = getClients().get(i);
if(c.getPseudo().equals(psd) ){
return c;
}
}
return null;
}
*/
/**
* Cette mŽthode vŽrifie que le client passŽ en argument existe bien dans le vecteur de clients
* @param c: l'objet dont on veut conna”tre l'existence
* @return
*/
/*
public static boolean existeClient(ClientProperties c) {
for(int i=0;i< getClients().size();i++){
ClientProperties cp = getClients().get(i);
String addr = cp.getAddr(); int p = cp.getPort();
if( addr.equals(c.getAddr()) && p == c.getPort() )
return true;
}
return false;
}
*/
}