- Java编程示例
- 示例-主页
- 示例-环境
- 示例-字符串
- 示例-数组
- 示例-日期和时间
- 示例-方法
- 示例-文件
- 示例-目录
- 示例-异常
- 示例-数据结构
- 示例-集合
- 示例-网络
- 示例-线程
- 示例-小程序
- 示例-简单图形用户界面
- 示例-JDBC
- 示例-正则表达式
- 示例-Apache PDF Box
- 示例-Apache POI PPT
- 示例-Apache POI Excel
- 示例-Apache POI Word
- 示例-OpenCV
- 示例-Apache Tika
- 示例-iText
- Java教程
- Java - 教程
- Java有用资源
- Java - 快速指南
- Java - 有用资源
如何在Java中创建多线程服务器
问题描述
如何创建多线程服务器?
解决方案
以下示例演示如何使用Socket类的ssock.accept()方法和ServerSocket类的MultiThreadServer(socketname)方法创建多线程服务器。
import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class MultiThreadServer implements Runnable { Socket csocket; MultiThreadServer(Socket csocket) { this.csocket = csocket; } public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); while (true) { Socket sock = ssock.accept(); System.out.println("Connected"); new Thread(new MultiThreadServer(sock)).start(); } } public void run() { try { PrintStream pstream = new PrintStream(csocket.getOutputStream()); for (int i = 100; i >= 0; i--) { pstream.println(i + " bottles of beer on the wall"); } pstream.close(); csocket.close(); } catch (IOException e) { System.out.println(e); } } }
结果
以上代码样例将产生以下结果。
Listening Connected
以下是创建多线程服务器的另一个示例。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Calendar; public class NewClass { ServerSocket myServerSocket; boolean ServerOn = true; public NewClass() { try { myServerSocket = new ServerSocket(8888); } catch(IOException ioe) { System.out.println("Could not create server socket on port 8888. Quitting."); System.exit(-1); } Calendar now = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat( "E yyyy.MM.dd 'at' hh:mm:ss a zzz"); System.out.println("It is now : " + formatter.format(now.getTime())); while(ServerOn) { try { Socket clientSocket = myServerSocket.accept(); ClientServiceThread cliThread = new ClientServiceThread(clientSocket); cliThread.start(); } catch(IOException ioe) { System.out.println("Exception found on accept. Ignoring. Stack Trace :"); ioe.printStackTrace(); } } try { myServerSocket.close(); System.out.println("Server Stopped"); } catch(Exception ioe) { System.out.println("Error Found stopping server socket"); System.exit(-1); } } public static void main (String[] args) { new NewClass(); } class ClientServiceThread extends Thread { Socket myClientSocket; boolean m_bRunThread = true; public ClientServiceThread() { super(); } ClientServiceThread(Socket s) { myClientSocket = s; } public void run() { BufferedReader in = null; PrintWriter out = null; System.out.println( "Accepted Client Address - " + myClientSocket.getInetAddress().getHostName()); try { in = new BufferedReader( new InputStreamReader(myClientSocket.getInputStream())); out = new PrintWriter( new OutputStreamWriter(myClientSocket.getOutputStream())); while(m_bRunThread) { String clientCommand = in.readLine(); System.out.println("Client Says :" + clientCommand); if(!ServerOn) { System.out.print("Server has already stopped"); out.println("Server has already stopped"); out.flush(); m_bRunThread = false; } if(clientCommand.equalsIgnoreCase("quit")) { m_bRunThread = false; System.out.print("Stopping client thread for client : "); } else if(clientCommand.equalsIgnoreCase("end")) { m_bRunThread = false; System.out.print("Stopping client thread for client : "); ServerOn = false; } else { out.println("Server Says : " + clientCommand); out.flush(); } } } catch(Exception e) { e.printStackTrace(); } finally { try { in.close(); out.close(); myClientSocket.close(); System.out.println("...Stopped"); } catch(IOException ioe) { ioe.printStackTrace(); } } } } }
java_networking.htm
广告