📄 chatclient.java
字号:
// ChatClient.java
// Andrew Davison, June 2003, dandrew@ratree.psu.ac.th
/* The ChatClient interacts with the ChatServer.
It can send the following messages:
who -- a list of users is returned, with the format
"WHO$$ cliAddr1 & port1 & ... cliAddrN & portN & "
bye -- client is disconnecting
any text -- which is broadcast to all clients with the format
"(cliAddr,port): msg".
The address and port uniquely identify the client.
The first & third messages are sent via a GUI interface:
* a "Who" button
* a "Message:" text field
Clicking the close box of the window, causes the client to
send the "bye" message before terminating.
There is a separate threaded ChatWatcher object for processing
messages coming from the ChatServer object.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends JFrame implements ActionListener
{
private static final int PORT = 1234; // server details
private static final String HOST = "localhost";
private Socket sock;
private PrintWriter out; // output to the server
private JTextArea jtaMesgs; // output text area
private JTextField jtfMsg; // for sending messages
private JButton jbWho; // for sending a "who" message
public ChatClient()
{
super("Chat Client");
initializeGUI();
makeContact();
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ closeLink(); }
});
setSize(300,450);
show();
} // end of ChatClient();
private void initializeGUI()
/* Text area in center, and controls below.
Controls:
- textfield for entering messages
- a "Who" button
*/
{
Container c = getContentPane();
c.setLayout( new BorderLayout() );
jtaMesgs = new JTextArea(7, 7);
jtaMesgs.setEditable(false);
JScrollPane jsp = new JScrollPane( jtaMesgs);
c.add( jsp, "Center");
JLabel jlMsg = new JLabel("Message: ");
jtfMsg = new JTextField(15);
jtfMsg.addActionListener(this); // pressing enter triggers sending of name/score
jbWho = new JButton("Who");
jbWho.addActionListener(this);
JPanel p1 = new JPanel( new FlowLayout() );
p1.add(jlMsg); p1.add(jtfMsg);
JPanel p2 = new JPanel( new FlowLayout() );
p2.add(jbWho);
JPanel p = new JPanel();
p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(p1); p.add(p2);
c.add(p, "South");
} // end of initializeGUI()
private void closeLink()
{
try {
out.println("bye"); // tell server that client is disconnecting
sock.close();
}
catch(Exception e)
{ System.out.println( e ); }
System.exit( 0 );
} // end of closeLink()
private void makeContact()
// contact the server and start a ChatWatcher thread
{
try {
sock = new Socket(HOST, PORT);
BufferedReader in = new BufferedReader(
new InputStreamReader( sock.getInputStream() ) );
out = new PrintWriter( sock.getOutputStream(), true ); // autoflush
new ChatWatcher(this, in).start(); // start watching for server msgs
}
catch(Exception e)
{ System.out.println(e); }
} // end of makeContact()
public void actionPerformed(ActionEvent e)
/* Either a message is to be sent or the "Who"
button has been pressed. */
{
if (e.getSource() == jbWho)
out.println("who"); // the response is read by ChatWatcher
else if (e.getSource() == jtfMsg)
sendMessage();
}
private void sendMessage()
/* Check if the user has supplied a message, then
send it to the server. */
{
String msg = jtfMsg.getText().trim();
// System.out.println("'"+msg+"'");
if (msg.equals(""))
JOptionPane.showMessageDialog( null,
"No message entered", "Send Message Error",
JOptionPane.ERROR_MESSAGE);
else {
out.println(msg);
// showMsg("Sent: " + msg + "\n");
}
} // end of sendMessage()
synchronized public void showMsg(String msg)
/* Synchronized since this method can be called by this
object or the ChatWatcher thread. */
{ jtaMesgs.append(msg); }
// ------------------------------------
public static void main(String args[])
{ new ChatClient(); }
} // end of ChatClient class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -