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

📄 chatserver.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
/* 聊天程序之服务器端ChatServer.java
 * 建立服务器端,接收客户端连接,
 * 发送信息、关闭连接
*/


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

public class ChatServer extends JFrame {
   private JTextField inputBox;
   private JTextArea outFrame;
   private ObjectOutputStream outputS;
   private ObjectInputStream inputS;
   private ServerSocket toserver;
   private Socket connection;
   private int counter = 1;

   public ChatServer()
   {
      super( "Server" );

      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 connectServer()
   {
       try {
         // 创建一个ServerSocket.
         toserver = new ServerSocket( 4000, 100 );
         while ( true ) {
            // 等待连接
            wait4Connection();
            // 获取输入输出流
            getStreams();
            // 处理连接
            processConnection();
            // 关闭连接
            closeConnection();
            ++counter;
         }
      }

      catch ( EOFException eofException ) {
         System.out.println( "Client terminated connection" );
      }
      //捕获异常
      catch ( IOException ioException ) {
         ioException.printStackTrace();
      }
   }

   private void wait4Connection() throws IOException
   {
      outFrame.setText( "等待连接……\n" );

      connection = toserver.accept();

      outFrame.append( "Connection " + counter +
         "from: " +
         connection.getInetAddress().getHostName()
    );
   }

   private void getStreams() throws IOException
   {
      // 设置输出流
      outputS = new ObjectOutputStream(
         connection.getOutputStream()
      );
      outputS.flush();
      // 设置输入流
      inputS = new ObjectInputStream(
         connection.getInputStream()
      );
      outFrame.append( "\nGet I/O streams\n" );
   }

   //处理客户端连接
   private void processConnection() throws IOException
   {
      // 连接成功
      String message = "服务器端>> 连接成功";
      outputS.writeObject( message );
      outputS.flush();

      // 输入框
      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" ) );
   }

   // 关闭socket
   private void closeConnection() throws IOException
   {
      outFrame.append( "\nUser terminated connection" );
      inputBox.setEnabled( false );
      outputS.close();
      inputS.close();
      connection.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[] )
   {
      ChatServer process = new ChatServer();

      process.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE );

      process.connectServer();
   }

}  // end class ChatServer

⌨️ 快捷键说明

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