DataInput與DataOutput

習慣了讀寫字符串時用PrintWriter和BufferedReaderjava

PrintWriter(BufferedWriter(OutputStreamWriter(OutputStream)))小程序

BufferedReader(InputStreamReader(InputStream))socket

這兩天給別人看項目代碼的時候看到程序裏用了readUTF和writeUTF這兩個方法,頓時從塵封的記憶中喚起DataInput和DataOutput這兩個接口。好久不用,我都快把它倆給忘了。ide

項目運行的時候老是會拋出EOFException,不明白怎麼回事,因而看了看JDK文檔,本身寫了一段小程序測試了一下,猜想readUTF方法彷佛是經過拋出EOFException來代表到達數據的末尾,如同bufferedReader.readLine()經過返回null代表到達數據的結尾同樣,只是表達的方式不一樣而已。所以只須要經過檢查EOFException異常來結束程序便可。測試

EOFException文檔中提到:this

    此異常主要被數據輸入流用來代表到達流的末尾。注意,其餘許多輸入操做返回一個特殊值表示到達流的末尾,而不是拋出異常。.net

如下是我寫的測試代碼:(Java6)code

服務端:server

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Socket socket = null;
		DataInputStream in = null;
		DataOutputStream out = null;
		try {
			socket = new Socket("localhost", 10011);
			out = new DataOutputStream(socket.getOutputStream());
			in = new DataInputStream(socket.getInputStream());
			for (int i = 0; i < 100; i++) {
				out.writeUTF("aaa1");
				out.flush();
				System.out.println(in.readUTF());
			}
			in.close();
			out.close();
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

客戶端:接口

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Server {
	static class ReadTask implements Runnable {
		private final Socket so;

		public ReadTask(Socket so) {
			this.so = so;
		}

		@Override
		public void run() {
			DataInputStream in = null;
			DataOutputStream out = null;
			try {
				in = new DataInputStream(this.so.getInputStream());
				out = new DataOutputStream(this.so.getOutputStream());
				System.out.println("開始處理");
				while (true) {
					String cmd = in.readUTF();
					// do some work
					TimeUnit.MILLISECONDS.sleep(10);
					out.writeUTF("result -- " + cmd);
				}
			} catch (EOFException e) {
				System.out.println("處理完成");
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (out != null) {
					try {
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (this.so != null) {
					try {
						this.so.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		ExecutorService exec = Executors.newCachedThreadPool();
		ServerSocket server = null;
		try {
			server = new ServerSocket(10011);
			System.out.println("server started");
			while (true) {
				Socket socket = server.accept();
				ReadTask task = new ReadTask(socket);
				exec.execute(task);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			exec.shutdown();
			exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
			if (server != null) {
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
相關文章
相關標籤/搜索