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

📄 chatserver.java

📁 TCP一对多通信
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.util.*;
public class chatserver
{
	public static void main(String[] args) throws IOException
	{
			ServerSocket socket=null;
			Vector m_threads=new Vector();
			System.out.println("Listen...");
			try
			{
				socket=new ServerSocket(5555);
			}
			catch(Exception e)
			{
				System.out.println("new ServerSocket() failed!");
				return;	
			}
			try
			{
				int nid=0;
				while(true)
				{
					//监听是否有新chat Applet连接到Server,
					//程序会陷入到该语句,直到有新的连接产生。
					Socket s=socket.accept();
					System.out.println("accepted");
					//创建一个新的ServerThread
					ServerThread st=new ServerThread(s,m_threads);
					//为该线程设置一个ID号。
					st.setID(nid++);
					//将该线程加入到m_threads Vector中。
					m_threads.addElement(st);
					new Thread(st).start();
					//通知所有Chat Applet有一个新的网友加入
					for(int i=0;i<m_threads.size();i++)
					{
						ServerThread st1=(ServerThread)m_threads.elementAt(i);
						st1.write("<#>welcome"+st.getID()+"to enter chatroom!");
					}
					System.out.println("Listen again...");
						
				}		
			}
			catch(Exception e)
			{
				System.out.println("Server is down...");	
			}
	}		
}
/*
 *ServerThread是监听线程,用于监听对应的Chat Applet是否有信息传来。
*/
class ServerThread implements Runnable
{
	Vector m_threads;
	Socket m_socket=null;
	DataInputStream m_in=null;
	DataOutputStream m_out=null;
	
	int m_nid;
	//初始化该线程
	public ServerThread(Socket s,Vector threads)
	{
		m_socket=s;
		m_threads=threads;
		try
		{
	//		System.out.println(inetaddr);
			m_in=new DataInputStream(m_socket.getInputStream());
			m_out=new DataOutputStream(m_socket.getOutputStream());
		}
		catch(Exception e)
		{
				
		}	
		
	}
	//线程的执行体
	public void run()
	{
		System.out.println("Thread is runing");
		try
		{
			while(true)
			{
				//监听对应的Applet是否传来消息
				//程序陷入到m_in.readUTF()中,直到有信息传来才返回。
				String s=m_in.readUTF();
				if(s==null)
				 	break;
				if(s.equals("leave"))
					for(int i=0;i<m_threads.size();i++)
					{
						ServerThread st=(ServerThread) m_threads.elementAt(i);
						st.write("***"+getID()+"leave..."+"***");
						m_threads.removeElement(this);		
					}
				else
				{
					for(int i=0;i<m_threads.size();i++)
					{
						ServerThread st=(ServerThread) m_threads.elementAt(i);
						st.write("<"+getID()+">"+s);
					}	
				}
				
			}	
		}
		catch(Exception e)
		{
			e.printStackTrace();	
		}
		//从m_threads Vector中删除该线程,表示该线程已经离开chat room
		//m_threads.removeElement(this);
		try
		{
			m_socket.close();	
		}	
		catch(Exception e)
		{
			
		}
	}
	//write()将msg送回对应的Applet
	public void write(String msg)
	{
		synchronized(m_out)
		{
			try
			{
				m_out.writeUTF(msg);	
			}	
			catch(Exception e)
			{
				
			}
		}	
	}
	//获得该线程的ID
	public int getID()
	{
		return m_nid;	
	}
	//设置线程的ID
	public void setID(int nid)
	{
		m_nid=nid;
	}
}

























⌨️ 快捷键说明

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