java IO 之 BIO

BIO 阻塞式IO.一个简单的TCP+BIO demo如下:

 

 

 Server端:

 

package tcpbio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerDemo {

	public  static void main(String[]args) throws IOException, InterruptedException{
		
		int PORT = 11111;
	
		int msgcount =0;
		ServerSocket serverSocket = new ServerSocket(PORT);
		
		System.out.println("Server starts,server is listenning to 11111 ");
		Socket socket =  serverSocket.accept();
		
		BufferedReader msgFromClient =  new BufferedReader(new InputStreamReader(socket.getInputStream()));
		
		
		PrintWriter out = new PrintWriter(socket.getOutputStream());
		
		while(true){
			String clientInput  = msgFromClient.readLine();
			
			if(clientInput == null ){
			
				Thread.sleep(1000);
				
				continue;
			}
			if("over".equalsIgnoreCase(clientInput.trim())){
				
				msgFromClient.close();
				out.close();
				serverSocket.close();
				
				System.out.println("Server has been shutdown!");
				System.exit(0);
			}
			else{
				//模拟耗时的业务操作
				Thread.sleep(3000);
				out.println("the server syas "+clientInput);
				out.flush();
				msgcount++;
				
				Thread.sleep(100);
				
			}
			
		}
		
	}
}
 

 

 Client端:
package tcpbio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientDemo {

	public static void main(String[]args) throws UnknownHostException, IOException{
		
		String HOST_ADDRESS = "127.0.0.1";
		int PORT = 11111;
	
		
		//建立与服务器端通信套接字
		Socket socket = new Socket(HOST_ADDRESS,PORT);
		//获取服务端返回信息
		BufferedReader msgFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		//建立写入流 向socket写入输入
		PrintWriter out = new PrintWriter(socket.getOutputStream());
		
		System.out.println("Client start");
		
		while(true){
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			long start = System.currentTimeMillis();
			//读取输入
			String clientInput = input.readLine();
			
			//匹配退出
			if(clientInput ==null ||clientInput.equalsIgnoreCase("over")){
				
				clientInput = "over";//写入退出命令  以令服务器关闭监听的serverSocket
				out.println(clientInput);
				out.flush();
				out.close();
				input.close();
				socket.close();
				
				//tag = false;
				break;
			}
			//将输入写入
			out.println(clientInput);
			out.flush();
			String responseMsg = msgFromServer.readLine();
			long end = System.currentTimeMillis();
			System.out.println("Recieving  msg from Server:"+responseMsg);
			
			System.out.println("This process totally costs: "+(end-start) +" milliSeconds");
		}
		
	}
}
 

 

说明:这里通过简单的server端工作线程在处理得到的输入时sleep 3秒钟来模仿复杂的业务耗时操作,这里通过client端的计时可以发现,client端的workThread一直要等到server端的响应结果,这样的话,十分耗时,资源利用率太低。但它的好处在于,当服务端有响应返回客户端的时候能继续进行业务操作。这种IO方式适合于CPU等资源非常充分并且这个业务请求时唯一的业务操作的情况。因为这种情况下,即使你切走cpu也没有其他事情可做。

你可能感兴趣的:(java,thread,工作,.net,socket)