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

📄 servestart.java

📁 Received chat message sent there is choice, as well as an increase of server features and color disp
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		while(true){
			//transfer location change Single User or Multi User
			ChatServer mu=new ChatServer(server.accept(),null);
			mu.start();
		}
	}
}

class ChatServer extends Thread{

	/**
	 * @param args
	 */
 Socket client;
	static  String cache="";
	private String localcache="";
	static boolean outtype=false;
	private String str;
	private Thread t;
	PrintWriter out;
	BufferedReader in;
	static ServeStart servestart;
    String currentUser=null;
	static boolean isWork=false;
    boolean check=true;
	static public Vector <ChatServer> serverCollection= new Vector<ChatServer>();
	static Vector <Socket> socketlist=new Vector<Socket>();
	static public Vector <String> userList= new Vector<String>();
	private static HashMap<String,String> user= new HashMap<String,String>();
	static HashMap<String,Socket> userSocket= new HashMap<String,Socket>();
	public ChatServer(Socket c, ServeStart ss) throws IOException{
		this.client=c;
		servestart=ss;
		user.put("1","1");
		user.put("2","2");
	/*	t = new Thread(new Runnable(){
			public void run(){
				try{
					out=new PrintWriter(client.getOutputStream());
					boolean outted=false;
					while(isWork){
						if((!outted)&&outtype){
							out.println(cache);
							out.flush();
							outted=true;
						}else if(!outtype){
							outted=false;
						}
					}
				}catch(IOException e){
					e.printStackTrace();
				}finally{
				}
				
			}
		});
			*/			
	    //t.start();
	}
							
						 
	

	public void run(){
		try{
			in=new BufferedReader(new InputStreamReader(client.getInputStream()));
			out=new PrintWriter(client.getOutputStream());
			//Mutil User but can't parallel
			boolean flag=true;
			while(flag&&isWork){
				str=in.readLine();
				if(str.equals("%1%1")){//添加用户
					String username=in.readLine();
					String password=in.readLine();
					if(user.get(username)==null){
						user.put(username,password);
						out.println("%6%6");//succeed
						out.flush();
					}else{
						out.println("%5%5");
						out.flush();
					}
				}else if(str.equals("%t%tTestServer")){
					out.println("%t%tOK");
					out.flush();
				}else if(str.equals("%0")){//jump out of cycle 1 at server
					
					//flag=false;
					//break;
				}else if(str.equals("%end")){
					//退出thread,登陆任务完成
					break;
				}else if(str.equals("%2%2")){//检测用户密码,若无则新用户
					String username=in.readLine();
					String password=in.readLine();
					String realpass=user.get(username);
					if(realpass==null){
						out.println("%3%3");//无此用户, 新用户
						out.flush();
						//user.put(username,password);
					}else if(realpass.equals(password)){
						//while(true){
						//	if(userList.iterator().hasNext()){
						//		if(userList.iterator().next()==username){
						//		   out.println("%3%3");
						//		   out.flush();
						//		   break;
						//	    }
						
						//	
						//   }
						//}
						
					
						out.println("%4%4");          //密码正确,登陆成功
						out.flush();
						out.println(username);
						out.flush();
						this.currentUser=username;
					    userList.add(currentUser);
					    ServeStart.jList.setListData(userList);
					    
					    socketlist.add(client);
				        //userSocket.put(currentUser,client);
					    serverCollection.add(this);
					    //sendMsg("%7%7",socketlist);
			            sendMsg("%2%2,"+now()+",欢迎 "+currentUser+" 进入聊天室!%8%8",socketlist);
			            sendMsg("%1%1,"+changeString(userList),socketlist);
						flag=false;
						break;	
					}else{
						out.println("%5%5");//密码错误
						out.flush();
					}
					
				}
			}
			System.out.println(this);
			while(isWork){
				//getUserList();
				//if(check==true){
				//	if(((String)ServeStart.index)==this.currentUser){
			    //        check=false;
			    //        out.println("%7%7");
			    //       out.flush();
			    //        out.println("你已被管理员踢出去了!");
			    //        out.flush();
			    //        break;
		        //   }
			
					
				//}
				try{
					str=in.readLine();
				}catch(SocketException e){
					e.printStackTrace();
					del(!isWork);
					
				}
				if(str.equals("%end")){
					//退出thread,chat任务完成
					break;
				}else if(!str.equals("%7%7")){
					System.out.println(str+"ccccccccccccccccccccccccccccccccccccc");
					sendMsg(str,socketlist);
					
					//localcache=cache;
					//System.out.println(cache+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
					//out.println(cache);
					//out.flush();
				}
			}
			//out.println("%ServerEnd");
			//out.flush();
			
			del(check);
			
		}catch(IOException ex){
			ex.printStackTrace();
			System.out.println("一个断开");
			del(!isWork);
		}finally{
		}
		
	}

