`
yongjian1092
  • 浏览: 40859 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

基于java平台设计--登陆

阅读更多
import java.net.*;
import java.util.*;
import java.io.*;


public class ChatServer{
	public static void main(String[] args)
	{
		Hashtable<String,DataOutputStream> userList = new Hashtable<String,DataOutputStream>();
		DataInputStream dis;
		DataOutputStream dos;
		String name;
 
		
		try
		{
			ServerSocket ss = new ServerSocket(9978);
			while(true)
			{
				Socket s = ss.accept();
				dis = new DataInputStream(s.getInputStream());
				dos = new DataOutputStream(s.getOutputStream());
				name = dis.readUTF();
				userList.put(name,dos);
				new Server(name,userList,dis,dos).start();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}		
		
}


class ClientInformation
{
	public static int num = 0;
	public static String desknum[] = new String[10]; 
	public static String name[] = new String[10];
	public static String headicon[] = new String[10];
}


class Server extends Thread
{
	private Hashtable<String,DataOutputStream> userList;
	private DataInputStream dis;
	private DataOutputStream dos;
	private DataOutputStream dos2;
	private String name;
	private String name2;
	private String info;
	private String iconstring;
	private String iconstring2;
	private String desknum;
	private String desknum2;
	private boolean judue = false;
	
	public Server(String name,Hashtable<String,DataOutputStream> userList,DataInputStream dis, DataOutputStream dos)
	{
		this.name = name;
		this.dis = dis;
		this.userList = userList;
		this.dos = dos;
	}
	
	public void run()
	{
		try
		{
			if(ClientInformation.num > 0)
			{			
				String s = String.valueOf(ClientInformation.num);		
					try{
						dos.writeUTF("comein");
						dos.writeUTF(s);
						for(int i = 0; i < ClientInformation.num; ++i)
						{																									
							dos.writeUTF(ClientInformation.desknum[i]);			
							dos.writeUTF(ClientInformation.name[i]);
							dos.writeUTF(ClientInformation.headicon[i]);							
						}													
					}
					catch(Exception e)
					{
						e.printStackTrace();
				   }
			}
			transimitMessage(name+"进入大厅","服务器信息  ");
			while(true)
			{
				info = dis.readUTF();
				if(info.equals("exit")){
					DataOutputStream dos = (DataOutputStream)(userList.get(name));
					dos.close();
					dis.close();
					userList.remove(name);
					Collection doses = userList.values();
					if(judue)
					{
						for(int i = 0; i < ClientInformation.num; ++i)
						{
							if(name == ClientInformation.name[i])
							{
								String Temp;
								Temp = ClientInformation.desknum[i];
								ClientInformation.desknum[i] = ClientInformation.desknum[ClientInformation.num - 1];
								ClientInformation.desknum[ClientInformation.num - 1] = Temp;
								
								Temp = ClientInformation.name[i];
								ClientInformation.name[i] = ClientInformation.name[ClientInformation.num - 1];
								ClientInformation.name[ClientInformation.num - 1] = Temp;
								
								Temp = ClientInformation.headicon[i];
								ClientInformation.headicon[i] = ClientInformation.headicon[ClientInformation.num-1];
								ClientInformation.headicon[ClientInformation.num - 1] = Temp;
								
								ClientInformation.num--;
								break;
							}
						}
						for(Object o:doses)
						{
							dos = (DataOutputStream)o;
							try{
								dos.writeUTF("exit");
								dos.writeUTF(desknum);
							}catch(Exception e){
								e.printStackTrace();
							}
						}
					}
					transimitMessage(name+"退出大厅","服务器信息 ");
					break;
				}
				else if(info.equals("sit"))
				{
					String OutPutString;
					desknum = dis.readUTF();
					iconstring = dis.readUTF();
					int DeskNum = Integer.parseInt(desknum);
					if(DeskNum <= 10)
					{
						OutPutString = String.valueOf(DeskNum);
						desknum2 = String.valueOf(DeskNum + 10);
					}
					else
					{
						OutPutString = String.valueOf(DeskNum - 10);
						desknum2 = OutPutString;
					}				
					transimitMessage_sit(name+"进入"+OutPutString+"号桌子","服务器信息 ");
					
				}
				else if(info.equals("move"))
				{
					name2 = dis.readUTF();
					int H_YS =dis.readInt();
					int H_XS = dis.readInt();
					int B_YS = dis.readInt();
					int B_XS = dis.readInt();
					boolean Rule = dis.readBoolean();
					transimitMessageMove(H_YS, H_XS, B_YS, B_XS, Rule);
				}
				else if(info.equals("Chat"))
				{
					
					name2 = dis.readUTF();
					String Chat = dis.readUTF();
					dos2 = userList.get(name2);
					try
					{
						dos2.writeUTF("Chat");
						dos2.writeUTF(Chat);
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
				else if(info.equals("Start"))
				{
					name2 = dis.readUTF();
					dos2 = userList.get(name2);
					try
					{
						dos2.writeUTF("Start");
					}
				   catch(Exception e)
				   {
					   e.printStackTrace();
				   }
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void transimitMessage(String msg,String SeverInf)
	{
		Collection doses = userList.values();
		DataOutputStream dos;
		for(Object o:doses)
		{
			dos = (DataOutputStream)o;
			try
			{
				dos.writeUTF(SeverInf+":"+msg);				
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public void transimitMessage_sit(String msg,String SeverInf)
	{
		try
		{
			Collection doses = userList.values();
			DataOutputStream Dos;
			int ZNum = ClientInformation.num;
			int DeskNum = Integer.parseInt(desknum);
							
			boolean Otherplayer = false;
			boolean JudgeDN = true;
			
			for(int i = 0; i < ZNum; ++i)
			{
				if(desknum.equals(ClientInformation.desknum[i]))
				{
					JudgeDN = false;
					break;	
				}
				else if(desknum2.equals(ClientInformation.desknum[i]))
				{
					Otherplayer = true;
					name2 = ClientInformation.name[i];
					iconstring2 = ClientInformation.headicon[i];
					dos2 = userList.get(name2);
				}
			}
			if(JudgeDN)
			{
				if(Otherplayer)
				{
					if(judue != true && name != name2)
					{
						try
						{
							dos.writeUTF("play");
							dos.writeUTF(name2);
							dos.writeUTF(iconstring2);
							dos2.writeUTF("play");
							dos2.writeUTF(name);
							dos2.writeUTF(iconstring);
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
					}
				}
				for(Object o:doses)
				{
					Dos = (DataOutputStream)o;
					try
					{
						Dos.writeUTF(SeverInf+":  "+msg);
						Dos.writeUTF(info);
						String Numstring = desknum;
						Dos.writeUTF(Numstring);
						Dos.writeUTF(name);
						Dos.writeUTF(iconstring);		
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
				if(judue == true)
				{
					for(int i = 0; i < ZNum; ++i)
					{
						if(name == ClientInformation.name[i])
						{
							ClientInformation.desknum[i] = desknum;
							ClientInformation.name[i] = name;
							ClientInformation.headicon[i] = iconstring;
						}
					}
				}
				else
				{
					ClientInformation.desknum[ClientInformation.num] = desknum;
					ClientInformation.name[ClientInformation.num] = name;
					ClientInformation.headicon[ClientInformation.num] = iconstring;
					ClientInformation.num++; 
					judue = true;
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	public void transimitMessageMove(int H_YS, int H_XS, int B_YS, int B_XS, boolean Rule)
	{
		dos2 = userList.get(name2);
		try
		{
			dos2.writeUTF("Move");
			dos2.writeInt(H_YS);
			dos2.writeInt(H_XS);
			dos2.writeInt(B_YS);
			dos2.writeInt(B_XS);
			dos2.writeBoolean(Rule);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics