-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.java
101 lines (88 loc) · 2.99 KB
/
User.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
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.ServerSocket;
import java.util.*;
public class User extends Thread {
// The user socket
private static Socket userSocket = null;
// The output stream
private static PrintStream outToServer = null;
// The input stream
private static BufferedReader inFromServer = null;
private static BufferedReader inFromUser = null;
private static boolean closed = false;
private static String SentenceFromServer;
private static String UserName;
public void stopMe() {
closed = true;
}
public static void main(String[] args) throws UnknownHostException, IOException,InterruptedException{
// The default port.
int portNumber = 8000;
// The default host.
String host = "localhost";
if (args.length < 2) {
System.out
.println("Usage: java User <host> <portNumber>\n"
+ "Now using host=" + host + ", portNumber=" + portNumber);
} else {
host = args[0];
portNumber = Integer.valueOf(args[1]).intValue();
}
/*
* Open a socket to a given host and port. Open input and output streams.
*/
userSocket = new Socket(host,portNumber);
System.out.println("Connected to server on" + host +"," + portNumber);
inFromServer = new BufferedReader(new InputStreamReader(userSocket.getInputStream()));
inFromUser = new BufferedReader(new InputStreamReader(System.in));
outToServer = new PrintStream(userSocket.getOutputStream());
/*
* If everything has been initialized then create a listening thread to
* read from the server.
* Also send any user’s message to server until user logs out.
*/
User u = new User();
u.start();
//when get "bye" message from server, close connection
if(SentenceFromServer == "###Bye " + UserName + " ###") {
u.stopMe();
outToServer.close();
inFromServer.close();
userSocket.close();
u.join();
}
String sentence;
while(!closed) {
if(SentenceFromServer == "Enter your name") {
UserName = inFromUser.readLine();
outToServer.println(UserName);
}
sentence = inFromUser.readLine();
outToServer.println(sentence);
}
}
public void run() {
/*
* Keep on reading from the socket till we receive “### Bye …” from the
* server. Once we received that then we want to break and close the connection.
*/
while(!closed) {
try {
synchronized(userThread.class) {
if((SentenceFromServer = inFromServer.readLine()) != null) {
System.out.println(SentenceFromServer);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}