java完成socket客戶端銜接辦事端。本站提示廣大學習愛好者:(java完成socket客戶端銜接辦事端)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成socket客戶端銜接辦事端正文
本例只做簡略功效演示,代碼其實不嚴謹,只是解釋客戶端若何完成銜接辦事端簡略代碼。
代碼在集成Eclipse對象下測試編譯運轉情況以下圖所示:
客戶端echoClient.java代碼:
package com.zhengzz.echo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; class EchoC{ private Socket socket; public EchoC() { // TODO Auto-generated constructor stub try { socket = new Socket("localhost", 60000); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void Client() { try { BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); BufferedReader lbr = new BufferedReader(new InputStreamReader(System.in)); String linestr = null; while ((linestr = lbr.readLine()) != null) { pw.println(linestr); System.out.println(br.readLine()); if (linestr.equals("bye#")) { break; } } } catch (IOException e) { e.printStackTrace(); } } } public class echoClient { public static void main(String[] args) { new EchoC().Client(); } }
辦事端echoServer.java代碼:
package com.zhengzz.echo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; class EchoS{ private ServerSocket serverSocket; public EchoS() { try { serverSocket = new ServerSocket(60000); } catch (IOException e) { e.printStackTrace(); } } public void Server() { while (true) { try { Socket socket = serverSocket.accept(); System.out.println("client:" + socket.getInetAddress() + ":" + socket.getLocalPort()); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); String linestr; while ((linestr = br.readLine()) != null) { System.out.println(linestr); pw.println("--->" + linestr); } } catch (IOException e) { System.out.println("銜接斷開:("); } } } } public class echoServer { public static void main(String[] args) { new EchoS().Server(); } }
我在Eclipse對象下直接調試成果以下:
上面我們再來看一個 HelloWord 級其余 Java Socket 通訊的例子。通信進程:
先啟動 Server 端,進入一個逝世輪回以便一向監聽某端口能否有銜接要求。然後運轉 Client 端,客戶端收回銜接要求,辦事端監聽到此次要求後向客戶端發還接收新聞,銜接樹立,啟動一個線程行止理此次要求,然後持續逝世輪回監聽其他要求。客戶端輸出字符串後按回車鍵,向辦事器發送數據。辦事器讀取數據後答復客戶端數據。此次要求處置終了,啟動的線程滅亡。假如客戶端吸收到 "OK" 以外的前往數據,會再次發送銜接要求並發送數據,辦事器會為此次銜接再次啟動一個線程來停止呼應。。。直到當客戶端吸收到的前往數據為 "OK" 時,客戶端加入。
辦事端源代碼:
package com.defonds.socket.begin; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server { public static final int PORT = 12345;//監聽的端標語 public static void main(String[] args) { System.out.println("辦事器啟動...\n"); Server server = new Server(); server.init(); } public void init() { try { ServerSocket serverSocket = new ServerSocket(PORT); while (true) { // 一旦有梗塞, 則表現辦事器與客戶端取得了銜接 Socket client = serverSocket.accept(); // 處置此次銜接 new HandlerThread(client); } } catch (Exception e) { System.out.println("辦事器異常: " + e.getMessage()); } } private class HandlerThread implements Runnable { private Socket socket; public HandlerThread(Socket client) { socket = client; new Thread(this).start(); } public void run() { try { // 讀取客戶端數據 DataInputStream input = new DataInputStream(socket.getInputStream()); String clientInputStr = input.readUTF();//這裡要留意和客戶端輸入流的寫辦法對應,不然會拋 EOFException // 處置客戶端數據 System.out.println("客戶端發過去的內容:" + clientInputStr); // 向客戶端答復信息 DataOutputStream out = new DataOutputStream(socket.getOutputStream()); System.out.print("請輸出:\t"); // 發送鍵盤輸出的一行 String s = new BufferedReader(new InputStreamReader(System.in)).readLine(); out.writeUTF(s); out.close(); input.close(); } catch (Exception e) { System.out.println("辦事器 run 異常: " + e.getMessage()); } finally { if (socket != null) { try { socket.close(); } catch (Exception e) { socket = null; System.out.println("辦事端 finally 異常:" + e.getMessage()); } } } } } }
客戶端源代碼:
package com.defonds.socket.begin; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class Client { public static final String IP_ADDR = "localhost";//辦事器地址 public static final int PORT = 12345;//辦事器端標語 public static void main(String[] args) { System.out.println("客戶端啟動..."); System.out.println("當吸收到辦事器端字符為 \"OK\" 的時刻, 客戶端將終止\n"); while (true) { Socket socket = null; try { //創立一個流套接字並將其銜接到指定主機上的指定端標語 socket = new Socket(IP_ADDR, PORT); //讀取辦事器端數據 DataInputStream input = new DataInputStream(socket.getInputStream()); //向辦事器端發送數據 DataOutputStream out = new DataOutputStream(socket.getOutputStream()); System.out.print("請輸出: \t"); String str = new BufferedReader(new InputStreamReader(System.in)).readLine(); out.writeUTF(str); String ret = input.readUTF(); System.out.println("辦事器端前往過去的是: " + ret); // 如吸收到 "OK" 則斷開銜接 if ("OK".equals(ret)) { System.out.println("客戶端將封閉銜接"); Thread.sleep(500); break; } out.close(); input.close(); } catch (Exception e) { System.out.println("客戶端異常:" + e.getMessage()); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { socket = null; System.out.println("客戶端 finally 異常:" + e.getMessage()); } } } } } }
留意: Socket 輸入流寫數據辦法是 writeUTF 時,輸出流讀取相干數據要用 readUTF。不然會拋 EOFException 異常。