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

📄 chatclient.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
/* 聊天程序之客户端ChatClient.java
 * 建立客户端以获取服务器端发送信息并显示
*/

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ChatClient extends JFrame {
   private JTextField inputBox;
   private JTextArea outFrame;
   private ObjectOutputStream outputS;
   private ObjectInputStream inputS;
   private String message = "";
   private String chatServer;
   private Socket toclient;

   // 初始化聊天服务
   public ChatClient( String srvhost )
   {
      super( "Client" );

      chatServer = srvhost;
      // 设置客户端连接的服务器
      Container container = getContentPane();

      inputBox = new JTextField();
      // 建立输入框
      inputBox.setEnabled( false );
      inputBox.addActionListener(
      //监听
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
            {
               sendMsg( event.getActionCommand() );
            }
            // 向服务器端发送消息
         }
      );
      container.add( inputBox, BorderLayout.NORTH );

      outFrame = new JTextArea();
      container.add( new JScrollPane( outFrame ),
         BorderLayout.CENTER );

      setSize( 280, 160 );
      setVisible( true );
      // 输出输出框
   }

   // 连接服务器端
   public void connectClient()
   {
      try {
         // 创建用于连接的Socket
         connect2Server();
         // 得到输入输出流
         getStreams();
         // 建立连接
         processConnection();
         // 关闭连接
         closeConnection();
      }
      catch ( EOFException eofException ) {
         System.out.println( "Server terminated connection" );
      }
      catch ( IOException ioException ) {
         ioException.printStackTrace();
      }
      //捕获异常
   }

   // 获取输入输出流
   private void getStreams() throws IOException
   {
      // 输出
      outputS = new ObjectOutputStream(
         toclient.getOutputStream() );

      outputS.flush();

      // 输入
      inputS = new ObjectInputStream(
         toclient.getInputStream() );

      outFrame.append( "\nGet I/O streams\n" );
   }

   // 连接服务器端
   private void connect2Server() throws IOException
   {
      outFrame.setText( "连接中……\n" );
      toclient = new Socket(
         InetAddress.getByName( chatServer ), 4000 );
      // 连接信息显示
      outFrame.append( "连接至: " +
         toclient.getInetAddress().getHostName() );
   }

   private void processConnection() throws IOException
   {
      // 输出框
      inputBox.setEnabled( true );
      do {
         // 读入信息并输出
         try {
            message = ( String ) inputS.readObject();
            outFrame.append( "\n" + message );
            outFrame.setCaretPosition(
               outFrame.getText().length() );
         }
         catch ( ClassNotFoundException classNotFoundException ) {
            outFrame.append( "\nUnknown object type received" );
         }
      } while ( !message.equals( "服务器端>> TERMINATE" ) );
   }
//关闭输入输出流、关闭连接,注意顺序
   private void closeConnection() throws IOException
   {
      outFrame.append( "\n关闭连接" );
      outputS.close();
      inputS.close();
      toclient.close();
   }

   // 给服务器端发消息
   private void sendMsg( String message )
   {
      try {
         outputS.writeObject( "客户端>> " + message );
         outputS.flush();
         outFrame.append( "\n客户端>>" + message );
      }
      catch ( IOException ioException ) {
         outFrame.append( "\nError writing object" );
      }
   }

   //main()方法
   public static void main( String args[] )
   {
      ChatClient beginning;

      if ( args.length == 0 )
         beginning = new ChatClient( "127.0.0.1" );
      else
         beginning = new ChatClient( args[ 0 ] );

      beginning.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE );

      beginning.connectClient();
   }

}  // end class ChatClient

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -