java Socket 使用多線程實現一對多的通訊

來源:互聯網
上載者:User

             在上一篇完成了P2P的通行之後,只需要在服務端和用戶端加入多線程,就可以實現伺服器端和多個用戶端進行通訊,代碼如下:

服務端線程:

</pre><pre name="code" class="java">import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;/*** *  * @author dell *伺服器端每次都要啟動一個線程來對當前監聽到的Socket進行通訊處理 */public class ServerThread implements Runnable{private Socket client;  // 當前用戶端對應的Socketprivate BufferedReader read=new BufferedReader(new InputStreamReader(System.in));  // 用來從鍵盤讀取資訊private PrintStream out;  // 用來向當前的用戶端Socket輸出資訊private BufferedReader in;// 用來向當前的用戶端的Socket讀取資訊public ServerThread(Socket client) {  // 構造方法super();this.client = client;}public void run() {try {out=new PrintStream(client.getOutputStream());in=new BufferedReader(new InputStreamReader(client.getInputStream()));out.println("伺服器已經串連成功");  // 告訴用戶端已經串連成功out.flush();             while(true){String msg=in.readLine();  // 讀取用戶端的資訊System.out.println("連接埠為"+client.getLocalPort()+"的客戶說:"+msg);// 輸出用戶端的資訊msg=read.readLine(); // 從鍵盤得到伺服器端的回複資訊out.println(msg);out.flush();     // 輸出緩衝流}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println(e.getMessage()); // 列印錯誤資訊}finally{try {out.close();in.close();client.close();} catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());}}}}
用戶端線程:

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;/*** *  * @author dell *伺服器使用Socket串連上伺服器之後,啟動ClientThread 線程來處理該線程的Socket */public class ClientThread implements Runnable {private Socket server;  //與伺服器端對應的Socketprivate BufferedReader read=new BufferedReader(new InputStreamReader(System.in));  // 用來從鍵盤讀取資訊private PrintStream out;  // 用來向當前的伺服器端Socket輸出資訊private BufferedReader in;// 用來向當前的伺服器的Socket讀取資訊public ClientThread(Socket server) {super();this.server = server;}public void run() {try {out=new PrintStream(server.getOutputStream());in=new BufferedReader(new InputStreamReader(server.getInputStream()));while(true){String msg=in.readLine();  // 讀取伺服器的資訊System.out.println("伺服器端的回複:"+msg);// 輸出伺服器的資訊msg=read.readLine(); // 從鍵盤得到向伺服器的回複資訊out.println(msg);out.flush();     // 輸出緩衝流}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println(e.getMessage()); // 列印錯誤資訊}finally{try {out.close();in.close();server.close();} catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());}}}}

伺服器代碼:

import java.net.ServerSocket;import java.net.Socket;public class MyServer {/** * @param args */public static void main(String[] args) {try {ServerSocket ss=new ServerSocket(30000);while(true){Socket client=ss.accept();ServerThread serverThread=new ServerThread(client);new Thread(serverThread).start();  // 啟動線程}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

用戶端代碼:
import java.net.Socket;public class MyClient {public static void main(String[] args) {try {Socket client=new Socket("127.0.0.1",30000);new Thread(new ClientThread(client)).start();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} }}
由此可以運行多個用戶端和同一個伺服器端進行通訊。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.