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

📄 chatclient.java

📁 利用Socket编写的java聊天程序
💻 JAVA
字号:
/* 
 * version 0.2
 * 添加两个控件
 */

/* 
 * version 0.3
 * 添加TextField的监听器
 */

/*
 * version 0.4
 * 添加连接Server端的功能
 */

/*
 * version 0.5
 * 发送一句话到服务器
 * Socket s 由局部变量改为成员变量
 */

/*
 * version 0.6
 * 向服务器发送多句话
 * 将DataOutputSream由局部变量改为成员变量
 * 添加disConnect函数,用于关闭socket
 */

/*
 * version 1.0
 * client端添加接收各个client信息的功能
 * 开一个ClientReceive线程进行接收 
 */

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

public class ChatClient{
	public static void main(String[] args){
		new ChatClient().start();
	}
	
	private void start(){
		ClientFrame cf = new ClientFrame("Client");
	}
	
    //Client窗口类
	private class ClientFrame extends Frame{
		
		TextField tf = null;
		TextArea ta = null;
		Socket socket = null;
		DataOutputStream dos = null;
		DataInputStream dis = null;
		boolean isReceived = false;
		
		public ClientFrame(String s){
			super(s);
			
			setBounds(300,300,400,300);
			tf = new TextField();
			ta = new TextArea();
			
			//添加控件
			add(tf,BorderLayout.SOUTH);
			add(ta,BorderLayout.NORTH);
			pack();

			//添加关闭功能
			addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e) {
					//释放资源
					disConnected();
					System.exit(0);
				}
				
			});
			
			//为TextField添加监听器
			tf.addActionListener(new TFListener());
			
			//与Server建立连接
			connect2Server();
			
			//接收各个client的数据
			ClientReceive cr = new ClientReceive();
			new Thread(cr).start();
			
			//显示窗体
			setVisible(true);
		}
		
		//连接服务器端
		private void connect2Server(){
			try {
				socket = new Socket("127.0.0.1",8888);
				
				//添加通信管道
				dos = new DataOutputStream(socket.getOutputStream());
				dis = new DataInputStream(socket.getInputStream());
				isReceived = true;
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		//接收其它Client的信息
		private void receive(){

			try{
				while(isReceived){
					String str = dis.readUTF();
					ta.setText(ta.getText() + str +'\n');
				}
			} catch(SocketException e){
				System.out.println("一个客户端程序退出,bye");
			} catch(IOException e){
				e.printStackTrace();
			} finally{
				try {
					if(dis!=null)
						dis.close();
					if(socket!=null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		
		}
		
		//断开服务器,释放资源
		private void disConnected(){
			try {
				if(socket!=null)
					socket.close();
				if(dis!=null)
					dis.close();
				if(dos!=null)
					dos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		//添加TextField的监听器类
		private class TFListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				
				//获取TextField的内容
				String str = tf.getText();
				//设置TextArea的内容
				//ta.setText(str);
				tf.setText("");
	
				//向Server发送信息
				try {
					dos.writeUTF(str);
					dos.flush();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
			}
		}

//		client的接收线程,接收其它client的信息
		private class ClientReceive implements Runnable{
			public void run(){
				receive();
			}
		}
	}
	
	
}

⌨️ 快捷键说明

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