Skip to content

Commit 687e312

Browse files
author
Sandy W
committed
moved socket close to finally block, removed sleep()
1 parent 4013110 commit 687e312

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

client/ClientThread.java

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,44 @@ public class ClientThread extends Thread {
2020

2121
public void run() {
2222
PrintWriter out = null;
23-
BufferedReader in = null;
24-
try
25-
{
26-
//creates a new Socket object and names it socket.
27-
//Establishes the socket connection between the client & server
28-
//name of the machine & the port number to which we want to connect
29-
socket = new Socket(hostName, 4000);
30-
System.out.print("Establishing connection.");
31-
out = new PrintWriter(socket.getOutputStream(), true);//opens a PrintWriter
32-
//on the socket
23+
BufferedReader in = null;
24+
try {
25+
//creates a new Socket object and names it socket.
26+
//Establishes the socket connection between the client & server
27+
//name of the machine & the port number to which we want to connect
28+
socket = new Socket(hostName, 4000);
29+
System.out.print("Establishing connection.");
30+
out = new PrintWriter(socket.getOutputStream(), true);//opens a PrintWriter
31+
//on the socket
3332

34-
//opens a BufferedReader on the socket
35-
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
36-
System.out.println("\nRequesting output for the '" + menuSelection + "' command from " + hostName);
37-
// send the command to the server
38-
for (int i = 0; i < 25; i++) {
39-
out.println(Integer.toString(menuSelection));
40-
out.flush();
41-
}
42-
System.out.println("Sent output");
43-
// read the command from the server
44-
String outputString = "hi";
45-
while (((outputString = in.readLine()) != null) && (!outputString.equals("END_MESSAGE"))) {
46-
System.out.println(threadNum + " " + outputString);
47-
}
48-
System.out.println("closing");
49-
try {
50-
sleep(500);
51-
} catch (InterruptedException e) {
52-
// TODO Auto-generated catch block
53-
e.printStackTrace();
54-
}
55-
socket.close();
33+
//opens a BufferedReader on the socket
34+
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
35+
System.out.println("\nRequesting output for the '" + menuSelection + "' command from " + hostName);
36+
37+
// send the command to the server
38+
for (int i = 0; i < 25; i++) {
39+
out.println(Integer.toString(menuSelection));
40+
out.flush();
41+
}
42+
System.out.println("Sent output");
5643

57-
}
58-
catch (UnknownHostException e)
59-
{
60-
System.err.println("Unknown host: " + e);
61-
System.exit(1);
62-
}
63-
catch (IOException e)
64-
{
65-
e.printStackTrace();
66-
}
44+
// read the command from the server
45+
String outputString = "";
46+
while (((outputString = in.readLine()) != null) && (!outputString.equals("END_MESSAGE"))) {
47+
System.out.println(threadNum + " " + outputString);
48+
}
49+
}
50+
catch (UnknownHostException e) {
51+
System.err.println("Unknown host: " + e);
52+
System.exit(1);
53+
}
54+
catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
finally {
58+
System.out.println("closing");
59+
socket.close();
60+
}
6761

6862
}
6963

0 commit comments

Comments
 (0)