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

📄 multichatserverdemo.java

📁 java经典10例子
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultiChatServerDemo{ 
	public static void main(String args[]){
		ServerSocket serverSocket=null;
		try{
			serverSocket=new ServerSocket(1111);
		}catch(Exception e){
			System.out.println("Error"+e);
			System.exit(1);
		}
		while(true){
			Socket clientSocket=null;
			System.out.println("waiting for users...");
			try	{
				clientSocket=serverSocket.accept();
			}catch(IOException e){
				System.out.println("accept failed:"+e);
			}
			new ServerThread(clientSocket).start();
		}
	}
} 

class ServerThread extends Thread{
	DataInputStream input;
	PrintStream output;
	String user;
	Socket clientSocket;
	
	ServerThread(Socket clientSocket){
		this.clientSocket=clientSocket;
	}
	
	public void run(){
		try{
			input=new DataInputStream(
				clientSocket.getInputStream());
			output=System.out;
			user=input.readLine();
			System.out.println(user+" Connected!");
		}catch(IOException e){ }
		try {
			while(true){
				String string;
				if((string=input.readLine())==null)
					break;
				output.println(user+string);
				output.flush();
			}
		}catch(Exception e){
			return;
		}		
		System.out.println(user+ "has disconnected.");
		try{
			clientSocket.close();
			input.close();
		}catch(Exception e){
			return;
		}
	}
}

⌨️ 快捷键说明

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