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

📄 firstthread.java

📁 初学java,最近帮同学作的一些实验
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class firstthread extends Thread{

	protected Socket client;
	String line,name;
	protected DataOutputStream firstout,out;
	protected chatseverthree sever;
	protected DataInputStream in;
	public firstthread(chatseverthree sever,Socket client)
	{
		this.sever=sever;
		this.client=client;
		try{
			in=new DataInputStream(client.getInputStream());
			out=new DataOutputStream(client.getOutputStream());
			firstout=new DataOutputStream(client.getOutputStream()); 
		}catch(IOException e){
			try{
				sever.connections.remove(this);
				client.close();
			}catch(IOException e2){
				System.out.println("有问题哦"+e);
				return;
			}
		}
		if(this.client==null)
		{
			sever.broadcast("quit"+this.name);
			this.name=null;
		}
	}
	public void run()
	{
		//System.out.println("thread");
		try{
			for(int i=0;i<sever.connections.size();i++)         //告之已在线成员
			{
				firstthread c=(firstthread)sever.connections.elementAt(i);  
				if(c.name!=null)
				{
					try{
						firstout.writeUTF(c.name+"*");
					}catch(IOException e){}
				}
			}
		}catch(ArrayIndexOutOfBoundsException e){}
		catch(NullPointerException e){}
		try{
			while(true)
			{
				line=in.readUTF();
				if(line.startsWith("people"))
				{
					try{
						firstthread d=(firstthread)(sever.connections.elementAt(sever.connections.indexOf(this)));
						if(d.name==null)
						{  //System.out.println("name2");
							d.name=line;
							
						}else if(d.name!=null)
						{   //System.out.println("name1");
							sever.broadcast("quit"+this.name);
							d.name=line;
						}
					}catch(ArrayIndexOutOfBoundsException e){}
					catch(NullPointerException e){}
					finally{sever.broadcast(line);}    //通知各成员该新成员加入
				}
				else if(line.startsWith("quit"))
				{
					sever.broadcast("quit"+this.name);
					sever.connections.removeElement(this);
					this.in.close();
					this.out.close();
					this.firstout.close();
					this.client.close();
					this.yield();
				}
				else if(line.startsWith("MSG"))
				{
					sever.broadcast(line);
				}
				else if(line.startsWith("悄悄地对"))
				{
					this.out.writeUTF(line);
					sever.broadcast1(line);
				}
				else if (line.startsWith("newlist"))
				{
					try{
						for(int i=0;i<sever.connections.size();i++)
						{
							firstthread c=(firstthread)(sever.connections.elementAt(i));
							if(c.name!=null)
							{
								try{
									firstout.writeUTF(c.name+"*");
								}catch(IOException e){}
							}
						}
					}catch(ArrayIndexOutOfBoundsException e){}
					catch(NullPointerException e){}
				}
			}
		}catch(IOException e){
			sever.connections.removeElement(this);
		}
		catch(NullPointerException e){
			sever.connections.removeElement(this);
		}
	}
}

⌨️ 快捷键说明

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