	public static String now() 
    {
        Date today = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("[ hh:mm:ss ] ");
        String dateString=null;
        try 
        {
            dateString = formatter.format(today);
            return dateString;
        } catch (IllegalArgumentException iae) {
            //result.setForeground(Color.red);
           // result.setText("Error: " + iae.getMessage());
        }
		return dateString;
    }
	
	public static Vector<String> getUserList(){
		return userList;
	}
	
	void del(boolean xp){
		userList.remove(currentUser);
		ServeStart.jList.setListData(userList);
		
		socketlist.remove(client);
		//userSocket.remove(currentUser);
		serverCollection.remove(this);
		if(xp==false){
			
		}else{
		    //sendMsg("%7%7",socketlist);
		    sendMsg("%2%2,"+now()+","+currentUser+"已经离开了..%8%8",socketlist);
		    sendMsg("%1%1,"+changeString(userList),socketlist);
		}
		try{
			client.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.stop();
		}
		
		
		
	}
	
	static void sendMsg(String a,Vector <Socket> b){
		for(int i=0;i<b.size();i++){
			try{
				PrintWriter dout=new PrintWriter(((Socket)b.get(i)).getOutputStream());
				dout.println(a);
				dout.flush();
			}catch(Exception e){
				e.printStackTrace();
			}finally{
			}
		}
		
		
	}
	
	static void delete(String userab){
		ChatServer s=search(userab);
		userList.remove(s.currentUser);
		ServeStart.jList.setListData(userList);
		
		socketlist.remove(s.client);
		userSocket.remove(s.currentUser);
		//sendMsg("%7%7",userab);
		sendMsg("%2%2,"+now()+",你已经被管理员踢出去了!%8%8",userab);
		//sendMsg("%7%7",socketlist);
		sendMsg("%2%2,"+now()+","+s.currentUser+"已经被管理员踢出去了!%8%8",socketlist);
		sendMsg("%1%1,"+changeString(userList),socketlist);
		s.check=false;
		try{
			s.client.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			s.stop();
		}
	}
	
	static void sendMsg(String a,String userab){
		try{
			PrintWriter dout=new PrintWriter(search(userab).client.getOutputStream());
			dout.println(a);
			dout.flush();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
		}
	}
	
	
	static ChatServer search(String userab){
		for(int i=0;i<serverCollection.size();i++){
			if(userab==((ChatServer)serverCollection.get(i)).currentUser){
				return (ChatServer)serverCollection.get(i);
			//	break;
			}
		}
		return null;
	}
	
	public String toString(){
		return currentUser+client;
	}


	public static void end() {
		// TODO Auto-generated method stub
		isWork=false;
	}
	
	public static String changeString(Vector a){
		String lm="";
		for(int i=0;i<a.size();i++){
			lm+=(String)a.get(i);
			if(i<a.size()-1){
				lm+=",";
			}
		}
		return lm;
	}
}

⌨️ 快捷键说明

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