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

📄 appserver.java

📁 使用java实现聊天室
💻 JAVA
字号:
/**
 * 
 */
package server;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.Timer;
/**
 * @author jason
 *
 */
public class AppServer implements Runnable {

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	
	ServerSocket server;
	Socket fromClient;
	Thread serverThread;
	
	public AppServer()
	{
		System.out.println("FunChat 聊天服务器启动......");
		try{
			server = new ServerSocket(1001);
			serverThread = new Thread(this);
			serverThread.start();
		}catch(Exception e)
		{
			System.out.println("不能启动该线程"+e);
		}
	}
	
	public void run() {
		// TODO Auto-generated method stub
		try{
			while(true)
			{
				fromClient = server.accept();
				Connect con = new Connect(fromClient);
			}				
		}catch(Exception e)
		{
			System.out.println("不能监听客户请求"+e);
		}
	}
	
	static class Connect
	{
		ObjectOutputStream streamToClient;
		int ctr = 0;
		
		BufferedReader streamFromClient;
		static Vector vector;
		static Vector vctrList;
		String message ="";
		static String str = new String("用户列表");
		

		int verify(String mesg)
		{
			try{
				RandomAccessFile RAS = new RandomAccessFile("UsrPwd.txt","r");
				int i = 0;
				String str ="";
				while((RAS.getFilePointer())!=(RAS.length()))
				{
					str = RAS.readLine();
					if(str.equals(mesg))
					{
						ctr = 1;
						break;
					}
				}
				RAS.close();				
			}catch(Exception e)
			{
				System.out.println("发生异常"+e);
			}
			return ctr;
		}
		
		int checkFile(String mesg)
		{
			int chk = 1;
			try{
				RandomAccessFile RS = new RandomAccessFile("UsrPwd.txt","r");
				int i = 0;
				String str = "";
				String colon = new String(":");
				int index = ((String)mesg).lastIndexOf(colon);
				String userName = (String)mesg.substring(0,index);
				while((RS.getFilePointer())!=(int)(RS.length()))
				{
					str = RS.readLine();
					int index1 = ((String)str).lastIndexOf(colon);
					String usrName = (String)str.substring(0,index1);
					if(usrName.equals(userName))
					{
						chk = 0;
						break;
					}
				}
				RS.close();
			}catch(Exception e)
			{
				System.out.println("发生异常"+e);
			}
			return chk;
		}
		
		public Connect(Socket inFromClient)
		{
			//提取客户端的流
			String msg="";
			String mesg="";
			try
			{
				streamFromClient = new BufferedReader(new InputStreamReader(inFromClient.getInputStream()));
				streamToClient = new ObjectOutputStream(inFromClient.getOutputStream());
				
				msg = streamFromClient.readLine();
				if(msg.equals("来自定时器"))
				{
					streamToClient.writeObject(vector);
					streamToClient.writeObject(vctrList);
				}
				else if(msg.equals("登录信息"))
				{
					msg = streamFromClient.readLine();
					int ver = verify(msg);
					if(ver==1)
					{
						String colon = new String(":");
						int index = ((String )msg).lastIndexOf(colon);
						String userName = (String)msg.substring(0, index);
						if(!(vctrList.indexOf((String)userName)>0))
						{
							streamToClient.writeObject("欢迎");
							vctrList.addElement((String)userName);
						}
					}
					else
					{
						streamToClient.writeObject("拒绝登录");
					}
				}
				else if(msg.equals("注册信息"))
				{
					msg = streamFromClient.readLine();
					int ret = checkFile(msg);
					if(ret==0)
					{
						streamToClient.writeObject("用户已存在");
					}
					if(ret==1)
					{
						FileOutputStream out = new FileOutputStream("UsrPwd.txt",true);
						PrintStream p = new PrintStream(out);
						p.println();
						p.println(msg);
						p.close();
						streamToClient.writeObject("已注册");
					}
				}
				else if(msg.equals("用户退出"))
				{
					String remUser = streamFromClient.readLine();
					boolean b = vctrList.removeElement((String)remUser);
				}
				else
				{
					message = message + msg;
					vector.addElement((String)message);
					//vector.addElement(msg);
					streamToClient.writeObject(vector);
					//streamToClient.writeObject((String)message);
				}
			}//try块结束
			catch(Exception e)
			{
				System.out.println("无法获取客户端的流对象"+e);
			}
			finally
			{
				try{
					inFromClient.close();
				}catch(IOException e)
				{
					System.out.println("发生异常:"+e);
				}
			}
		}
		
		static{
			vector = new Vector(1,1);
			vctrList = new Vector(1,1);
			vctrList.addElement((String)str);
		}
		
	}
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new AppServer();
	}

}

⌨️ 快捷键说明

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