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

📄 appserver.java

📁 java初学者
💻 JAVA
字号:
import javax.swing.*; 
import java.util.*; 
import java.awt.*; 
import java.net.*; 
import java.io.*; 

class Customer implements Serializable 
{ 
	String infoMessage =""; 
	String sendMessage =""; 
	String custName =""; 
	String otherName =""; 
	Vector nameList = new Vector(1, 1); 
	int custSign = 0;
} 

public class AppServer extends Thread 
{ 
	ServerSocket server; 
	static Vector sumName = new Vector(1, 1); 
	static Vector sumList = new Vector(1, 1); 

	public AppServer() 
	{ 
		try 
		{ 
			server = new ServerSocket(7000); 
			System.out.println(server.toString()); 
		} 
		catch (IOException e) 
		{ 
			System.out.println("Cannot start the thread " + e); 
		}
		System.out.println("Server started....."); 
		this.start(); 
	}
	public static void main(String args[]) 
	{
		new AppServer(); 
	} 
	public void run() 
	{ 
		try 
		{ 
			while (true) 
			{ 
				Socket client = server.accept(); 
				sumName.addElement(client); 
				Connection con = new Connection(client); 
				System.out.println("From:" + client.toString() + "Connected"); 
			} 
		} 
		catch (Exception e) 
		{ 
			System.out.println("Cannot listen to the client " + e); 
		} 
	} 
} 
class Connection extends Thread 
{ 
	protected Socket netClient; 
	protected ObjectInputStream fromClient; 
	protected ObjectOutputStream toClient; 

	public Connection(Socket client) 
	{ 
		netClient = client; 

		try 
		{ 
			fromClient = new ObjectInputStream(netClient.getInputStream()); 
		} 
		catch (IOException e) 
		{ 
			System.out.println("IO Exception" + e); 

			try 
			{ 
				netClient.close(); 
			} 
			catch (IOException evt) 
			{ 
				System.out.println("Cannot closed" + evt); 
			} 
		} 
		this.start(); 
	} 

	public void run() 
	{ 
		Socket socket; 
		Customer temp; 

		try 
		{ 
			while (true) 
			{ 
				temp = (Customer) fromClient.readObject(); 

				if (temp.custSign == 2) 
				{
					AppServer.sumList.addElement(temp.custName); 
					temp.nameList = AppServer.sumList; 
					temp.infoMessage = "> > > > > > > > > > " + "欢迎" + temp.custName + "光临本聊天室" + " < < < < < < < < < <"; 
				} 

				if (temp.custSign == 3) 
				{
					int index = AppServer.sumList.indexOf(temp.custName); 
					AppServer.sumList.removeElementAt(index); 
					temp.nameList = AppServer.sumList; 
					System.out.println(temp.custName + " Exit!"); 
					AppServer.sumName.removeElementAt(index); 
					temp.infoMessage = "> > > > > > > > > > " + temp.custName + "已退出本聊天室" + " < < < < < < < < < <"; 
				} 

				if (!temp.otherName.equals("") && !temp.otherName.equals("所有人")) 
				{ 
					if (AppServer.sumList.contains(temp.otherName)) 
					{ 
						for (int i = 0; i < AppServer.sumName.size(); i++) 
						{ 
							socket = (Socket) AppServer.sumName.elementAt(i); 
							toClient = new ObjectOutputStream(socket.getOutputStream()); 
							toClient.writeObject((Customer)temp); 
						} 
					} 
					else 
					{ 
						int sum = AppServer.sumList.indexOf(temp.custName); 
						temp.sendMessage = "Sorry, " + temp.otherName + "已离开本聊天室"; 
						temp.custSign = 4; 
						socket = (Socket) AppServer.sumName.elementAt(sum); 
						toClient = new ObjectOutputStream(socket.getOutputStream()); 
						toClient.writeObject((Customer)temp); 
					} 
				} 
				else 
				{ 
					for (int i = 0; i < AppServer.sumName.size(); i++) 
					{ 
						socket = (Socket) AppServer.sumName.elementAt(i); 
						toClient = new ObjectOutputStream(socket.getOutputStream()); 
						toClient.writeObject((Customer)temp); 
					} 
				} 
			} 
		} 
		catch(Exception e) 
		{ 
			//System.out.println("Exception " + e); 
		} 
	} 
} 

⌨️ 快捷键说明

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