用socket類比實現全雙工系統通訊,socket全雙工系統通訊

來源:互聯網
上載者:User

用socket類比實現全雙工系統通訊,socket全雙工系統通訊

//源碼下載 http://download.csdn.net/detail/qq_24054661/9802994
package com.tsocket.test;import java.io.*;import java.net.*;public class Client {public static void main(String[] args) throws UnknownHostException, IOException {Socket socket = new Socket("169.254.87.76",8888);new Thread(new Send(socket)).start();new Thread(new Receive(socket)).start();/*String info = "用戶端要串連!";BufferedWriter br = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));br.write(info);br.newLine();br.flush();*/}}
package com.tsocket.test;import java.io.*;import java.net.*;public class Server {public static void main(String[] args) throws IOException {ServerSocket server = new ServerSocket(8888);Socket socket = server.accept();new Thread(new Send(socket)).start();new Thread(new Receive(socket)).start();//new Server().start();/*Socket socket = server.accept();DataInputStream dis = new DataInputStream(socket.getInputStream());DataOutputStream dos = new DataOutputStream(socket.getOutputStream());while(true){String info = dis.readUTF();System.out.println("伺服器-->"+info);dos.writeUTF("伺服器:--->"+info);}*/}public void start() throws IOException{ServerSocket server = new ServerSocket(8888);while(true){Socket socket = server.accept();MyChannel channel = new MyChannel(socket);new Thread(channel).start();}}private class MyChannel implements Runnable{private BufferedReader br ;private DataInputStream dis;private DataOutputStream dos;private boolean isRunning = true;private String Msg = null;@Overridepublic void run() {while(isRunning){Receive();Send();}}private String getMsg(){try {return br.readLine();} catch (IOException e) {return null;}}public MyChannel (Socket socket){br = new BufferedReader(new InputStreamReader(System.in));try {dis = new DataInputStream(socket.getInputStream());dos = new DataOutputStream(socket.getOutputStream());} catch (IOException e) {CloseUtil.closeAll(dis,dos); isRunning = false;}}private String Receive(){try{Msg = dis.readUTF();}catch(Exception e){CloseUtil.closeAll(dis,dos);isRunning = false;}return Msg;}private void Send(){String Msg=null;try {Msg = br.readLine();} catch (IOException e1) {//e1.printStackTrace();}if(Msg==null||Msg.equals("")){return;}try{dos.writeUTF(Msg);dos.flush();}catch(Exception e){CloseUtil.closeAll(dis,dos);isRunning = false;}}}}



package com.tsocket.test;import java.io.*;import java.net.*;public class Receive implements Runnable {private boolean isRunning = true;private DataInputStream dis;public Receive(Socket s){try {dis = new DataInputStream(s.getInputStream());} catch (IOException e) {isRunning = false;CloseUtil.closeAll(dis);}}public String receive(){String Msg="";try {Msg=dis.readUTF();} catch (IOException e) {isRunning = false;CloseUtil.closeAll(dis);}return Msg;}@Overridepublic void run() {while(isRunning){System.out.println(receive());}}}

 

package com.tsocket.test;import java.io.Closeable;public class CloseUtil {public static void closeAll(Closeable...io){for(Closeable temp :io){try{if(null!=temp){temp.close();}}catch(Exception e){e.printStackTrace();}}}}

 

package com.tsocket.test;import java.io.*;import java.net.*;public class Send implements Runnable {private BufferedReader br ;private DataOutputStream dos;private boolean isRunning = true;public Send(Socket socket){br = new BufferedReader(new InputStreamReader(System.in));try {dos = new DataOutputStream(socket.getOutputStream());} catch (IOException e) {isRunning = false;CloseUtil.closeAll(dos,br);}}private String getMsg(){try {return br.readLine();} catch (IOException e) {return null;}}public void send(){String Msg = getMsg();if(Msg!=null&&!Msg.equals("")){try {dos.writeUTF(Msg);dos.flush();} catch (IOException e) {isRunning = false;CloseUtil.closeAll(dos,br);}}}@Overridepublic void run() {while(isRunning){send();}}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.