-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientConnection.java
70 lines (60 loc) · 1.99 KB
/
ClientConnection.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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
This class represents a connection between a client and a server.
It implements the Runnable interface to allow for it to be run on a separate thread.
*/
public class ClientConnection implements Runnable{
private Socket socket;
private BufferedReader input;
private PrintWriter output;
private boolean done;
/**
Constructor for a new ClientConnection object.
@param socket The socket representing the connection between the client and server.
*/
public ClientConnection(Socket socket) {
this.socket = socket;
done = false;
}
/**
* This method is called when a new thread is started for this object.
* It initializes the input and output streams and listens for incoming messages from the server.
* When a message is received, it is printed to the console.
*/
@Override
public void run() {
try {
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream(), true);
String serverMessage;
while (!done && (serverMessage = input.readLine()) != null) {
System.out.println(serverMessage);
}
} catch (Exception e) {
disconnect();
}
}
/**
This method sends a message to the server through the output stream.
@param message The message to be sent.
*/
public void sendMessage(String message) {
output.println(message);
}
/**
This method disconnects the client from the server by closing the input and output streams and the socket.
*/
public void disconnect() {
done = true;
try {
input.close();
output.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}