Java多线程服务器一直是我们不断的研究和创新的技术,下面我们就来看看如何才能更好的使用相关服务。们的Java多线程服务器研究完了。在我们接着讨论带有连接池示例之前,让我们回顾一下创建和使用“多线程版”的服务器的步骤:
修改 acceptConnections() 以用缺省为 50(或任何您想要的大于 1 的指定数字)实例化 ServerSocket 。
修改 ServerSocket 的 handleConnection() 以用 ConnectionHandler 的一个实例生成一个新的 Thread 。
借用 RemoteFileServer 的 handleConnection() 方法的代码实现 ConnectionHandler 类。
附: MultithreadedRemoteFileServer 的完整代码清单
Java代码
- 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;
- 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(3000);
- server.acceptConnections();
- }
- }
- 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;
- 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(3000);
- server.acceptConnections();
- }
- }
ConnectionHandler 的完整代码清单
Java代码
- import java.io.*;
- import java.net.*;
- public class ConnectionHandler implements Runnable {
- protected Socket socketToHandle;
- public ConnectionHandler(Socket aSocketToHandle) {
- socketToHandle = aSocketToHandle;
- }
- public void run() {
- try {
- PrintWriter streamWriter = new PrintWriter(socketToHandle.getOutputStream());
- BufferedReader streamReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));
- String fileToRead = streamReader.readLine();
- BufferedReader fileReader = new BufferedReader(new FileReader(fileToRead));
- String line = null;
- while ((line = fileReader.readLine()) != null)
- streamWriter.println(line);
- fileReader.close();
- streamWriter.close();
- streamReader.close();
- } catch (Exception e) {
- System.out.println("Error handling a client: " + e);
- }
- }
- }
以上就是对Java多线程服务器相关使用的介绍。希望大家有所收获。
文章标题:Java多线程服务器如何进行组建和修改
URL地址:http://www.shufengxianlan.com/qtweb/news43/457743.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联