-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
80 lines (71 loc) · 3.04 KB
/
Copy pathClient.java
File metadata and controls
80 lines (71 loc) · 3.04 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
private static final String SERVER_IP = "127.0.0.1";
private static final int PORT = 5000;
private static final Logger logger = new Logger("Client");
private static final BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
private static Socket socket;
private static BufferedReader in;
private static PrintWriter out;
public static void main(String[] args)
throws Exception{
try {
socket = new Socket(SERVER_IP, PORT);
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("connection check");
System.out.println("Waiting...");
Thread waitThread = new Thread(new Runnable() {
private Logger logger = new Logger("Client Manager");
private boolean flag = false;
@Override
public void run() {
try {
long start = System.currentTimeMillis();
while(true) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedException();
long end = System.currentTimeMillis();
if ((end - start) > 10000l && (end - start) < 11000l && !flag) {
flag = true;
logger.log("The server is a one-to-one server and it seems that a client is already connected. You can try again later.");
}
}
} catch (InterruptedException ex) {
if (flag) {
logger.log("It seems the connection has established...");
}
}
}
});
waitThread.start();
System.out.println(in.readLine());
waitThread.interrupt();
logger.log("Connected to server");
logger.log("Type in a command to get response");
logger.log("Type 'stop' to quit");
while (true) {
System.out.print(">");
String cmd = keyboard.readLine();
out.println(cmd);
String reply = in.readLine();
if (reply.equals("ok bye!!!")) {
System.out.println(reply);
break;
}
System.out.println(reply);
System.out.println("");
}
} catch (IOException e) {
logger.log(e);
} finally {
socket.close();
logger.log("Disconnected");
}
}
}