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

📄 liaoserver.java

📁 在参加培训时做的一个聊天室
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		jt_time.setFont(f);
		jt_time.setForeground(Color.blue);
		date=new Date();
		calendar=new GregorianCalendar();
		calendar.setTime(date);
		strTime=calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
		strDate=(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
		strStatus=strTime+"       "+strDate;
		//getAppletContext().showStatus(strStatus);
		jt_time.setText(strStatus);
	}//整个时间线程结束
//============================================================
//聊天线程
	class liao extends Thread
	{	
		public void run()
		{		
			try
			{
				 while(true)
				 {
					client=serverS.accept();
					connection con=new connection(client);
				 }
			}
			catch(Exception e)
			{
				 System.out.println("监听聊天端口错误!!!");
			}
			
		}
	}
	//=======创建一个帮助实现聊天线程的类
	class connection extends Thread
    {
	  protected Socket netClient;
	  protected PrintStream toClient;
	  public connection(Socket client)
	  {
		  jta.append("\n"+z.name+"进入聊天室"+"\n\n");
		  netClient=client;		
		  this.start();
	  }
      public void run()
	  {
		 try
		 {
			 while(true)
			 {			   
                InputStreamReader in=new InputStreamReader(netClient.getInputStream());
			    BufferedReader br=new BufferedReader(in);
			    String str=br.readLine();
			    jta.append("\n Client:"+str+"      "+strTime);
				
			 }
		 }
		 catch(Exception e)
		 {
			 System.out.println("发送信息给客户错误!!!");
		 }
		 finally
		 {
			 try
			 {
				netClient.close();
				jta.append("\n"+z.name+"离开聊天室"+"\n");
			 }
			 catch(Exception e)
			 {
				System.out.println("Close Error!!!");
			 }
		 }
	  }
    }//=====帮助实现聊天线程的类结束
	//=======按钮事件代码
	public void actionPerformed(ActionEvent e)
	{
      Object obj=e.getSource();
	  if(obj==jb)
	   {
		  if (jt_name.getText().length()==0)
		  {
			 JOptionPane.showMessageDialog(null,"发送信息不能为空");//弹出消息框
			 return;
		  }
		  if (jt_name.getText().length()!=0)
		  {
		       try
		       {                
                  String str1=jt_name.getText();
				  
			      jta.append("\n Server:"+str1+"      "+strTime);
			      PrintStream bw=new PrintStream(client.getOutputStream());
			      bw.println(str1);	
		       }
		       catch(Exception ee)
		       {
			       System.out.println("发送信息给自己错误!");
		       }
		  }
	    }
		if (obj==jb2)//清空信息
		{
			jt_name.setText("");
		}
	}//聊天线程结束
//==================================================================
//注册线程
	class Zhuce extends Thread
	{	
		public void run()
		{		
				try
				{
				 while(true)
				   {
					client1=zhuceSocket.accept();
					connectionA con=new connectionA(client1);
				   }
				}
				catch(Exception e)
				{
				   System.out.println("监听注册端口错误!!!");
				}
			
		}
	}
	//=======创建一个帮助实现注册线程的类
	class connectionA extends Thread
    {
	    protected ObjectInputStream fromClient;
	    protected Socket netClient;
	    protected PrintStream toClient;
	    public connectionA(Socket client)
	    {
		    netClient=client;		
		    this.start();
	    }
        public void run()
	    {
		    try
		    {
                fromClient=new ObjectInputStream(netClient.getInputStream());
			    toClient=new PrintStream(netClient.getOutputStream());
		    }
		    catch(Exception e)
		    {
			     System.out.println("注册错误!!!");
		    }
		    try
		    {
				 z=new Message();
				 z=(Message)fromClient.readObject();			
				 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("zhuce.txt"));
				 oos.writeObject(z);
				 toClient.println("ok");
		    }
	        catch(Exception e)
		    {
			     System.out.println("发送信息给注册页面错误!!!");
		    }
		    finally
		    {
			     try
			     {
				     netClient.close();
			     }
			     catch(Exception e)
			     {
				     System.out.println("Close Error!!!");
			     }
		    }
		}
	}//帮助实现注册线程的类结束
	//注册线程结束
//==============================================================
//登录线程
	class Denglu extends Thread
	{	
		public void run()
		{		
				try
				{
				   while(true)
				   {
					   client2=dengluSocket.accept();
					   connectionB con=new connectionB(client2);
				   }
				}
				catch(Exception e)
				{
				    System.out.println("监听登陆端口错误!!!");
				}
			
		}
	}
	//=======创建一个帮助实现登陆线程的类
	class connectionB extends Thread
    {
	     protected ObjectInputStream fromClient;
	     protected Socket netClient;
	     protected PrintStream toClient;
	     public connectionB(Socket client)
	     {
		      netClient=client;		
		      this.start();
	     }
         public void run()
	     {
		     try
		     {
                 fromClient=new ObjectInputStream(netClient.getInputStream());
			     toClient=new PrintStream(netClient.getOutputStream());
		     }
		     catch(Exception e)
		     {
			     System.out.println("登陆错误!!!");
		     }
		     try
		     {
				 try
				 {
				    l=new LoginMessage();
				    l=(LoginMessage)fromClient.readObject();	
				 }
				 catch(Exception e1)
				 {
			        System.out.println("read error!");
				 }

				 try
				 {
				    ObjectInputStream oos=new ObjectInputStream(new FileInputStream("../zhuce.txt"));
				    z=(Message)oos.readObject();
				 }
				 catch(Exception e2)
				 {
				    System.out.println("read text error!");
				 }
				 if (l.name.equals(z.name)&&l.password.equals(z.password))
				 {
					  toClient.println("ok");
				 }
				 else
				 {
					  toClient.println("密码或用户名错误!");
				 }
			 }
		     catch(Exception e)
		     {
			     System.out.println("发送信息给登陆页面错误!!!");
		     }
		     finally
		     {
			     try
			     {
				     netClient.close();
			     }
			     catch(Exception e)
			     {
				     System.out.println("Close Error!!!");
			     }
			 }
		 }
	}//帮助实现登陆线程的类结束
	//登陆线程结束
//============================================
//主函数
	public static void main(String[] args) 
	{
		new liaoserver();
	}//主函数结束
}//整个应用程序结束

⌨️ 快捷键说明

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