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

📄 chatserver.java

📁 JAVA开发者入门教程
💻 JAVA
字号:
/*
 * @author: 先风剑客
 * @version: chat0.8
 * 实现各按钮事件响应
 * 主要是对不同按钮作出反应
 * 实现上一版本未完成的功能
 * 在对发送后,显示本机发送的内容
 * 实现了网络连接 
 * 对网络上接收的数据进行简单处理
 * 但只能处理一次
 * 添加时间功能
 * 显示信息定位到显示面板
 * 可以接收单个主机发来的多条消息
 * 各类异常的人性化处理
 * 接收多个客户端
 * 虽然只实现一点功能,但总体格局发生巨大改变
 * 
 * 对接收到的数据,分别再发送给其它的客户端
 */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ChatServer {
	public static void main(String[] args){
		new ChatServerListener();

	}
}

class ChatServerListener extends Frame{
	ServerSocket ss = null; 
	Socket s = null;
	
	TextArea ta = null;
	TextField tf = null;
	Date dt = null;	
	
	boolean started = false;
	List<Client> clients = new ArrayList<Client>();

	ChatServerListener(){
		super("ChatServer");
		ta = new TextArea();
		tf = new TextField();
		ChatServerMonitor cs = new ChatServerMonitor();
		Dimension screen = getToolkit().getScreenSize();
		
		addWindowListener(cs);
		setBackground(Color.LIGHT_GRAY);
		add(ta,BorderLayout.NORTH);
		add(tf,BorderLayout.SOUTH);
		pack();
		setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) /2);
		setResizable(false); 
		setVisible(true);
		
		Server();//启动服务器
	}
	
	public void Server(){//服务器启动
		try{
			ta.setText("服务器正在启动并进行初始化.....");
			ss = new ServerSocket(5678);
			ta.setText(ta.getText()+"\n"+"服务器已经启动......."+"\n"+"静候客户端连接.......");
			started = true;
			
			while(started){//启动连接线程
				s = ss.accept();
				Client c = new Client(s);
				new Thread(c).start();
				clients.add(c);
			}
		}catch(BindException e1){
			started = false;
			System.out.println("端口无法使用.......");
			System.exit(-1);
		}catch(IOException e2){
			started = false;
			System.out.println("连接失败!"+"\n"+e2.getMessage());
			System.exit(-1);
		}
	}
	
	class Client implements Runnable{//处理客户端
		private Socket s;
		private DataInputStream dis = null;
		private DataOutputStream dos = null;
		private boolean bconnect = false;
		
		Client(Socket s){
			this.s = s;
			try{
				dt = new Date();
				ta.setText(ta.getText()+"\n"+"有客户机连接上....."+"\n"+"获取客户机信息......"+"\n"+"连接客户机IP&Port: "+s.getRemoteSocketAddress()+"\n"+"连接时间: "+dt.getMonth()+"月 "+dt.getDay()+"日 "+dt.getHours()+"时 "+dt.getMinutes()+"分 "+dt.getSeconds()+"秒"+"\n");
				dis = new DataInputStream(s.getInputStream());
				dos = new DataOutputStream(s.getOutputStream());
				bconnect = true;
				
			}catch(EOFException e){
				bconnect = false;
				clients.remove(this);
				ta.setText(ta.getText()+"\n"+"客户端连接已经中断......");
			}catch(IOException e){
				bconnect = false;
				clients.remove(this);
				ta.setText(ta.getText()+"\n"+"网络连接中断......");
			}
		}
		
		public void send(String str){
				try {
					dos.writeUTF(str);
				} catch (IOException e) {
					clients.remove(this);
					ta.setText(ta.getText()+"\n"+"客户端断开连接.....");
				}
		}

		public void run() {
			try{
				while(bconnect){
					String str = dis.readUTF();
					ta.setText(ta.getText()+"\n"+"主机:"+s.getRemoteSocketAddress()+"  有消息送达:"+"\n"+"  "+str+"\n"+"发送时间:"+(new Date()).getMonth()+"月 "+(new Date()).getDay()+"日 "+(new Date()).getHours()+"时 "+(new Date()).getMinutes()+"分 "+(new Date()).getSeconds()+"秒"+"\n");
					for(int i=0; i<clients.size(); i++){//调用向其它客户端发送消息的方法
						Client c = clients.get(i);
						c.send(str);
					}		
				}
			}catch(EOFException e){
				bconnect = false;
				//ta.setText(ta.getText()+"\n"+"客户端连接已经中断......");
			}catch(IOException e){
				bconnect = false;
				ta.setText(ta.getText()+"\n"+"网络连接中断......");
			}finally{
				try {
					if(dis != null){
						dis.close();
						dis = null;
					}
					if(dos != null){
						dos.close();
						dos = null;
					}
					if(s != null){
						ta.setText(ta.getText()+"\n"+s.getRemoteSocketAddress()+"准备退出连接");
						s.close();
						clients.remove(this);
						ta.setText(ta.getText()+"\n"+"客户端已经安全退出连接.....");
						s = null;
					}
				} catch (IOException e1) {
					clients.remove(this);
					ta.setText(ta.getText()+"\n"+"客户端停止通话.....");
				}
			}
			
		}
	}
	
	private class ChatServerMonitor extends WindowAdapter{
		public void windowClosing(WindowEvent e) {
	  		setVisible(false);
	  		System.exit(0);
	  	}
	}
}

⌨️ 快捷键说明

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