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

📄 testserver.java

📁 Chat案例 SWING多人聊天系统
💻 JAVA
字号:
//SWING界面
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestServer 
{
	ServerReadThread rt;
	JTextField tf;				
	JTextArea ta = new JTextArea();
	public static int Num = 0;
	DataInputStream dis[] = new DataInputStream[100];
	DataOutputStream dos[] = new DataOutputStream[100];
	Socket soc[] = new Socket[100];
    public TestServer()//构造方法,设置界面与监听器
    {
    	JFrame f = new JFrame("Server");
    	JScrollPane jsp = new JScrollPane(ta);
    	ta.setEditable(false);
    	
    	f.getContentPane().add(jsp);
    	
    	f.setSize(300,250);
    	
    	f.addWindowListener(new WindowAdapter()
    	{
    		public void windowClosing(WindowEvent e)
    		{
    			System.exit(1);
            }    			
    	});
    	f.setLocation(300,300);
    	f.setVisible(true);
    	System.out.println("服务器正常工作中.....");

    }    
	public void connect()//搭建起输入输出流的连接
    {
    	try{    		
    		ServerSocket ss = new ServerSocket(2006);      		 
    		while(true){
	    	    soc[Num] = ss.accept();		     	    
	    	    dis[Num] = new DataInputStream(soc[Num].getInputStream());//DataInputStream dis;要处理字符串
	    	    dos[Num] = new DataOutputStream(soc[Num].getOutputStream());//DataOutputStream dos; 
	    	    
	    	    rt = new ServerReadThread(Num,this.ta,this.dis[Num],this.dos);//外部定义了ReadThread类,调用该方法的对象是TestServer sv
    			rt.start(); 
    			Num ++;			
    		}//while		

        }
        catch(IOException e)
        {
        	System.out.println("连接服务器故障!");	
        }  		   	    		
    }
 
   	public static void main(String[] args){
		TestServer sv = new TestServer();
		sv.connect();//自定义的方法
    }	    	   			
}	

class ServerReadThread extends Thread
{
	JTextArea ta;
	DataInputStream dis;
	DataOutputStream dos[];
	String newwords ;
	static int bad[] = new int[100];//保存坏掉连接序号的数组
	static int badIndex = 0;//坏掉连接的下标
	int index;//保存当前线程的编号
	public ServerReadThread(int index,JTextArea t,DataInputStream d,DataOutputStream[] dos)//传入三个对象类型的参数
	{
		this.index = index;
		this.ta = t;
		this.dis = d;
		this.dos = dos;
    }    
    public void run()//读入的任何一句话都必须写出到所有客户端
    {    	
    		try
    		{
    			while(true)
    			{    				
    				newwords = dis.readUTF();
    				ta.append("用户" + newwords );//向服务器的聊天记录追加
	    			ta.append("\n");
	    			ta.setCaretPosition(ta.getText().length());//文本区滚动到最下方+++++++++++++++++
	    	outer:	for(int n = 0;n < TestServer.Num; n ++){
	    				for(int i = 0;i < badIndex;i ++){//遍历坏掉的连接,如果找到匹配,则跳过不再写出
	    					if(n == bad[i])
	    						continue outer;
	    				}
	    				dos[n].writeUTF(newwords);//把读到的东西依次写出去
	    			}
	    		}  
    	    }
    	    catch(IOException e)
            {
    			bad[badIndex ++] = index;//如果出现异常,则添加到错误连接的数组中
				System.out.println("与某客户端连接中断!服务器继续运行");

            }  	           		
    }    	  
}


⌨️ 快捷键说明

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