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

📄 server.java

📁 javasocket简单聊天程序适合socket初学者
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.util.*;

class Server {
	boolean started = false;
	ServerSocket ss = null;
	List<ClientRun> list = new ArrayList<ClientRun>();
	public static void main(String[] args) {
		new Server().start();
	}

	public void start()
	{
		try
		{
			ss = new ServerSocket(8888);
			started = true;				
		}
		catch (BindException be)
		{
			System.out.println("端口使用中!");
		}
		catch (IOException e)
		{
			System.out.println("服务器连接失败!");
		}
		try
		{
			while (started) {
				Socket s = ss.accept();
				ClientRun cr = new ClientRun(s);        //主线程只负责接收信息,每个客户端连接进来都会开始一个新线程
				new Thread(cr).start();    //把连接进来的Socket传到线程中。
				list.add(cr);
System.out.println("系统提示:"+s.getInetAddress()+"已经联入");
			}
		}
		catch (IOException e)
		{
			System.out.println("Client closed!");
		}
	}
/***********************************多线程实现多客户端同时连接**************************************************/
	class ClientRun implements Runnable
	{
		private Socket s;
		private DataInputStream dis = null;
		private DataOutputStream dos = null;
		boolean bConnect = false;
		public ClientRun(Socket s)                        
		{
			this.s = s;
			try
			{
				dis = new DataInputStream(s.getInputStream());   
				dos = new DataOutputStream(s.getOutputStream());
				bConnect = true;
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}	
		}

		public void send(String str)
		{
			try
			{
				dos.writeUTF(str);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			
		}
		public void run()
		{
			try
			{
				while (bConnect)
				{
					String str = dis.readUTF();
System.out.println(str);
					for (int i=0 ;i<list.size() ;i++ )
					{
						ClientRun cr = list.get(i);
						cr.send(str);
					}
				}
			}
			catch (Exception e)
			{
				System.out.println(s.getInetAddress()+"离开了!");
			}
			finally
			{
				try
				{
					if (dis != null) dis.close();
					if (dos != null) dos.close();
					if (s != null){
						s.close();
						s = null;
					}
				}
				catch (IOException io)
				{
					io.printStackTrace();
				}
			}
			
		}
	}
}

⌨️ 快捷键说明

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