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

📄 mserver.java

📁 用java写的一个基于Client-Server Model的简单的聊天程序。包括服务器端和客户端程序。
💻 JAVA
字号:
/****************************
		
	        	Created by : Khurram Nawaz Abbasi
			Student ID : 600304568

			****************************/
//Different Packages introduced under these pakages i worked on my program below
import java.io.*;
import java.net.*;
import java.util.*;


public class MServer //server class starts here
{
	private static ServerSocket servSocket;
	private static final int PORT = 1234;
	static Socket client;
	static int client_id = 0;


	public static void main(String[] args) throws IOException
	{
		try
		{
			System.out.println("\n Waiting For Client Request......");
			servSocket = new ServerSocket(PORT);



		}// try block ends here
		catch (IOException e)
		{
			System.out.println("\n Unable to setup the port");
			System.exit(1);
		}// catch block ends here

		do
		{
			Socket client = servSocket.accept();

			System.out.println("\n We are joined by Client # " + client_id);


			ChatHandler handler = new ChatHandler(client);
			handler.start();


			client_id++;


		} while (true); // while loop ends here

	} // Main method ends here
}

// class that i used to handel multiple clients

class ChatHandler implements Runnable
{

	// it creates a vector for all the handlers

	protected static Vector handlers = new Vector();
	protected Socket socket;
	protected DataInputStream dataRcv;
	protected DataOutputStream dataSend;
	protected Thread listener;

	public ChatHandler(Socket socket)
	{
		this.socket = socket;
	}
	public synchronized void start()
	{
		if (listener == null)
		{
			try
			{
				dataRcv = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
				dataSend = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
				listener = new Thread(this);
				listener.start();
			}
			catch (IOException ignored) { }
		}
	}

	public synchronized void stop()
	{
		if (listener != null)
		{
			try
			{
				if (listener != Thread.currentThread())
					listener.interrupt();
				listener = null;
				dataSend.close();
			}
			catch (IOException ignored) { }
		}
	}

	/*******************
	
	run method starts here
	
********************/
	public void run()
	{
		try
		{
			// add them to the handlers pool

			handlers.addElement(this);
			while (!Thread.interrupted())
			{

					// rcv or read the message so it can be broadcast

					String message = dataRcv.readUTF();
				broadcast(message);
			}
		}
		catch (EOFException ignored)
		{
		}
		catch (IOException ex)
		{
			if (listener == Thread.currentThread())
				ex.printStackTrace();
		}
		finally
		{
			// thread  to exit
			handlers.removeElement(this);
		}
		stop();
	}

	/******************************

	 this is the main function that i used to broadcast my client message to every other client who joins the server.

*******************************/

	protected void broadcast(String message)
	{
		synchronized (handlers)
		{
			Enumeration enum1 = handlers.elements();
			while (enum1.hasMoreElements())
			{
				ChatHandler handler = (ChatHandler)enum1.nextElement();
				try
				{
					handler.dataSend.writeUTF(message);
					handler.dataSend.flush();
				}
				catch (IOException ex)
				{

					// the connection ends
					handler.stop();
				}
			}
			}
		}
	}

⌨️ 快捷键说明

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