-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
42 lines (33 loc) · 1.06 KB
/
ChatClient.java
File metadata and controls
42 lines (33 loc) · 1.06 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
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatClient implements Runnable {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private ChatGUI gui;
public ChatClient(ChatGUI gui) {
this.gui = gui;
}
public void connect(String hostname, int port) throws IOException {
socket = new Socket("localhost",9999);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
new Thread(this).start();
}
public void sendMessage(String message) {
out.println(message);
gui.displayMessage(message);
}
@Override
public void run() {
try {
String message;
while ((message = in.readLine()) != null) {
gui.displayMessage(message);
}
} catch (IOException e) {
gui.displayMessage("Error: " + e.getMessage());
}
}
}