Browse Source

messageTcp

LMAGallois 3 years ago
parent
commit
b7a0265261

BIN
Application/Clavardage/bin/User.class View File


+ 61
- 1
Application/Clavardage/src/User.java View File

@@ -1,5 +1,10 @@
1
+import java.io.BufferedReader;
1 2
 import java.io.IOException;
3
+import java.io.InputStreamReader;
4
+import java.io.PrintWriter;
2 5
 import java.net.*;
6
+import java.text.DateFormat;
7
+import java.text.SimpleDateFormat;
3 8
 import java.util.Date;
4 9
 import java.util.Scanner;  // Import the Scanner class
5 10
 import java.util.ArrayList; // import the ArrayList class
@@ -119,6 +124,9 @@ public class User{
119 124
 	* 		Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations
120 125
 	* 		On regarde si le pseudo est bien différent de l'ancien
121 126
 	*/
127
+	
128
+	
129
+	
122 130
 	private void setPseudo() throws IOException { 
123 131
 		    String oldPseudo = this.pseudo; //Saves the old one for comparison
124 132
 
@@ -161,6 +169,53 @@ public class User{
161 169
 		System.out.println("Your nickname : " + tmpPseudo + " is valid !");
162 170
 	}
163 171
 	
172
+	public void TCPmessage(int index) throws IOException {
173
+		Socket link=null;
174
+		try {
175
+			link=new Socket(this.userChatList.get(index).addIP,this.portTCP);
176
+
177
+			System.out.println("Server is listening on port"+this.portTCP+"of localhost");
178
+		}catch(IOException e) {
179
+			
180
+			System.out.println("Server is not listening on port"+this.portTCP+" of localhost");
181
+			
182
+		}
183
+		BufferedReader in=null;
184
+		try {
185
+			in = new BufferedReader(new InputStreamReader(link.getInputStream()));
186
+		} catch (IOException e) {
187
+			// TODO Auto-generated catch block
188
+			e.printStackTrace();
189
+		}
190
+		try {
191
+			PrintWriter out = new PrintWriter(link.getOutputStream(),true);
192
+		} catch (IOException e) {
193
+			// TODO Auto-generated catch block
194
+			e.printStackTrace();
195
+		}
196
+		String input;
197
+		try {
198
+			while (!(input=in.readLine()).equals("end")) {
199
+				System.out.print("client_recoit:"+input);
200
+			}
201
+		} catch (IOException e) {
202
+			// TODO Auto-generated catch block
203
+			e.printStackTrace();
204
+		}
205
+		/*or (int i=0; i<4; i++) {
206
+			System.out.println("client envoie");
207
+			out.println("coucou \n");
208
+		}
209
+		out.println("end");*/
210
+		try {
211
+			link.close();
212
+		} catch (IOException e) {
213
+			// TODO Auto-generated catch block
214
+			e.printStackTrace();
215
+		}
216
+	}
217
+	
218
+	
164 219
 	/* validatePseudo
165 220
 	 * $parametres
166 221
 	 * 		*tmpPseudo : String => Le pseudo à valider
@@ -342,7 +397,7 @@ public class User{
342 397
 	 * $description
343 398
 	 * 		Laisse l'utilisateur choisir parmis la liste d'utilisateur actif celui avec lequel il souhaite échangé via un chat
344 399
 	 */
345
-	public void getOneActiveUser() throws UnknownHostException {
400
+	public void getOneActiveUser() throws IOException {
346 401
 		this.printRemoteUserList();
347 402
 		Scanner sc2= new Scanner(System.in);
348 403
         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{
360 415
         else {
361 416
         	System.out.println("Wrong index (no active at index number "+index+" )");
362 417
         }
418
+        
419
+        this.TCPmessage(index);
363 420
         //sc2.close();
364 421
     }
365 422
 
@@ -378,6 +435,9 @@ public class User{
378 435
 			System.out.println("Sleep mode for 5 seconds");
379 436
 			Thread.sleep(5000);
380 437
 			// On ferme les différents threads et socket d'écoute
438
+			
439
+			
440
+			
381 441
 		    usr1.threadListeningUDP.close();
382 442
 		    usr2.threadListeningUDP.close();
383 443
 		    usr3.threadListeningUDP.close();

+ 64
- 2
Application/Clavardage/src/UserListeningThreadTCP.java View File

@@ -1,8 +1,16 @@
1
+import java.io.BufferedReader;
1 2
 import java.io.IOException;
3
+import java.io.InputStreamReader;
4
+import java.io.PrintWriter;
2 5
 import java.net.DatagramPacket;
3 6
 import java.net.DatagramSocket;
4 7
 import java.net.InetAddress;
8
+import java.net.ServerSocket;
9
+import java.net.Socket;
5 10
 import java.net.UnknownHostException;
11
+import java.text.DateFormat;
12
+import java.text.SimpleDateFormat;
13
+import java.util.Date;
6 14
 
7 15
 public class UserListeningThreadTCP extends Thread{
8 16
 	
@@ -23,7 +31,10 @@ public class UserListeningThreadTCP extends Thread{
23 31
 			this.dgramSocket = new DatagramSocket(this.myUser.getPortTCP(),this.myUser.getAddIP());
24 32
 		} catch (IOException e) {
25 33
 			e.printStackTrace();
26
-		}	
34
+		}
35
+		
36
+		
37
+		
27 38
 	}
28 39
 	
29 40
 	/* run
@@ -36,7 +47,57 @@ public class UserListeningThreadTCP extends Thread{
36 47
 		
37 48
 		// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
38 49
 		while(true) {
39
-
50
+			
51
+			ServerSocket servSocket=null;
52
+			try {
53
+				servSocket = new ServerSocket(myUser.portTCP);
54
+			} catch (IOException e) {
55
+				// TODO Auto-generated catch block
56
+				e.printStackTrace();
57
+			}
58
+			Socket link=null;
59
+			try {
60
+				link = servSocket.accept();
61
+			} catch (IOException e) {
62
+				// TODO Auto-generated catch block
63
+				e.printStackTrace();
64
+			}
65
+			try {
66
+				BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream()));
67
+			} catch (IOException e) {
68
+				// TODO Auto-generated catch block
69
+				e.printStackTrace();
70
+			}
71
+			PrintWriter out=null;
72
+			try {
73
+				out = new PrintWriter(link.getOutputStream(),true);
74
+			} catch (IOException e) {
75
+				// TODO Auto-generated catch block
76
+				e.printStackTrace();
77
+			}
78
+			out.println("awaiting data...");
79
+			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
80
+			Date date = new Date();
81
+			
82
+			out.println(dateFormat.format(date));
83
+			out.println("end");
84
+			String input;
85
+			/*while (!(input=in.readLine()).equals("end")) {
86
+				System.out.print("server_recoit:"+input);
87
+			}*/
88
+			try {
89
+				link.close();
90
+			} catch (IOException e) {
91
+				// TODO Auto-generated catch block
92
+				e.printStackTrace();
93
+			}
94
+			try {
95
+				servSocket.close();
96
+			} catch (IOException e) {
97
+				// TODO Auto-generated catch block
98
+				e.printStackTrace();
99
+			}	
100
+			
40 101
 		}	
41 102
 		
42 103
 	}
@@ -55,4 +116,5 @@ public class UserListeningThreadTCP extends Thread{
55 116
 			e.printStackTrace();
56 117
 		}
57 118
 	}
119
+	
58 120
 }

Loading…
Cancel
Save