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

📄 chatserver.java

📁 J2SE视频 第0讲_1 视频ppt及源码.rar
💻 JAVA
字号:
import java.net.*;
import java.util.*;
import java.io.*;

public class ChatServer
{
	ServerSocket server = null;
	Collection cClient = new ArrayList();
	
	public ChatServer(int port) throws Exception
	{
		server = new ServerSocket(port);
	}
	
	public void startServer() throws Exception
	{
		while(true)
		{
			Socket s = server.accept();
			cClient.add( new ClientConn(s) );
		}
	}
	
	class ClientConn implements Runnable
	{
		Socket s = null;
		public ClientConn(Socket s)
		{
			this.s = s;
			(new Thread(this)).start();
		}
		
		public void send(String str) throws IOException
		{
			DataOutputStream dos = new DataOutputStream(s.getOutputStream());
			dos.writeUTF(str);
		}
		
		public void run()
		{
			try {
				
				DataInputStream dis = new DataInputStream(s.getInputStream());
				String str = dis.readUTF();
				while(str != null && str.length() !=0)
				{
					System.out.println(str);
					for(Iterator it = cClient.iterator(); it.hasNext(); )
					{
						ClientConn cc = (ClientConn)it.next();
						if(this != cc)
						{
							cc.send(str);
						}
					}
					str = dis.readUTF();
					//send(str);
				}
				s.close();
				cClient.remove(this);
			} 
			catch (IOException e) 
			{
				System.out.println("client quit");
				try 
				{
					if(s != null)
						s.close();
					cClient.remove(this);
				} 
				catch (IOException ioe)
				{
					ioe.printStackTrace();
				}
			}
			
		}
	}
	
	public static void main(String[] args) throws Exception
	{
		ChatServer cs = new ChatServer(8888);
		cs.startServer();
	}
}

⌨️ 快捷键说明

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