Java--Socket通訊(雙向)__Java

來源:互聯網
上載者:User

建立兩個工程,一個用戶端,一個服務端,先啟動服務端再啟動用戶端

兩個工程的讀寫操作線程類基本上完全相同

 

 

服務端:

 

import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class Server {public static final int PORT = 8000;//監聽的連接埠號碼   public static void main(String[] args) {  Server server = new Server();  server.init();  }  public void init() {  ServerSocket serverSocket = null;try {  serverSocket = new ServerSocket(PORT);  while (true) {  Socket client = serverSocket.accept();  //一個用戶端串連就開戶兩個線程處理讀寫  new Thread(new ReadHandlerThread(client)).start();  new Thread(new WriteHandlerThread(client)).start(); }  } catch (Exception e) {  e.printStackTrace();  } finally{try {if(serverSocket != null){serverSocket.close();}} catch (IOException e) {e.printStackTrace();}}}  }  /* *處理讀操作的線程  */class ReadHandlerThread implements Runnable{private Socket client;public ReadHandlerThread(Socket client) {this.client = client;}@Overridepublic void run() {DataInputStream dis = null;try{while(true){//讀取用戶端資料  dis = new DataInputStream(client.getInputStream());String reciver = dis.readUTF();System.out.println("用戶端發過來的內容:" + reciver); }}catch(Exception e){e.printStackTrace();}finally{try {if(dis != null){dis.close();}if(client != null){client = null;}} catch (IOException e) {e.printStackTrace();}}}}/* * 處理寫操作的線程 */class WriteHandlerThread implements Runnable{private Socket client;public WriteHandlerThread(Socket client) {this.client = client;}@Overridepublic void run() {DataOutputStream dos = null;BufferedReader br = null;try{while(true){//向用戶端回複資訊  dos = new DataOutputStream(client.getOutputStream());  System.out.print("請輸入:\t");  // 鍵盤錄入  br = new BufferedReader(new InputStreamReader(System.in));String send = br.readLine();  //發送資料dos.writeUTF(send);  }}catch(Exception e){e.printStackTrace();}finally{try {if(dos != null){dos.close();}if(br != null){br.close();}if(client != null){client = null;}} catch (IOException e) {e.printStackTrace();}}}}


 

 

用戶端:

 

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 = "localhost";//伺服器位址 public static final int PORT = 8000;//伺服器連接埠號碼  public static void main(String[] args) {  handler(); }private static void handler(){try {//執行個體化一個Socket,並指定伺服器位址和連接埠Socket client = new Socket(IP, PORT);//開啟兩個線程,一個負責讀,一個負責寫new Thread(new ReadHandlerThread(client)).start();new Thread(new WriteHandlerThread(client)).start();} catch (Exception e) {e.printStackTrace();}}}  /* *處理讀操作的線程  */class ReadHandlerThread implements Runnable{private Socket client;public ReadHandlerThread(Socket client) {this.client = client;}@Overridepublic void run() {DataInputStream dis = null;try {while(true){//讀取伺服器端資料  dis = new DataInputStream(client.getInputStream());String receive = dis.readUTF();   System.out.println("伺服器端返回過來的是: " + receive);  }} catch (IOException e) {e.printStackTrace();} finally{try {if(dis != null){dis.close();}if(client != null){client = null;}} catch (IOException e) {e.printStackTrace();}}}}/* * 處理寫操作的線程 */class WriteHandlerThread implements Runnable{private Socket client;public WriteHandlerThread(Socket client) {this.client = client;}@Overridepublic void run() {DataOutputStream dos = null;BufferedReader br = null;try {while(true){//取得輸出資料流dos = new DataOutputStream(client.getOutputStream());System.out.print("請輸入: \t");  //鍵盤錄入br = new BufferedReader(new InputStreamReader(System.in));String send = br.readLine();  //發送資料dos.writeUTF(send);  }} catch (IOException e) {e.printStackTrace();}  finally{try{if(dos != null){dos.close();}if(br != null){br.close();}if(client != null){client = null;}}catch(Exception e){e.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.