java_advanced_review(3)补充:利用网络套接字实现类似qq 的控制台通讯
生活随笔
收集整理的這篇文章主要介紹了
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)
【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 的控制台通讯的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异常org.xmlpull.v1.Xml
- 下一篇: 编译报错+解决方法:错误: 找不到符号