📄 client.java
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame
{
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
public Client( String host )
{
super( "Client" );
chatServer = host;
Container container = getContentPane();
enterField = new JTextField();
enterField.setEditable( false );
enterField.addActionListener (
new ActionListener()
{
public void actionPerformed( ActionEvent event ){
sendData( event.getActionCommand() );
enterField.setText( "" );
}
}
);
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ), BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
// connect to server and process messages from server
private void runClient()
{
try
{
connectToServer();
getStreams();
processConnection();
}
catch ( EOFException eofException )
{
System.err.println( "Client terminated connection" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
finally
{
closeConnection();
}
}
// connect to server
private void connectToServer() throws IOException
{
displayMessage( "Attempting connection\n" );
client = new Socket( InetAddress.getByName( chatServer ), 12345 );
displayMessage( "Connected to: " +client.getInetAddress().getHostName() );
}
// get streams to send and receive data
private void getStreams() throws IOException
{
output = new ObjectOutputStream( client.getOutputStream() );
input = new ObjectInputStream( client.getInputStream() );
}
// process connection with server
private void processConnection() throws IOException
{
setTextFieldEditable( true );
do
{
// read message and display it
try
{
message = ( String ) input.readObject();
displayMessage( "\n" + message );
}
catch ( ClassNotFoundException classNotFoundException )
{
displayMessage( "\nUnknown object type received" );
}
}
while ( !message.equals( "SERVER>>> TERMINATE" ) );
}
// close streams and socket
private void closeConnection()
{
displayMessage( "\nClosing connection" );
setTextFieldEditable( false );
try
{
output.close();
input.close();
client.close();
}
catch( IOException ioException )
{
ioException.printStackTrace();
}
}
// send message to server
private void sendData( String message )
{
try
{
output.writeObject( "CLIENT>>> " + message );
output.flush();
displayMessage( "\nCLIENT>>> " + message );
}
catch ( IOException ioException )
{
displayArea.append( "\nError writing object" );
}
}
// utility method called from other threads to manipulate
// displayArea in the event-dispatch thread
private void displayMessage( final String messageToDisplay )
{
SwingUtilities.invokeLater
(
new Runnable()
{
public void run()
{
displayArea.append( messageToDisplay );
displayArea.setCaretPosition( displayArea.getText().length() );
}
}
);
}
private void setTextFieldEditable( final boolean editable )
{
SwingUtilities.invokeLater
(
new Runnable()
{
public void run()
{
enterField.setEditable( editable );
}
}
);
}
public static void main( String args[] )
{
Client application;
if ( args.length == 0 )
application = new Client( "127.0.0.1" );
else
application = new Client( args[ 0 ] );
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.runClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -