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

📄 client.java

📁 模拟最简易的聊天窗口界面
💻 JAVA
字号:
package com.biluo.web.commit;


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 host;
		private Socket client;
		
		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();
		}
		
		// 初始化host地址(参数为空时默认地址为本地环回地址,也可以创建对象时指定主机)和初始化几个swing组件
		public Client( String host )
		{
		   super( "Client" );
		   this.host = host;
		   Container container = getContentPane();
		 //初始化输入框,并注册监听器
		   enterField = new JTextField();
		 //  enterField.setEnabled( false );
		   enterField.addActionListener(
		      new ActionListener() {		         
		         public void actionPerformed( ActionEvent event )
		         {
		            sendData( event.getActionCommand() );
		         }
		      }  
		   ); 
		   container.add( enterField, BorderLayout.SOUTH );
		   //创建显示区
		   displayArea = new JTextArea();
		   container.add( new JScrollPane( displayArea ),
		      BorderLayout.CENTER );
		   setSize( 500,350  );
		   this.setLocation(550, 0);
		   setVisible( true );
		}
		
		//运行客户端
		public void runClient() 
		{
			//分为四步:连接服务器,得到数据流,中间处理过程,关闭接口
		   try {			      
		      connectToServer();
		      getStreams();
		      processConnection();
		      closeConnection();
		   }
		   catch ( EOFException eofException ) {
		      System.out.println( "与服务器断开连接" );
		   }	
		   catch ( IOException ioException ) {
		      ioException.printStackTrace();
		   }
		}		
		// 发送接受数据
		private void getStreams() throws IOException
		{
		   output = new ObjectOutputStream( client.getOutputStream() );		
		   output.flush();	
		   input = new ObjectInputStream( client.getInputStream() );		
		   displayArea.append( "\n已经得到IO流啦~~~~~~\n" );
		}		
		// 连接到服务器
		private void connectToServer() throws IOException
		{      
		   displayArea.setText( "正在试图连接\n" );	
		   //创建通信套接字(客户端只需要一个套接字)
		   client = new Socket( InetAddress.getByName( host ), 35535 );	
		   displayArea.append( "已链接到" + client.getInetAddress().getHostName() );
		}		
		// 中间处理操作
		private void processConnection() throws IOException
		{
		   enterField.setEnabled( true );
		 //处理客户端发来的数据,读出并显示信息(如果客户端不发送断开连接的请求,始终处理发来的信息)握手协议&延迟断开
		   do {
		      try {
		         message = ( String ) input.readObject();
		         displayArea.append( "\n" + message );
		         displayArea.setCaretPosition(
		            displayArea.getText().length() );
		      }catch ( ClassNotFoundException classNotFoundException ) {
		         displayArea.append( "\n对不起:读到未知类型,无法显示" );
		      }		
		   } while ( !message.contains("断开连接")&&!message.contains("end") );
		
		} 		
		//关闭流和套接字
		private void closeConnection() throws IOException
		{
		   displayArea.append( "\n关闭连接" );
		   output.close();
		   input.close();
		   client.close();
		}		
		// 发送数据
		private void sendData( String message )
		{
		   try {
		      output.writeObject( "客户端("+new 	java.util.Date()+"):\n " + message );
		      output.flush();
		      displayArea.append( "\n客户端("+new 	java.util.Date()+"):\n" + message );
		   }
		   catch ( IOException ioException ) {
		      displayArea.append( "\n错误输入类型" );
		   }
		}	
		
} 

⌨️ 快捷键说明

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