multithreadedremotefileserver.java

来自「java socket基础编程实例」· Java 代码 · 共 49 行

JAVA
49
字号
   /*
	* Name of the Application     : MultithreadedRemoteFileServer.java
	* Development Environment     : Eclipse3.0M8
	* @Version 1.0
	* Describtion 
	* 
	*	1)response requests from the client for the file  
	*
	*
	* Creation / Modification History
	*	wsl	19:30 06/29/2004	Creation       
	*
	* Copyright ® 2004 www.cnlab.net
	* All right reserved. 
	*/
package net.cnlab.wsl.testsocket.multithread;

import java.io.*;
import java.net.*;
public class MultithreadedRemoteFileServer {
	
	protected int listenPort;
	
	public MultithreadedRemoteFileServer(int aListenPort) {
		listenPort = aListenPort;
	}
	public void acceptConnections() {
		try {
			ServerSocket server = new ServerSocket(listenPort, 5);
			Socket incomingConnection = null;
			System.out.println("the MultiThreadServer is running...");
			while (true) {
				incomingConnection = server.accept();
				handleConnection(incomingConnection);
			}
		} catch (BindException e) {
			System.out.println("Unable to bind to port " + listenPort);
		} catch (IOException e) {
			System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
		}
		}
		public void handleConnection(Socket connectionToHandle) {
			new Thread(new ConnectionHandler(connectionToHandle)).start();
		}
		public static void main(String[] args) {
			MultithreadedRemoteFileServer server = new MultithreadedRemoteFileServer(3001);
			server.acceptConnections();
		}
}

⌨️ 快捷键说明

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