Repository navigation
Expand file tree
/
Copy pathChatClient.java
More file actions
107 lines (88 loc) · 3.25 KB
/
Copy pathChatClient.java
File metadata and controls
107 lines (88 loc) · 3.25 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
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
/******************************************************************************
* Compilation: javac ChatClient.java
* Execution: java ChatClient name host
* Dependencies: In.java Out.java
*
* Connects to host server on port 4444, enables an interactive
* chat client.
*
* % java ChatClient alice localhost
*
* % java ChatClient bob localhost
*
******************************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.net.Socket;
public class ChatClient extends JFrame implements ActionListener {
private String screenName;
// GUI stuff
private JTextArea enteredText = new JTextArea(10, 32);
private JTextField typedText = new JTextField(32);
// socket for connection to chat server
private Socket socket;
// for writing to and reading from the server
private Out out;
private In in;
public ChatClient(String screenName, String hostName) {
// connect to server
try {
socket = new Socket(hostName, 4444);
out = new Out(socket);
in = new In(socket);
}
catch (Exception ex) { ex.printStackTrace(); }
this.screenName = screenName;
// close output stream - this will cause listen() to stop and exit
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
out.close();
// in.close();
// try { socket.close(); }
// catch (Exception ioe) { ioe.printStackTrace(); }
}
}
);
// create GUI stuff
enteredText.setEditable(false);
enteredText.setBackground(Color.LIGHT_GRAY);
typedText.addActionListener(this);
Container content = getContentPane();
content.add(new JScrollPane(enteredText), BorderLayout.CENTER);
content.add(typedText, BorderLayout.SOUTH);
// display the window, with focus on typing box
setTitle("Chat Client 1.0: [" + screenName + "]");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
typedText.requestFocusInWindow();
setVisible(true);
}
// process TextField after user hits Enter
public void actionPerformed(ActionEvent e) {
out.println("[" + screenName + "]: " + typedText.getText());
typedText.setText("");
typedText.requestFocusInWindow();
}
// listen to socket and print everything that server broadcasts
public void listen() {
String s;
while ((s = in.readLine()) != null) {
enteredText.insert(s + "\n", enteredText.getText().length());
enteredText.setCaretPosition(enteredText.getText().length());
}
out.close();
in.close();
try { socket.close(); }
catch (Exception e) { e.printStackTrace(); }
System.err.println("Closed client socket");
}
public static void main(String[] args) {
ChatClient client = new ChatClient(args[0], args[1]);
client.listen();
}
}