您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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中怎么組建和修改多線程服務器就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。