-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientRead.java
More file actions
38 lines (34 loc) · 1.14 KB
/
ClientRead.java
File metadata and controls
38 lines (34 loc) · 1.14 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientRead extends Thread {
private Socket server;
// Constructor to connect to server using IP and port
public ClientRead(String address, int port) {
try {
server = new Socket(address, port);
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("Could not connect to server, please check server details.");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Server socket could not be created, the port may be reserved");
}
}
public void run() {
try {
// Buffered reader to read input from the server
BufferedReader serverIn = new BufferedReader(new InputStreamReader(server.getInputStream()));
while (true) {
// reads input from server and outputs it line by line
String serverRes = serverIn.readLine();
System.out.println(serverRes);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error while receiving message from server");
}
}
}