Browse Source

tcpmessage

LMAGallois 3 years ago
parent
commit
6bef5f9150

+ 2
- 0
Application/Clavardage/bin/.gitignore View File

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

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


+ 9
- 0
Application/Clavardage/src/Database.java View File

@@ -0,0 +1,9 @@
1
+
2
+public class Database {
3
+	private User myUser;
4
+	
5
+	public void Database() {
6
+		
7
+	}
8
+
9
+}

+ 4
- 3
Application/Clavardage/src/User.java View File

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

+ 55
- 0
Application/Clavardage/src/UserConnexionthreadTCP.java View File

@@ -0,0 +1,55 @@
1
+import java.io.BufferedReader;
2
+import java.io.IOException;
3
+import java.io.InputStreamReader;
4
+import java.io.PrintWriter;
5
+import java.net.DatagramSocket;
6
+import java.net.Socket;
7
+import java.text.DateFormat;
8
+import java.text.SimpleDateFormat;
9
+import java.util.Date;
10
+
11
+public class UserConnexionthreadTCP extends Thread{
12
+	private User myUser;
13
+	private Socket socket;
14
+	
15
+	public UserConnexionthreadTCP(String s,User user,Socket socket) { 
16
+		super(s);
17
+		this.myUser = user;
18
+		this.socket=socket;
19
+		
20
+	}
21
+	
22
+	public void run() {
23
+		Socket link = this.socket;
24
+		try {
25
+			BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream()));
26
+		} catch (IOException e) {
27
+			// TODO Auto-generated catch block
28
+			e.printStackTrace();
29
+		}
30
+		PrintWriter out=null;
31
+		try {
32
+			out = new PrintWriter(link.getOutputStream(),true);
33
+		} catch (IOException e) {
34
+			// TODO Auto-generated catch block
35
+			e.printStackTrace();
36
+		}
37
+		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
38
+		Date date = new Date();
39
+		System.out.println(myUser.getPseudo()+" envoie un message");
40
+		out.println(dateFormat.format(date));
41
+		out.println("end");
42
+		String input;
43
+		/*while (!(input=in.readLine()).equals("end")) {
44
+			System.out.print("server_recoit:"+input);
45
+		}*/
46
+		try {
47
+			link.close();
48
+		} catch (IOException e) {
49
+			// TODO Auto-generated catch block
50
+			e.printStackTrace();
51
+		}		
52
+	}
53
+	
54
+
55
+}

+ 12
- 39
Application/Clavardage/src/UserListeningThreadTCP.java View File

@@ -15,7 +15,7 @@ import java.util.Date;
15 15
 public class UserListeningThreadTCP extends Thread{
16 16
 	
17 17
 	private User myUser;
18
-	private DatagramSocket dgramSocket=null;
18
+	//private DatagramSocket dgramSocket=null;
19 19
 	
20 20
 	/* CONSTRUCTOR OF UserListeningThreadTCP
21 21
 	 * $parametres 
@@ -27,13 +27,20 @@ public class UserListeningThreadTCP extends Thread{
27 27
 	public UserListeningThreadTCP(String s,User user) { 
28 28
 		super(s);
29 29
 		this.myUser = user;
30
-		try {
30
+	/*	try {
31 31
 			this.dgramSocket = new DatagramSocket(this.myUser.getPortTCP(),this.myUser.getAddIP());
32 32
 		} catch (IOException e) {
33 33
 			e.printStackTrace();
34 34
 		}
35 35
 		
36
+		*/
37
+		
38
+	}
39
+	
40
+	public void accept(ServerSocket servSocket) throws IOException {
36 41
 		
42
+		UserConnexionthreadTCP threadtcp= new UserConnexionthreadTCP("Chat_with_"+myUser.getPseudo(),myUser,servSocket.accept());
43
+		threadtcp.start();
37 44
 		
38 45
 	}
39 46
 	
@@ -55,48 +62,14 @@ public class UserListeningThreadTCP extends Thread{
55 62
 				// TODO Auto-generated catch block
56 63
 				e.printStackTrace();
57 64
 			}
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 65
 			
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 66
 			try {
89
-				link.close();
67
+				this.accept(servSocket);
90 68
 			} catch (IOException e) {
91 69
 				// TODO Auto-generated catch block
92 70
 				e.printStackTrace();
93 71
 			}
94
-			try {
95
-				servSocket.close();
96
-			} catch (IOException e) {
97
-				// TODO Auto-generated catch block
98
-				e.printStackTrace();
99
-			}	
72
+				
100 73
 			
101 74
 		}	
102 75
 		
@@ -108,7 +81,7 @@ public class UserListeningThreadTCP extends Thread{
108 81
 	 * 		interrupt UDP listening threadS
109 82
 	 */
110 83
 	public void close() {
111
-		this.dgramSocket.close();
84
+	//	this.dgramSocket.close();
112 85
 		System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")");
113 86
 		try {
114 87
 			this.interrupt();

Loading…
Cancel
Save