⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 这是java 2应用开发指南这本书上所有例子的源代码
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame {
  private JTextField tf;
  private JTextArea display;
  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();
    tf = new JTextField();
    tf.setEnabled( false );
    tf.addActionListener(new ActionListener() {         
      public void actionPerformed( ActionEvent event ){
        sendData( event.getActionCommand() );
      }
    }); // end call to addActionListener
    container.add( tf, BorderLayout.NORTH );
    display = new JTextArea();
    container.add( new JScrollPane( display),BorderLayout.CENTER );
    setSize( 300, 150 );
    setVisible( true );
  }
  public void runClient(){
    try {
      connectToServer();
      getStreams();
      processConnection();
      closeConnection();
    }
    catch ( EOFException eofException ) {
      System.out.println( "Server terminated connection" );
    }
    catch ( IOException ioException ) {
      ioException.printStackTrace();
    }
  }
  private void getStreams() throws IOException{
    output = new ObjectOutputStream(client.getOutputStream() );
    output.flush();
    input = new ObjectInputStream(client.getInputStream() );
    display.append( "\nGot I/O streams\n" );
  }
  private void connectToServer() throws IOException{
    display.setText( "Attempting connection\n" );
    client = new Socket(InetAddress.getByName( chatServer ), 5000 );
    display.append( "Connected to: " +client.getInetAddress().getHostName() );
  }
  private void processConnection() throws IOException{
    // enable enterField so client user can send messages
    tf.setEnabled( true );
    do {
      try {
        message = ( String ) input.readObject();
        display.append( "\n" + message );
        display.setCaretPosition(display.getText().length() );
      }
      catch ( ClassNotFoundException classNotFoundException ) {
        display.append( "\nUnknown object type received" );
      }
    } 
    while ( !message.equals( "SERVER>>> TERMINATE" ) );
  }  // end method process connection
  private void closeConnection() throws IOException{
    display.append( "\nClosing connection" );
    output.close();
    input.close();
    client.close();
  }
  private void sendData( String message ){
    try {
      output.writeObject( "CLIENT>>> " + message );
      output.flush();
      display.append( "\nCLIENT>>>" + message );
    }
    catch ( IOException ioException ) {
      display.append( "\nError writing object" );
    }
  }
  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 + -