-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyClient.java
142 lines (121 loc) · 3.81 KB
/
myClient.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
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class myClient {
private static int port = 3339;
private static String host = "localhost";
public static List<String> userInput = Collections.synchronizedList(new ArrayList<String>());
public static void main (String[] args) throws IOException {
Socket serverSock = null;
//try to connect to the server specified on the port specified
try {
serverSock = new Socket(host, port);
} catch (UnknownHostException hostnotfound) {
System.err.println("Failed to find host");
System.err.println(hostnotfound);
System.exit(1);
} catch (IOException IOerr) {
System.err.println("I/O error when creating socket");
System.err.println(IOerr);
System.exit(1);
}
final Socket finalSock = serverSock;
//thread for stdIn to be run by clients that have established a server connection
Thread stdInThread = new Thread(new Runnable() {
private BufferedReader stdIn = null;
public void run() {
stdIn = new BufferedReader(new InputStreamReader(System.in));
//to hold user input
String usermsg = null;
while(true) {
try {
//read from stdin, add to list when not null input
if((usermsg = stdIn.readLine())!=null) {
synchronized (userInput) {
userInput.add(usermsg);
}
}
}
catch (IOException stdInerr) {
System.err.println("Unable to read user input");
System.err.println(stdInerr);
System.exit(1);
}
}
}
});
//client thread for reading/writing to server
Thread clientThread = new Thread(new Runnable() {
private BufferedReader fromServer = null;
private PrintWriter toServer = null;
private Socket clientSock = finalSock;
@Override
public void run() {
//set up the communication streams with the server
try {
fromServer = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
toServer = new PrintWriter(clientSock.getOutputStream(), true);
System.out.println("successfully made server streams");
} catch (IOException streamerr) {
System.err.println("error creating streams to/from server");
System.err.println(streamerr);
System.exit(1);
}
//to hold server input
String servermsg = null;
int clock = 0;
while (true) {
//clock gives reading/writing to socket equal time slices
if (clock < 250) {
//write to the socket whilst the list has stuff to write
synchronized(userInput) {
while((userInput.isEmpty())==false) {
toServer.println(userInput.remove(0));
}
}
}
else {
//read from the socket, while the input isn't null print the message
try {
if(fromServer.ready() == true) {
servermsg = fromServer.readLine();
System.out.println(servermsg);
}
} catch (IOException outputerr) {
System.err.println("Unable to read server input");
System.err.println(outputerr);
System.exit(1);
}
}
//increment the clock up to 500 then reset it
if (clock < 500) {
clock++;
} else {
clock = 0;
}
}
}
});
stdInThread.start();
clientThread.start();
//thread for handling ctrl+c
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
finalSock.close();
} catch (IOException e) {
System.err.println("Failed to close socket");
System.err.println(e);
System.exit(1);
}
System.out.println("Exiting client, shutting ports");
}
});
}
}