日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java_advanced_review(3)补充:利用网络套接字实现类似qq 的控制台通讯

發布時間:2023/12/3 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java_advanced_review(3)补充:利用网络套接字实现类似qq 的控制台通讯 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】README 1)本文旨在實現 利用網絡套接字實現類似qq 的控制臺通訊, client 采用telnet and java app, server采用 java app; 2)also, you can check out the source code from?https://github.com/pacosonTang/core-java-volume/tree/master/coreJavaAdvanced/chapter3/qq_resembled 3)本代碼的問題不足在于 沒有實現 server 發送到 多個 client 的 socket 連接,即,server 發送的消息屬于群發,而不是 peer to peer ?的 發送,如果需要完成這個工作的話,需要 socket 連接池,對于每個 client 都建立相應的 socket,即建立相應的 OutputStream and InputStream; 4)編譯本代碼,可能會報錯(錯誤:找不到符號),解決方法參見: http://blog.csdn.net/pacosonswjtu/article/details/51804636
【1】server by java app public class ServerPlainTest {public static void main(String[] args) {try {ServerSocket ss = new ServerSocket(8189);System.out.println("the server has startuped, waiting for connections.");while (true) { // accept multiple clients connection request.Socket s = ss.accept();System.out.println("a client has connected successfully.");new Thread(new ReceiveHandler(s, Role.SERVER)).start();new Thread(new SendHandler(s, Role.SERVER)).start();}} catch (Exception e) {System.out.println(e);}} } public class MessageHandler { }// receive msg. class ReceiveHandler implements Runnable {private Socket socket;private Role role;public ReceiveHandler(Socket socket, Role role) {this.socket = socket;this.role = role; }public void run() {try {System.out.println(role + " executes ReceiveHandler.run method.");InputStream is = socket.getInputStream();Scanner in = new Scanner(is);// echo client inputboolean done = false;while (!done && in.hasNextLine()) {String line = in.nextLine();System.out.println(line);if (line.trim().equalsIgnoreCase("bye")) {done = true;}}// close relative instance.in.close();} catch (IOException e) {e.printStackTrace();} } }// send msg. class SendHandler implements Runnable {private Socket socket;private Role role;public SendHandler(Socket socket, Role role) {this.socket = socket;this.role = role; }public void run() {PrintWriter pw = null;try {System.out.println(role + " executes SendHandler.run method.");Scanner s = new Scanner(System.in);OutputStream os = socket.getOutputStream();pw = new PrintWriter(os, true); // autoFlush=true.if (role.equals(Role.SERVER)) {pw.println("from server: you've connected with the server.");}while (s.hasNext()) {String line = s.nextLine();pw.println("from " + role + ": " + line);}pw.println("connection closed successfully. bye!");} catch (Exception e) {e.printStackTrace();} } } 【2】client by java app 1)source code is as following
public class ClientPlainTest {public static void main(String[] args) {Socket s = null;try {s = new Socket("localhost", 8189);System.out.println("connection with the server successfully.");new Thread(new ReceiveHandler(s, Role.CLIENT)).start();new Thread(new SendHandler(s, Role.CLIENT)).start();} catch (Exception e) {System.out.println(e);} } } 2)console info:

【3】client by telnet (connect with server by telnet localhost 8189)

總結

以上是生活随笔為你收集整理的java_advanced_review(3)补充:利用网络套接字实现类似qq 的控制台通讯的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。