3.2 組合輸入輸出資料流
Java中與流相關的類可以組合起來從而提供強大的功能。我們可以將一個Socket執行個體的OutputStream封裝在一個BufferedOutputStream執行個體中,這樣可以先將位元組暫時緩衝在一起,然後再一次全部發送到底層的通訊通道中,以提高程式的效能。我們還能再將這個BufferedOutputStream執行個體包裹在一個DataOutputStream執行個體中,以實現發送基礎資料型別 (Elementary Data Type)的功能。以下是實現這種組合的代碼:
Socket socket = new Socket(server, port);DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
下面是一個組合I/O流的例子
完整代碼如下所示
伺服器端
package com.suifeng.tcpip.chapter3;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketAddress;public class TCPEchoServer{public static void main(String[] args) throws IOException{if (args.length != 1){throw new IllegalArgumentException("Parameter:<Port>");}// 擷取伺服器的連接埠int serverPort = Integer.parseInt(args[0]);// 建立用於用戶端串連的SocketServer執行個體ServerSocket server = new ServerSocket(serverPort);System.out.println("Server has started!!!!\nWaiting data from client.");while (true){System.out.println("正在等待用戶端發送資料");Socket socket = server.accept();SocketAddress address = socket.getRemoteSocketAddress();System.out.println("Handling client at " + address);// 組合輸入資料流DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));// 讀取一個double值double value = dis.readDouble();System.out.println("Server received Double Value=" + value);// 讀取一個Int值int value2 = dis.readInt();System.out.println("Server received Int Value=" + value2);// 讀取一個Long值long value3 = dis.readLong();System.out.println("Server received Long Value=" + value3);System.out.println("伺服器端正在發送資料.....");DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));out.writeLong(value3);out.writeDouble(value);out.writeInt(value2);System.out.println("伺服器端正在發送資料結束");out.flush();socket.close();}}}
用戶端
package com.suifeng.tcpip.chapter3;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.Socket;import java.net.UnknownHostException;public class TCPEchoClient{/** * @param args * @throws IOException * @throws UnknownHostException */public static void main(String[] args) throws UnknownHostException,IOException{if (args.length < 1 || args.length > 2){throw new IllegalArgumentException("Parameter(s):<Server> [<Port>]");}// SerName or IP AddressString server = args[0];// 擷取連接埠號碼int serverPort = (args.length == 2) ? Integer.parseInt(args[1]) : 7;// 使用指定的伺服器和連接埠建立SocketSocket socket = new Socket(server, serverPort);System.out.println("Connected to server ..... Sending echo String");System.out.println("用戶端正在發送資料.....");// 組合輸出資料流DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));System.out.println("Client Send Double Value=3.14");// 先發送一個double值out.writeDouble(3.14);System.out.println("Client Send Int Value=50");// 再發送一個int值out.writeInt(50);System.out.println("Client Send Long Value=123333333333333333L");// 最後發送一個long值out.writeLong(123333333333333333L);out.flush();System.out.println("發送資料結束");System.out.println("用戶端正在接收資料.....");DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));System.out.println("Client received Long Value=" + dis.readLong());System.out.println("Client received Double Value=" + dis.readDouble());System.out.println("Client received Int Value=" + dis.readInt());System.out.println("用戶端正在接收資料結束");socket.close();}}
先啟動伺服器端,監聽39393連接埠
啟動用戶端,想39393連接埠發送資料
再次查看伺服器端