(转)Socket & 多线程,Socket & 多线程

看到博客首页有一篇很好的原创文章,关于io和newio的用法和比较,自己收藏了,

1.  前言

现在很多做网络通讯中间代理层的通讯都是使用Java1.4以后推出的NIO进行编写,现在还有很多开源的框架也是封装了NIO的书写细节来帮助大家简写异步非阻塞通讯服务。像MySql的代理中间件amoeba-mysql-proxy就是采用NIO的方式处理client端过来的request,之后与Mysql-Server层的通讯也是采用NIO进行命令消息发送的。再看咱们JavaEye首页介绍的项目xmemcached,其中作者Dennis是其xmemcached的开发人,他也是通过NIO的方式与memcachedServer进行异步通讯的,Dennis的另一个项目yanf4j就是一个NIO框架,xmemcache也是借助这个NIO框架实现的异步非阻塞方式的网络通讯,ApacheMINA框架都是NIO的封装再实现。

那么我们就来回顾一下以前的处理方式,来看看为什么现在要使用NIO来进行异步非阻塞方式的通讯吧,网上很多文章都是几句话将NIO和原始的socket通讯的优劣一带而过,我们这次用一个简单的下载大文件的网络服务程序进行说明。使用3种模式来说明,分别是同步单独线程服务运行模式、传统阻塞多线程模式、使用NIO异步非阻塞模式。

我们设置服务器上有一个1.81GB的电影,格式为RMVB。使用Server进行服务监听,客户端请求到Server,建立网络通讯,进行电影下载。

2.  同步单线程阻塞

使用同步单线程下载,是最原始的socket通讯,服务端的代码如下

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package server;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import java.net.ServerSocket;   
  7. import java.net.Socket;   
  8.   
  9. /**  
  10.  * liuyan  
  11.  */  
  12. public class FilmServer {   
  13.   
  14.     public static void main(String[] args) {   
  15.         FilmServer ms = new FilmServer();   
  16.         try {   
  17.             ms.server();   
  18.         } catch (Exception e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22.   
  23.     /**  
  24.      * 服务器端响应请求  
  25.      *   
  26.      * @throws Exception  
  27.      */  
  28.     public void server() throws Exception {   
  29.   
  30.         // 0.建立服务器端的server的socket   
  31.         ServerSocket ss = new ServerSocket(9999);   
  32.   
  33.         while (true) {   
  34.   
  35.             // 1.打开socket连接   
  36.             // 等待客户端的请求   
  37.             final Socket server = ss.accept();   
  38.   
  39.             System.out.println("服务-----------请求开始start");   
  40.   
  41.             // 2.打开socket的流信息,准备下面的操作   
  42.             final InputStream is = server.getInputStream();   
  43.             byte b[] = new byte[1024];   
  44.   
  45.             int readCount = is.read(b);   
  46.   
  47.             String str = new String(b);   
  48.   
  49.             str = str.trim();   
  50.   
  51.             final String serverFileName = str;   
  52.   
  53.             // 3.对流信息进行读写操作   
  54.             System.out.println("客户端传过来的信息是:" + str);   
  55.   
  56.             System.out.println("线程" + Thread.currentThread().getName() + "启动");   
  57.   
  58.             try {   
  59.   
  60.                 FileInputStream fileInputStream = new FileInputStream(   
  61.                         serverFileName);   
  62.   
  63.                 // 3.1 服务器回复客户端信息(response)   
  64.                 OutputStream os = server.getOutputStream();   
  65.   
  66.                 byte[] bfile = new byte[1024];   
  67.   
  68.                 // 往客户端写   
  69.                 while (fileInputStream.read(bfile) > 0) {   
  70.                     os.write(bfile);   
  71.                 }   
  72.   
  73.                 fileInputStream.close();   
  74.   
  75.                 os.close();   
  76.   
  77.                 // 4.关闭socket   
  78.                 // 先关闭输入流   
  79.                 is.close();   
  80.   
  81.                 // 最后关闭socket   
  82.                 server.close();   
  83.   
  84.             } catch (Exception e) {   
  85.                 // TODO Auto-generated catch block   
  86.                 e.printStackTrace();   
  87.             }   
  88.   
  89.             System.out.println("服务-----------请求结束over");   
  90.         }   
  91.   
  92.     }   
  93.   
  94. }</SPAN>  
package server;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * liuyan
 */
public class FilmServer {

	public static void main(String[] args) {
		FilmServer ms = new FilmServer();
		try {
			ms.server();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 服务器端响应请求
	 * 
	 * @throws Exception
	 */
	public void server() throws Exception {

		// 0.建立服务器端的server的socket
		ServerSocket ss = new ServerSocket(9999);

		while (true) {

			// 1.打开socket连接
			// 等待客户端的请求
			final Socket server = ss.accept();

			System.out.println("服务-----------请求开始start");

			// 2.打开socket的流信息,准备下面的操作
			final InputStream is = server.getInputStream();
			byte b[] = new byte[1024];

			int readCount = is.read(b);

			String str = new String(b);

			str = str.trim();

			final String serverFileName = str;

			// 3.对流信息进行读写操作
			System.out.println("客户端传过来的信息是:" + str);

			System.out.println("线程" + Thread.currentThread().getName() + "启动");

			try {

				FileInputStream fileInputStream = new FileInputStream(
						serverFileName);

				// 3.1 服务器回复客户端信息(response)
				OutputStream os = server.getOutputStream();

				byte[] bfile = new byte[1024];

				// 往客户端写
				while (fileInputStream.read(bfile) > 0) {
					os.write(bfile);
				}

				fileInputStream.close();

				os.close();

				// 4.关闭socket
				// 先关闭输入流
				is.close();

				// 最后关闭socket
				server.close();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println("服务-----------请求结束over");
		}

	}

}

 服务端这么写代码会有什么问题?咱们先来看客户端代码,之后运行后就知道了。

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package client;   
  2.   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.io.OutputStream;   
  7. import java.net.Socket;   
  8. import java.net.UnknownHostException;   
  9.   
  10. /**  
  11.  * liuyan  
  12.  * @version 1.0  
  13.  */  
  14. public class FilmClient {   
  15.     public static void main(String[] args) {   
  16.         for (int i = 1; i <= 2; i++) {   
  17.             Client client = new Client();   
  18.             client.i = i;   
  19.             client.start();   
  20.         }   
  21.     }   
  22. }   
  23.   
  24. class Client extends Thread {   
  25.   
  26.     int i;   
  27.   
  28.     @Override  
  29.     public void run() {   
  30.   
  31.         // 1.建立scoket连接   
  32.         Socket client;   
  33.         try {   
  34.             client = new Socket("127.0.0.1"9999);   
  35.   
  36.             // 2.打开socket的流信息,准备下面的操作   
  37.             OutputStream os = client.getOutputStream();   
  38.   
  39.             // 3.写信息   
  40.             os.write(("d://film//2.rmvb").getBytes());   
  41.                
  42.             String filmName = "c://io"+i+".rmvb";   
  43.                
  44.             FileOutputStream fileOutputStream = new FileOutputStream(filmName);   
  45.   
  46.             // 3.1接收服务器端的反馈   
  47.             InputStream is = client.getInputStream();   
  48.             byte b[] = new byte[1024];   
  49.                
  50.             while(is.read(b)>0){   
  51.                 fileOutputStream.write(b);   
  52.             }   
  53.   
  54.             // 4.关闭socket   
  55.             // 先关闭输出流   
  56.             os.close();   
  57.   
  58.             // 最后关闭socket   
  59.             client.close();   
  60.         } catch (UnknownHostException e) {   
  61.             // TODO Auto-generated catch block   
  62.             e.printStackTrace();   
  63.         } catch (IOException e) {   
  64.             // TODO Auto-generated catch block   
  65.             e.printStackTrace();   
  66.         }   
  67.     }   
  68.   
  69. }</SPAN>  
package client;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * liuyan
 * @version 1.0
 */
public class FilmClient {
	public static void main(String[] args) {
		for (int i = 1; i <= 2; i++) {
			Client client = new Client();
			client.i = i;
			client.start();
		}
	}
}

class Client extends Thread {

	int i;

	@Override
	public void run() {

		// 1.建立scoket连接
		Socket client;
		try {
			client = new Socket("127.0.0.1", 9999);

			// 2.打开socket的流信息,准备下面的操作
			OutputStream os = client.getOutputStream();

			// 3.写信息
			os.write(("d://film//2.rmvb").getBytes());
			
			String filmName = "c://io"+i+".rmvb";
			
			FileOutputStream fileOutputStream = new FileOutputStream(filmName);

			// 3.1接收服务器端的反馈
			InputStream is = client.getInputStream();
			byte b[] = new byte[1024];
			
			while(is.read(b)>0){
				fileOutputStream.write(b);
			}

			// 4.关闭socket
			// 先关闭输出流
			os.close();

			// 最后关闭socket
			client.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 客户端启动了2个线程进行下载电影的工作,先启动服务端,再运行客户端,会看笔者本地的硬盘C分区到有如下效果。

 可以看到线程2的下载任务一直是0字节,等第一个线程下载完成后呢,线程2的下载任务才能进行。



 

服务端的代码造成的问题就是使用传统的sokect网络通讯,那么另一个客户端的线程请求到server端的时候就发生了阻塞的情况,也就是说,服务端相当一个厕所,厕所就有只有一个坑位,来了一个人,相当于客户端请求,那这个人相当于就把坑位给占了,write操作和read操作会阻塞,这个人还没解决完问题呢,下个人就来了,没办法,哥们儿先在门外等等啊,等前一个客户爽完了再给您提供服务好吧。那么如何解决这个占着坑位不让别人用的情况呢?

3.  阻塞的多线程

为了解决以上问题,那么之后很多Server肯定不可能像以上程序那么做,不过以前很多Server都是基于单线程服务改造一下,做成多线程的Server的通讯,修改一下上面的Server代码,如下

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package server;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import java.net.ServerSocket;   
  7. import java.net.Socket;   
  8.   
  9. /**  
  10.  *   
  11.  */  
  12. public class FilmServerNewThread {   
  13.   
  14.     public static void main(String[] args) {   
  15.         FilmServerNewThread ms = new FilmServerNewThread();   
  16.         try {   
  17.             ms.server();   
  18.         } catch (Exception e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22.   
  23.     /**  
  24.      * 服务器端响应请求  
  25.      *   
  26.      * @throws Exception  
  27.      */  
  28.     public void server() throws Exception {   
  29.   
  30.         // 0.建立服务器端的server的socket   
  31.         ServerSocket ss = new ServerSocket(9999);   
  32.   
  33.         while (true) {   
  34.   
  35.             // 1.打开socket连接   
  36.             // 等待客户端的请求   
  37.             final Socket server = ss.accept();   
  38.   
  39.             System.out.println("服务-----------请求开始start");   
  40.   
  41.             // 2.打开socket的流信息,准备下面的操作   
  42.             final InputStream is = server.getInputStream();   
  43.             byte b[] = new byte[1024];   
  44.   
  45.             int readCount = is.read(b);   
  46.   
  47.             String str = new String(b);   
  48.   
  49.             str = str.trim();   
  50.   
  51.             final String serverFileName = str;   
  52.   
  53.             // 3.对流信息进行读写操作   
  54.             System.out.println("客户端传过来的信息是:" + str);   
  55.   
  56.             if (readCount > 0) {   
  57.                 new Thread() {   
  58.   
  59.                     @Override  
  60.                     public void run() {   
  61.   
  62.                         System.out.println("线程"  
  63.                                 + Thread.currentThread().getName() + "启动");   
  64.   
  65.                         try {   
  66.   
  67.                             FileInputStream fileInputStream = new FileInputStream(   
  68.                                     serverFileName);   
  69.   
  70.                             // 3.1 服务器回复客户端信息(response)   
  71.                             OutputStream os = server.getOutputStream();   
  72.   
  73.                             byte[] bfile = new byte[1024];   
  74.   
  75.                             // 往客户端写   
  76.                             while (fileInputStream.read(bfile) > 0) {   
  77.                                 os.write(bfile);   
  78.                             }   
  79.   
  80.                             fileInputStream.close();   
  81.   
  82.                             os.close();   
  83.   
  84.                             // 4.关闭socket   
  85.                             // 先关闭输入流   
  86.                             is.close();   
  87.   
  88.                             // 最后关闭socket   
  89.                             server.close();   
  90.   
  91.                         } catch (Exception e) {   
  92.                             // TODO Auto-generated catch block   
  93.                             e.printStackTrace();   
  94.                         }   
  95.                     }   
  96.                 }.start();   
  97.             }   
  98.   
  99.             System.out.println("服务-----------请求结束over");   
  100.         }   
  101.   
  102.     }   
  103. }</SPAN>  
package server;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 
 */
public class FilmServerNewThread {

	public static void main(String[] args) {
		FilmServerNewThread ms = new FilmServerNewThread();
		try {
			ms.server();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 服务器端响应请求
	 * 
	 * @throws Exception
	 */
	public void server() throws Exception {

		// 0.建立服务器端的server的socket
		ServerSocket ss = new ServerSocket(9999);

		while (true) {

			// 1.打开socket连接
			// 等待客户端的请求
			final Socket server = ss.accept();

			System.out.println("服务-----------请求开始start");

			// 2.打开socket的流信息,准备下面的操作
			final InputStream is = server.getInputStream();
			byte b[] = new byte[1024];

			int readCount = is.read(b);

			String str = new String(b);

			str = str.trim();

			final String serverFileName = str;

			// 3.对流信息进行读写操作
			System.out.println("客户端传过来的信息是:" + str);

			if (readCount > 0) {
				new Thread() {

					@Override
					public void run() {

						System.out.println("线程"
								+ Thread.currentThread().getName() + "启动");

						try {

							FileInputStream fileInputStream = new FileInputStream(
									serverFileName);

							// 3.1 服务器回复客户端信息(response)
							OutputStream os = server.getOutputStream();

							byte[] bfile = new byte[1024];

							// 往客户端写
							while (fileInputStream.read(bfile) > 0) {
								os.write(bfile);
							}

							fileInputStream.close();

							os.close();

							// 4.关闭socket
							// 先关闭输入流
							is.close();

							// 最后关闭socket
							server.close();

						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}.start();
			}

			System.out.println("服务-----------请求结束over");
		}

	}
}

 以上的Server就是在原始的socket基础上加了线程,每一个Client请求过来后,整个Server主线程不必处于阻塞状态,接收请求后直接另起一个新的线程来处理和客户端的交互,就是往客户端发送二进制包。这个在新线程中虽然阻塞,但是对于服务主线程没有阻塞的影响,主线程依然通过死循环监听着客户端的一举一动。另一个客户端的线程发起请求后就再起一个新的线程对象去为客户端服务。执行效果如下

 

2个线程互不影响,各自下载各自的。当然从非常严格的意义来讲,str变量在十分高并发的情况下有线程安全问题,这个咱暂且忽略,就着眼于低并发的情况。这个问题是什么呢,就是如果客户端请求比较多了,那么为每一个客户端开辟一个新的线程对象来处理网络传输的请求,需要创建个线程对象,而且这个线程对象从时间上来讲还是处于长连接,这个就比较消费系统资源,这个打开进程管理器就可以看到。而且每一个线程内部都是阻塞的,也没有说完全利用好这个新创建的线程。还拿刚才上厕所举例子,好比现在不止一个坑位了,来了一个用户我这边就按照工程师的厕所坑位图建立一个新的坑位,客户来了,不用等待老坑位,用新创建的坑位就行了。等那个老坑位用完了,自然有垃圾回收器去消灭那个一次性的坑位的,腾出资源位置为了建立新的坑位。长时间连接的意思,相当于这个人上厕所的时间非常长,便秘??需要拉一天才能爽完……老的坑位一时半会儿回收不了,新的坑位需要有空间为其建造茅房以便满足客户端的“急切方便”需要。久而久之,线程数目一多,系统就挂了的概率就增多了(谁也别想上,全玩完了)。

4.  异步非阻塞

使用JDK1.4NIO可以适当的解决上面的问题,异步 I/O 是一种 没有阻塞地读写数据的方法。通常,在代码进行 read() 调用时,代码会阻塞直至有可供读取的数据。同样, write() 调用将会阻塞直至数据能够写入。异步 I/O 调用不会阻塞。相反,您将注册对特定 I/O 事件的兴趣可读的数据的到达、新的套接字连接,等等,而在发生这样的事件时,系统将会告诉您。异步 I/O 的一个优势在于,它允许您同时根据大量的输入和输出执行 I/O。同步程序常常要求助于轮询,或者创建许许多多的线程以处理大量的连接。使用异步 I/O,您可以监听任何数量的通道上的事件,不用轮询,也不用额外的线程。还是举上公共厕所例子,虽然这个例子有点臭臭的。您现在有“便便”的需求了,不用那么麻烦,看看公共厕所是否有人占领,也不用给您另起个新坑位,您就拿一根我们服务端定制的容器和一个很粗管子,这个坐便器的大小因您那个地方的尺寸而定,坐便器往您的那个地方一放,再将坐便器和管子一连接,OK,您就敞开了“爽”吧。不用担心,这个管子自然会连接到相应的肥料厂家,将您的排泄物有效回收加以利用的。您完了事,擦擦屁股,关上管子该干嘛还干嘛就行了。另一个人也有这个需求,没问题,每个要我们提供服务的人都用这根管子,和自己的坐便器就行了,管子很粗,谁来连这个管子都行,有多少都行啊。下面我们来看基于NIO的网络下载程序

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package server;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.IOException;   
  5. import java.net.InetSocketAddress;   
  6. import java.nio.ByteBuffer;   
  7. import java.nio.CharBuffer;   
  8. import java.nio.channels.FileChannel;   
  9. import java.nio.channels.SelectionKey;   
  10. import java.nio.channels.Selector;   
  11. import java.nio.channels.ServerSocketChannel;   
  12. import java.nio.channels.SocketChannel;   
  13. import java.nio.charset.Charset;   
  14. import java.nio.charset.CharsetDecoder;   
  15. import java.util.Iterator;   
  16.   
  17. /**  
  18.  *   
  19.  * @author liuyan  
  20.  *  
  21.  */  
  22. public class NIOServer {   
  23.     static int BLOCK = 500*1024;   
  24.   
  25.     /**  
  26.      * 处理客户端的内部类,专门负责处理与用户的交互  
  27.      */  
  28.     public class HandleClient {   
  29.         protected FileChannel channel;   
  30.         protected ByteBuffer buffer;   
  31.         String filePath;   
  32.            
  33.          /**  
  34.           * 构造函数,文件的管道初始化  
  35.           * @param filePath  
  36.           * @throws IOException  
  37.           */  
  38.         public HandleClient(String filePath) throws IOException {   
  39.                
  40.             //文件的管道   
  41.             this.channel = new FileInputStream(filePath).getChannel();   
  42.                
  43.             //建立缓存   
  44.             this.buffer = ByteBuffer.allocate(BLOCK);   
  45.             this.filePath = filePath;   
  46.         }   
  47.            
  48.         /**  
  49.          * 读取文件管道中数据到缓存中  
  50.          * @return  
  51.          */  
  52.         public ByteBuffer readBlock() {   
  53.             try {   
  54.                    
  55.                 //清除缓冲区的内容,posistion设置为0,limit设置为缓冲的容量大小capacity   
  56.                 buffer.clear();   
  57.                    
  58.                 //读取   
  59.                 int count = channel.read(buffer);   
  60.                    
  61.                 //将缓存的中的posistion设置为0,将缓存中的limit设置在原始posistion位置上   
  62.                 buffer.flip();   
  63.                 if (count <= 0)   
  64.                     return null;   
  65.             } catch (IOException e) {   
  66.                 e.printStackTrace();   
  67.             }   
  68.             return buffer;   
  69.         }   
  70.            
  71.         /**  
  72.          * 关闭服务端的文件管道  
  73.          */  
  74.         public void close() {   
  75.             try {   
  76.                 channel.close();   
  77.             } catch (IOException e) {   
  78.                 e.printStackTrace();   
  79.             }   
  80.         }   
  81.     }   
  82.   
  83.     protected Selector selector;   
  84.     protected String filename = "d:\\film\\60.rmvb"// a big file   
  85.     protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);   
  86.     protected CharsetDecoder decoder;   
  87.        
  88.     //构造服务端口,服务管道等等   
  89.     public NIOServer(int port) throws IOException {   
  90.         selector = this.getSelector(port);   
  91.         Charset charset = Charset.forName("GB2312");   
  92.         decoder = charset.newDecoder();   
  93.     }   
  94.   
  95.     // 获取Selector   
  96.     //构造服务端口,服务管道等等   
  97.     protected Selector getSelector(int port) throws IOException {   
  98.         ServerSocketChannel server = ServerSocketChannel.open();   
  99.         Selector sel = Selector.open();   
  100.         server.socket().bind(new InetSocketAddress(port));   
  101.         server.configureBlocking(false);   
  102.            
  103.         //刚开始就注册链接事件   
  104.         server.register(sel, SelectionKey.OP_ACCEPT);   
  105.         return sel;   
  106.     }   
  107.   
  108.     // 服务启动的开始入口   
  109.     public void listen() {   
  110.         try {   
  111.             for (;;) {   
  112.                    
  113.                 //?   
  114.                 selector.select();   
  115.                 Iterator<SelectionKey> iter = selector.selectedKeys()   
  116.                         .iterator();   
  117.                 while (iter.hasNext()) {//首先是最先感兴趣的连接事件   
  118.                     SelectionKey key = iter.next();   
  119.                        
  120.                     //   
  121.                     iter.remove();   
  122.                        
  123.                     //处理事件   
  124.                     handleKey(key);   
  125.                 }   
  126.             }   
  127.         } catch (IOException e) {   
  128.             e.printStackTrace();   
  129.         }   
  130.     }   
  131.   
  132.     // 处理事件   
  133.     protected void handleKey(SelectionKey key) throws IOException {   
  134.         if (key.isAcceptable()) { // 接收请求   
  135.                
  136.             //允许网络连接事件   
  137.             ServerSocketChannel server = (ServerSocketChannel) key.channel();   
  138.             SocketChannel channel = server.accept();   
  139.             channel.configureBlocking(false);   
  140.                
  141.             //网络管道准备处理读事件   
  142.             channel.register(selector, SelectionKey.OP_READ);   
  143.         } else if (key.isReadable()) { // 读信息   
  144.             SocketChannel channel = (SocketChannel) key.channel();   
  145.                
  146.             //从客户端读过来的数据块   
  147.             int count = channel.read(clientBuffer);   
  148.             if (count > 0) {   
  149.                    
  150.                 //读取过来的缓存进行有效分割,posistion设置为0,保证从缓存的有效位置开始读取,limit设置为原先的posistion上   
  151.                 //这样一来从posistion~limit这段缓存数据是有效,可利用的   
  152.                 clientBuffer.flip();   
  153.                    
  154.                 //对客户端缓存块进行编码   
  155.                 CharBuffer charBuffer = decoder.decode(clientBuffer);   
  156.                 System.out.println("Client >>download>>" + charBuffer.toString());   
  157.                    
  158.                 //对网络管道注册写事件   
  159.                 SelectionKey wKey = channel.register(selector,   
  160.                         SelectionKey.OP_WRITE);   
  161.                    
  162.                 //将网络管道附着上一个处理类HandleClient,用于处理客户端事件的类   
  163.                 wKey.attach(new HandleClient(charBuffer.toString()));   
  164.             } else{   
  165.                 //如客户端没有可读事件,关闭管道   
  166.                 channel.close();   
  167.             }   
  168.                    
  169.             clientBuffer.clear();   
  170.         } else if (key.isWritable()) { // 写事件   
  171.             SocketChannel channel = (SocketChannel) key.channel();   
  172.                
  173.             //从管道中将附着处理类对象HandleClient取出来   
  174.             HandleClient handle = (HandleClient) key.attachment();   
  175.                
  176.             //读取文件管道,返回数据缓存   
  177.             ByteBuffer block = handle.readBlock();   
  178.             if (block != null){   
  179.                 //System.out.println("---"+new String(block.array()));   
  180.                    
  181.                 //写给客户端   
  182.                 channel.write(block);   
  183.             }else {   
  184.                 handle.close();   
  185.                 channel.close();   
  186.             }   
  187.         }   
  188.     }   
  189.   
  190.     public static void main(String[] args) {   
  191.         int port = 12345;   
  192.         try {   
  193.             NIOServer server = new NIOServer(port);   
  194.             System.out.println("Listernint on " + port);   
  195.             while (true) {   
  196.                 server.listen();   
  197.             }   
  198.         } catch (IOException e) {   
  199.             e.printStackTrace();   
  200.         }   
  201.     }   
  202. }</SPAN>  
package server;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Iterator;

/**
 * 
 * @author liuyan
 *
 */
public class NIOServer {
	static int BLOCK = 500*1024;

	/**
	 * 处理客户端的内部类,专门负责处理与用户的交互
	 */
	public class HandleClient {
		protected FileChannel channel;
		protected ByteBuffer buffer;
		String filePath;
		
		 /**
		  * 构造函数,文件的管道初始化
		  * @param filePath
		  * @throws IOException
		  */
		public HandleClient(String filePath) throws IOException {
			
			//文件的管道
			this.channel = new FileInputStream(filePath).getChannel();
			
			//建立缓存
			this.buffer = ByteBuffer.allocate(BLOCK);
			this.filePath = filePath;
		}
		
		/**
		 * 读取文件管道中数据到缓存中
		 * @return
		 */
		public ByteBuffer readBlock() {
			try {
				
				//清除缓冲区的内容,posistion设置为0,limit设置为缓冲的容量大小capacity
				buffer.clear();
				
				//读取
				int count = channel.read(buffer);
				
				//将缓存的中的posistion设置为0,将缓存中的limit设置在原始posistion位置上
				buffer.flip();
				if (count <= 0)
					return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
			return buffer;
		}
		
		/**
		 * 关闭服务端的文件管道
		 */
		public void close() {
			try {
				channel.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	protected Selector selector;
	protected String filename = "d:\\film\\60.rmvb"; // a big file
	protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);
	protected CharsetDecoder decoder;
	
	//构造服务端口,服务管道等等
	public NIOServer(int port) throws IOException {
		selector = this.getSelector(port);
		Charset charset = Charset.forName("GB2312");
		decoder = charset.newDecoder();
	}

	// 获取Selector
	//构造服务端口,服务管道等等
	protected Selector getSelector(int port) throws IOException {
		ServerSocketChannel server = ServerSocketChannel.open();
		Selector sel = Selector.open();
		server.socket().bind(new InetSocketAddress(port));
		server.configureBlocking(false);
		
		//刚开始就注册链接事件
		server.register(sel, SelectionKey.OP_ACCEPT);
		return sel;
	}

	// 服务启动的开始入口
	public void listen() {
		try {
			for (;;) {
				
				//?
				selector.select();
				Iterator<SelectionKey> iter = selector.selectedKeys()
						.iterator();
				while (iter.hasNext()) {//首先是最先感兴趣的连接事件
					SelectionKey key = iter.next();
					
					//
					iter.remove();
					
					//处理事件
					handleKey(key);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 处理事件
	protected void handleKey(SelectionKey key) throws IOException {
		if (key.isAcceptable()) { // 接收请求
			
			//允许网络连接事件
			ServerSocketChannel server = (ServerSocketChannel) key.channel();
			SocketChannel channel = server.accept();
			channel.configureBlocking(false);
			
			//网络管道准备处理读事件
			channel.register(selector, SelectionKey.OP_READ);
		} else if (key.isReadable()) { // 读信息
			SocketChannel channel = (SocketChannel) key.channel();
			
			//从客户端读过来的数据块
			int count = channel.read(clientBuffer);
			if (count > 0) {
				
				//读取过来的缓存进行有效分割,posistion设置为0,保证从缓存的有效位置开始读取,limit设置为原先的posistion上
				//这样一来从posistion~limit这段缓存数据是有效,可利用的
				clientBuffer.flip();
				
				//对客户端缓存块进行编码
				CharBuffer charBuffer = decoder.decode(clientBuffer);
				System.out.println("Client >>download>>" + charBuffer.toString());
				
				//对网络管道注册写事件
				SelectionKey wKey = channel.register(selector,
						SelectionKey.OP_WRITE);
				
				//将网络管道附着上一个处理类HandleClient,用于处理客户端事件的类
				wKey.attach(new HandleClient(charBuffer.toString()));
			} else{
				//如客户端没有可读事件,关闭管道
				channel.close();
			}
				
			clientBuffer.clear();
		} else if (key.isWritable()) { // 写事件
			SocketChannel channel = (SocketChannel) key.channel();
			
			//从管道中将附着处理类对象HandleClient取出来
			HandleClient handle = (HandleClient) key.attachment();
			
			//读取文件管道,返回数据缓存
			ByteBuffer block = handle.readBlock();
			if (block != null){
				//System.out.println("---"+new String(block.array()));
				
				//写给客户端
				channel.write(block);
			}else {
				handle.close();
				channel.close();
			}
		}
	}

	public static void main(String[] args) {
		int port = 12345;
		try {
			NIOServer server = new NIOServer(port);
			System.out.println("Listernint on " + port);
			while (true) {
				server.listen();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 ServerSocketChannel相当于我们说的那个大粗管子,在它上面注册了很多这个管子感兴趣的事件,比如大便、小便、酒醉后吐的污浊都是它关心的。至于谁来控制管道应该关心的事件,是由管道通过Selector注册事件完成的,Selector相当于一个大管道的维护员了。管道必须得有服务商的厂家维护吧,不能滥用吧。Selector就是个管家,负责管道的事件监听的。XXXXBuffer相当于咱们说的坐便器,它是以块为单位进行管道疏通的,假如您的尺寸特别大,估计您排出的那个玩意也小不了,就配置一个大点的缓存传给服务那边,当然,您这边得到的服务端返回的加工后肥料,返给您的也是和您配置的尺寸有关系的。客户端的代码如下

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package client;   
  2.   
  3. import java.io.FileNotFoundException;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6. import java.net.InetSocketAddress;   
  7. import java.nio.ByteBuffer;   
  8. import java.nio.CharBuffer;   
  9. import java.nio.channels.SelectionKey;   
  10. import java.nio.channels.Selector;   
  11. import java.nio.channels.SocketChannel;   
  12. import java.nio.charset.Charset;   
  13. import java.nio.charset.CharsetEncoder;   
  14. import java.util.Iterator;   
  15. import java.util.concurrent.ExecutorService;   
  16. import java.util.concurrent.Executors;   
  17.   
  18. /**  
  19.  *   
  20.  * @author liuyan  
  21.  *  
  22.  */  
  23. public class NIOClient {   
  24.     static int SIZE = 2;   
  25.     final static int bufferSize = 500 * 1024;   
  26.     static InetSocketAddress ip = new InetSocketAddress("localhost"12345);   
  27.     static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();   
  28.   
  29.     static class Download implements Runnable {   
  30.         protected int index;   
  31.         String outfile = null;   
  32.   
  33.         public Download(int index) {   
  34.             this.index = index;   
  35.             this.outfile = "c:\\" + index + ".rmvb";   
  36.         }   
  37.   
  38.         public void run() {   
  39.   
  40.             FileOutputStream fout = null;   
  41.             // FileChannel fcout = null;   
  42.             try {   
  43.                 fout = new FileOutputStream(outfile);   
  44.                 // fcout = fout.getChannel();   
  45.             } catch (FileNotFoundException e1) {   
  46.                 // TODO Auto-generated catch block   
  47.                 e1.printStackTrace();   
  48.             }   
  49.   
  50.             try {   
  51.                 long start = System.currentTimeMillis();   
  52.   
  53.                 // 打开客户端socket管道   
  54.                 SocketChannel client = SocketChannel.open();   
  55.   
  56.                 // 客户端的管道的通讯模式   
  57.                 client.configureBlocking(false);   
  58.   
  59.                 // 选择器   
  60.                 Selector selector = Selector.open();   
  61.   
  62.                 // 往客户端管道上注册感兴趣的连接事件   
  63.                 client.register(selector, SelectionKey.OP_CONNECT);   
  64.   
  65.                 // 配置IP   
  66.                 client.connect(ip);   
  67.   
  68.                 // 配置缓存大小   
  69.                 ByteBuffer buffer = ByteBuffer.allocate(bufferSize);   
  70.                 int total = 0;   
  71.                 FOR: for (;;) {   
  72.   
  73.                     // 阻塞,返回发生感兴趣事件的数量   
  74.                     selector.select();   
  75.   
  76.                     // 相当于获得感兴趣事件的集合迭代   
  77.                     Iterator<SelectionKey> iter = selector.selectedKeys()   
  78.                             .iterator();   
  79.   
  80.                     while (iter.hasNext()) {   
  81.                            
  82.                         SelectionKey key = iter.next();   
  83.                            
  84.                         System.out.println("-----Thread "  
  85.                                 + index + "------------------"+key.readyOps());   
  86.   
  87.                         // 删除这个马上就要被处理的事件   
  88.                         iter.remove();   
  89.   
  90.                         // 感兴趣的是可连接的事件   
  91.                         if (key.isConnectable()) {   
  92.   
  93.                             // 获得该事件中的管道对象   
  94.                             SocketChannel channel = (SocketChannel) key   
  95.                                     .channel();   
  96.   
  97.                             // 如果该管道对象已经连接好了   
  98.                             if (channel.isConnectionPending())   
  99.                                 channel.finishConnect();   
  100.   
  101.                             // 往管道中写一些块信息   
  102.                             channel.write(encoder.encode(CharBuffer   
  103.                                     .wrap("d://film//1.rmvb")));   
  104.   
  105.                             // 之后为该客户端管道注册新的感兴趣的事件---读操作   
  106.                             channel.register(selector, SelectionKey.OP_READ);   
  107.                         } else if (key.isReadable()) {   
  108.   
  109.                             // 由事件获得通讯管道   
  110.                             SocketChannel channel = (SocketChannel) key   
  111.                                     .channel();   
  112.   
  113.                             // 从管道中读取数据放到缓存中   
  114.                             int count = channel.read(buffer);   
  115.                             System.out.println("count:" + count);   
  116.                             if (count > 0) {   
  117.   
  118.                                 // 统计读取的字节数目   
  119.                                 total += count;   
  120.   
  121.                                 // 这样一来从posistion~limit这段缓存数据是有效,可利用的   
  122.                                 // buffer.flip();   
  123.   
  124.                                 buffer.clear();   
  125.   
  126.                                 // 往输出文件中去写了   
  127.                                 if (count < bufferSize) {   
  128.   
  129.                                     byte[] overByte = new byte[count];   
  130.   
  131.                                     for (int index = 0; index < count; index++) {   
  132.                                         overByte[index] = buffer.get(index);   
  133.                                     }   
  134.   
  135.                                     fout.write(overByte);   
  136.                                 } else {   
  137.                                     fout.write(buffer.array());   
  138.                                 }   
  139.   
  140.                             } else {   
  141.   
  142.                                 // 关闭客户端通道   
  143.                                 client.close();   
  144.   
  145.                                 // 退出大循环   
  146.                                 break FOR;   
  147.                             }   
  148.                         }   
  149.                     }   
  150.                 }   
  151.   
  152.                 // 计算时间   
  153.                 double last = (System.currentTimeMillis() - start) * 1.0 / 1000;   
  154.                 System.out.println("Thread " + index + " downloaded " + total   
  155.                         / 1024 + "kbytes in " + last + "s.");   
  156.             } catch (IOException e) {   
  157.                 e.printStackTrace();   
  158.             }   
  159.         }   
  160.     }   
  161.   
  162.     public static void main(String[] args) throws IOException {   
  163.   
  164.         long startTime = System.currentTimeMillis();   
  165.   
  166.         // 启用线程池   
  167.         ExecutorService exec = Executors.newFixedThreadPool(SIZE);   
  168.         for (int index = 1; index <= SIZE; index++) {   
  169.             exec.execute(new Download(index));   
  170.         }   
  171.         exec.shutdown();   
  172.   
  173.         long endTime = System.currentTimeMillis();   
  174.   
  175.         long timeLong = endTime - startTime;   
  176.   
  177.         System.out.println("下载时间:" + timeLong);   
  178.   
  179.     }   
  180. }</SPAN>  
package client;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
 * @author liuyan
 *
 */
public class NIOClient {
	static int SIZE = 2;
	final static int bufferSize = 500 * 1024;
	static InetSocketAddress ip = new InetSocketAddress("localhost", 12345);
	static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();

	static class Download implements Runnable {
		protected int index;
		String outfile = null;

		public Download(int index) {
			this.index = index;
			this.outfile = "c:\\" + index + ".rmvb";
		}

		public void run() {

			FileOutputStream fout = null;
			// FileChannel fcout = null;
			try {
				fout = new FileOutputStream(outfile);
				// fcout = fout.getChannel();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				long start = System.currentTimeMillis();

				// 打开客户端socket管道
				SocketChannel client = SocketChannel.open();

				// 客户端的管道的通讯模式
				client.configureBlocking(false);

				// 选择器
				Selector selector = Selector.open();

				// 往客户端管道上注册感兴趣的连接事件
				client.register(selector, SelectionKey.OP_CONNECT);

				// 配置IP
				client.connect(ip);

				// 配置缓存大小
				ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
				int total = 0;
				FOR: for (;;) {

					// 阻塞,返回发生感兴趣事件的数量
					selector.select();

					// 相当于获得感兴趣事件的集合迭代
					Iterator<SelectionKey> iter = selector.selectedKeys()
							.iterator();

					while (iter.hasNext()) {
						
						SelectionKey key = iter.next();
						
						System.out.println("-----Thread "
								+ index + "------------------"+key.readyOps());

						// 删除这个马上就要被处理的事件
						iter.remove();

						// 感兴趣的是可连接的事件
						if (key.isConnectable()) {

							// 获得该事件中的管道对象
							SocketChannel channel = (SocketChannel) key
									.channel();

							// 如果该管道对象已经连接好了
							if (channel.isConnectionPending())
								channel.finishConnect();

							// 往管道中写一些块信息
							channel.write(encoder.encode(CharBuffer
									.wrap("d://film//1.rmvb")));

							// 之后为该客户端管道注册新的感兴趣的事件---读操作
							channel.register(selector, SelectionKey.OP_READ);
						} else if (key.isReadable()) {

							// 由事件获得通讯管道
							SocketChannel channel = (SocketChannel) key
									.channel();

							// 从管道中读取数据放到缓存中
							int count = channel.read(buffer);
							System.out.println("count:" + count);
							if (count > 0) {

								// 统计读取的字节数目
								total += count;

								// 这样一来从posistion~limit这段缓存数据是有效,可利用的
								// buffer.flip();

								buffer.clear();

								// 往输出文件中去写了
								if (count < bufferSize) {

									byte[] overByte = new byte[count];

									for (int index = 0; index < count; index++) {
										overByte[index] = buffer.get(index);
									}

									fout.write(overByte);
								} else {
									fout.write(buffer.array());
								}

							} else {

								// 关闭客户端通道
								client.close();

								// 退出大循环
								break FOR;
							}
						}
					}
				}

				// 计算时间
				double last = (System.currentTimeMillis() - start) * 1.0 / 1000;
				System.out.println("Thread " + index + " downloaded " + total
						/ 1024 + "kbytes in " + last + "s.");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {

		long startTime = System.currentTimeMillis();

		// 启用线程池
		ExecutorService exec = Executors.newFixedThreadPool(SIZE);
		for (int index = 1; index <= SIZE; index++) {
			exec.execute(new Download(index));
		}
		exec.shutdown();

		long endTime = System.currentTimeMillis();

		long timeLong = endTime - startTime;

		System.out.println("下载时间:" + timeLong);

	}
}

 效果和上一个程序的效果差不多,只是时间上和内存资源占有率上有所提高,当然本机仅仅启动了几个线程,如果客户端启动更多线程,NIO的方式节约资源的效果是明显的,宕机概率小于阻塞IO方式很多。

5.  总结

其实NIO想写得更多,但是看到网络上已经有很多资料了,就不再展开了,非一篇所能尽述的了的。当然了,NIO也是有场景的,比如适合与长连接的请求,以为NIO维护管道、缓存块、时间选择器等等也需要资源消耗的,而且比传统IO的对象们要重量级。所以原始IO也并不是一无是处,现在还是有很多socket中间件还是已然使用第二种方式。

代码在附件中~~~

看到博客首页有一篇很好的原创文章,关于io和newio的用法和比较,自己收藏了,

1.  前言

现在很多做网络通讯中间代理层的通讯都是使用Java1.4以后推出的NIO进行编写,现在还有很多开源的框架也是封装了NIO的书写细节来帮助大家简写异步非阻塞通讯服务。像MySql的代理中间件amoeba-mysql-proxy就是采用NIO的方式处理client端过来的request,之后与Mysql-Server层的通讯也是采用NIO进行命令消息发送的。再看咱们JavaEye首页介绍的项目xmemcached,其中作者Dennis是其xmemcached的开发人,他也是通过NIO的方式与memcachedServer进行异步通讯的,Dennis的另一个项目yanf4j就是一个NIO框架,xmemcache也是借助这个NIO框架实现的异步非阻塞方式的网络通讯,ApacheMINA框架都是NIO的封装再实现。

那么我们就来回顾一下以前的处理方式,来看看为什么现在要使用NIO来进行异步非阻塞方式的通讯吧,网上很多文章都是几句话将NIO和原始的socket通讯的优劣一带而过,我们这次用一个简单的下载大文件的网络服务程序进行说明。使用3种模式来说明,分别是同步单独线程服务运行模式、传统阻塞多线程模式、使用NIO异步非阻塞模式。

我们设置服务器上有一个1.81GB的电影,格式为RMVB。使用Server进行服务监听,客户端请求到Server,建立网络通讯,进行电影下载。

2.  同步单线程阻塞

使用同步单线程下载,是最原始的socket通讯,服务端的代码如下

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package server;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import java.net.ServerSocket;   
  7. import java.net.Socket;   
  8.   
  9. /**  
  10.  * liuyan  
  11.  */  
  12. public class FilmServer {   
  13.   
  14.     public static void main(String[] args) {   
  15.         FilmServer ms = new FilmServer();   
  16.         try {   
  17.             ms.server();   
  18.         } catch (Exception e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22.   
  23.     /**  
  24.      * 服务器端响应请求  
  25.      *   
  26.      * @throws Exception  
  27.      */  
  28.     public void server() throws Exception {   
  29.   
  30.         // 0.建立服务器端的server的socket   
  31.         ServerSocket ss = new ServerSocket(9999);   
  32.   
  33.         while (true) {   
  34.   
  35.             // 1.打开socket连接   
  36.             // 等待客户端的请求   
  37.             final Socket server = ss.accept();   
  38.   
  39.             System.out.println("服务-----------请求开始start");   
  40.   
  41.             // 2.打开socket的流信息,准备下面的操作   
  42.             final InputStream is = server.getInputStream();   
  43.             byte b[] = new byte[1024];   
  44.   
  45.             int readCount = is.read(b);   
  46.   
  47.             String str = new String(b);   
  48.   
  49.             str = str.trim();   
  50.   
  51.             final String serverFileName = str;   
  52.   
  53.             // 3.对流信息进行读写操作   
  54.             System.out.println("客户端传过来的信息是:" + str);   
  55.   
  56.             System.out.println("线程" + Thread.currentThread().getName() + "启动");   
  57.   
  58.             try {   
  59.   
  60.                 FileInputStream fileInputStream = new FileInputStream(   
  61.                         serverFileName);   
  62.   
  63.                 // 3.1 服务器回复客户端信息(response)   
  64.                 OutputStream os = server.getOutputStream();   
  65.   
  66.                 byte[] bfile = new byte[1024];   
  67.   
  68.                 // 往客户端写   
  69.                 while (fileInputStream.read(bfile) > 0) {   
  70.                     os.write(bfile);   
  71.                 }   
  72.   
  73.                 fileInputStream.close();   
  74.   
  75.                 os.close();   
  76.   
  77.                 // 4.关闭socket   
  78.                 // 先关闭输入流   
  79.                 is.close();   
  80.   
  81.                 // 最后关闭socket   
  82.                 server.close();   
  83.   
  84.             } catch (Exception e) {   
  85.                 // TODO Auto-generated catch block   
  86.                 e.printStackTrace();   
  87.             }   
  88.   
  89.             System.out.println("服务-----------请求结束over");   
  90.         }   
  91.   
  92.     }   
  93.   
  94. }</SPAN>  
package server;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * liuyan
 */
public class FilmServer {

	public static void main(String[] args) {
		FilmServer ms = new FilmServer();
		try {
			ms.server();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 服务器端响应请求
	 * 
	 * @throws Exception
	 */
	public void server() throws Exception {

		// 0.建立服务器端的server的socket
		ServerSocket ss = new ServerSocket(9999);

		while (true) {

			// 1.打开socket连接
			// 等待客户端的请求
			final Socket server = ss.accept();

			System.out.println("服务-----------请求开始start");

			// 2.打开socket的流信息,准备下面的操作
			final InputStream is = server.getInputStream();
			byte b[] = new byte[1024];

			int readCount = is.read(b);

			String str = new String(b);

			str = str.trim();

			final String serverFileName = str;

			// 3.对流信息进行读写操作
			System.out.println("客户端传过来的信息是:" + str);

			System.out.println("线程" + Thread.currentThread().getName() + "启动");

			try {

				FileInputStream fileInputStream = new FileInputStream(
						serverFileName);

				// 3.1 服务器回复客户端信息(response)
				OutputStream os = server.getOutputStream();

				byte[] bfile = new byte[1024];

				// 往客户端写
				while (fileInputStream.read(bfile) > 0) {
					os.write(bfile);
				}

				fileInputStream.close();

				os.close();

				// 4.关闭socket
				// 先关闭输入流
				is.close();

				// 最后关闭socket
				server.close();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println("服务-----------请求结束over");
		}

	}

}

 服务端这么写代码会有什么问题?咱们先来看客户端代码,之后运行后就知道了。

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package client;   
  2.   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.io.OutputStream;   
  7. import java.net.Socket;   
  8. import java.net.UnknownHostException;   
  9.   
  10. /**  
  11.  * liuyan  
  12.  * @version 1.0  
  13.  */  
  14. public class FilmClient {   
  15.     public static void main(String[] args) {   
  16.         for (int i = 1; i <= 2; i++) {   
  17.             Client client = new Client();   
  18.             client.i = i;   
  19.             client.start();   
  20.         }   
  21.     }   
  22. }   
  23.   
  24. class Client extends Thread {   
  25.   
  26.     int i;   
  27.   
  28.     @Override  
  29.     public void run() {   
  30.   
  31.         // 1.建立scoket连接   
  32.         Socket client;   
  33.         try {   
  34.             client = new Socket("127.0.0.1"9999);   
  35.   
  36.             // 2.打开socket的流信息,准备下面的操作   
  37.             OutputStream os = client.getOutputStream();   
  38.   
  39.             // 3.写信息   
  40.             os.write(("d://film//2.rmvb").getBytes());   
  41.                
  42.             String filmName = "c://io"+i+".rmvb";   
  43.                
  44.             FileOutputStream fileOutputStream = new FileOutputStream(filmName);   
  45.   
  46.             // 3.1接收服务器端的反馈   
  47.             InputStream is = client.getInputStream();   
  48.             byte b[] = new byte[1024];   
  49.                
  50.             while(is.read(b)>0){   
  51.                 fileOutputStream.write(b);   
  52.             }   
  53.   
  54.             // 4.关闭socket   
  55.             // 先关闭输出流   
  56.             os.close();   
  57.   
  58.             // 最后关闭socket   
  59.             client.close();   
  60.         } catch (UnknownHostException e) {   
  61.             // TODO Auto-generated catch block   
  62.             e.printStackTrace();   
  63.         } catch (IOException e) {   
  64.             // TODO Auto-generated catch block   
  65.             e.printStackTrace();   
  66.         }   
  67.     }   
  68.   
  69. }</SPAN>  
package client;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * liuyan
 * @version 1.0
 */
public class FilmClient {
	public static void main(String[] args) {
		for (int i = 1; i <= 2; i++) {
			Client client = new Client();
			client.i = i;
			client.start();
		}
	}
}

class Client extends Thread {

	int i;

	@Override
	public void run() {

		// 1.建立scoket连接
		Socket client;
		try {
			client = new Socket("127.0.0.1", 9999);

			// 2.打开socket的流信息,准备下面的操作
			OutputStream os = client.getOutputStream();

			// 3.写信息
			os.write(("d://film//2.rmvb").getBytes());
			
			String filmName = "c://io"+i+".rmvb";
			
			FileOutputStream fileOutputStream = new FileOutputStream(filmName);

			// 3.1接收服务器端的反馈
			InputStream is = client.getInputStream();
			byte b[] = new byte[1024];
			
			while(is.read(b)>0){
				fileOutputStream.write(b);
			}

			// 4.关闭socket
			// 先关闭输出流
			os.close();

			// 最后关闭socket
			client.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 客户端启动了2个线程进行下载电影的工作,先启动服务端,再运行客户端,会看笔者本地的硬盘C分区到有如下效果。

 可以看到线程2的下载任务一直是0字节,等第一个线程下载完成后呢,线程2的下载任务才能进行。



 

服务端的代码造成的问题就是使用传统的sokect网络通讯,那么另一个客户端的线程请求到server端的时候就发生了阻塞的情况,也就是说,服务端相当一个厕所,厕所就有只有一个坑位,来了一个人,相当于客户端请求,那这个人相当于就把坑位给占了,write操作和read操作会阻塞,这个人还没解决完问题呢,下个人就来了,没办法,哥们儿先在门外等等啊,等前一个客户爽完了再给您提供服务好吧。那么如何解决这个占着坑位不让别人用的情况呢?

3.  阻塞的多线程

为了解决以上问题,那么之后很多Server肯定不可能像以上程序那么做,不过以前很多Server都是基于单线程服务改造一下,做成多线程的Server的通讯,修改一下上面的Server代码,如下

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package server;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import java.net.ServerSocket;   
  7. import java.net.Socket;   
  8.   
  9. /**  
  10.  *   
  11.  */  
  12. public class FilmServerNewThread {   
  13.   
  14.     public static void main(String[] args) {   
  15.         FilmServerNewThread ms = new FilmServerNewThread();   
  16.         try {   
  17.             ms.server();   
  18.         } catch (Exception e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22.   
  23.     /**  
  24.      * 服务器端响应请求  
  25.      *   
  26.      * @throws Exception  
  27.      */  
  28.     public void server() throws Exception {   
  29.   
  30.         // 0.建立服务器端的server的socket   
  31.         ServerSocket ss = new ServerSocket(9999);   
  32.   
  33.         while (true) {   
  34.   
  35.             // 1.打开socket连接   
  36.             // 等待客户端的请求   
  37.             final Socket server = ss.accept();   
  38.   
  39.             System.out.println("服务-----------请求开始start");   
  40.   
  41.             // 2.打开socket的流信息,准备下面的操作   
  42.             final InputStream is = server.getInputStream();   
  43.             byte b[] = new byte[1024];   
  44.   
  45.             int readCount = is.read(b);   
  46.   
  47.             String str = new String(b);   
  48.   
  49.             str = str.trim();   
  50.   
  51.             final String serverFileName = str;   
  52.   
  53.             // 3.对流信息进行读写操作   
  54.             System.out.println("客户端传过来的信息是:" + str);   
  55.   
  56.             if (readCount > 0) {   
  57.                 new Thread() {   
  58.   
  59.                     @Override  
  60.                     public void run() {   
  61.   
  62.                         System.out.println("线程"  
  63.                                 + Thread.currentThread().getName() + "启动");   
  64.   
  65.                         try {   
  66.   
  67.                             FileInputStream fileInputStream = new FileInputStream(   
  68.                                     serverFileName);   
  69.   
  70.                             // 3.1 服务器回复客户端信息(response)   
  71.                             OutputStream os = server.getOutputStream();   
  72.   
  73.                             byte[] bfile = new byte[1024];   
  74.   
  75.                             // 往客户端写   
  76.                             while (fileInputStream.read(bfile) > 0) {   
  77.                                 os.write(bfile);   
  78.                             }   
  79.   
  80.                             fileInputStream.close();   
  81.   
  82.                             os.close();   
  83.   
  84.                             // 4.关闭socket   
  85.                             // 先关闭输入流   
  86.                             is.close();   
  87.   
  88.                             // 最后关闭socket   
  89.                             server.close();   
  90.   
  91.                         } catch (Exception e) {   
  92.                             // TODO Auto-generated catch block   
  93.                             e.printStackTrace();   
  94.                         }   
  95.                     }   
  96.                 }.start();   
  97.             }   
  98.   
  99.             System.out.println("服务-----------请求结束over");   
  100.         }   
  101.   
  102.     }   
  103. }</SPAN>  
package server;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 
 */
public class FilmServerNewThread {

	public static void main(String[] args) {
		FilmServerNewThread ms = new FilmServerNewThread();
		try {
			ms.server();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 服务器端响应请求
	 * 
	 * @throws Exception
	 */
	public void server() throws Exception {

		// 0.建立服务器端的server的socket
		ServerSocket ss = new ServerSocket(9999);

		while (true) {

			// 1.打开socket连接
			// 等待客户端的请求
			final Socket server = ss.accept();

			System.out.println("服务-----------请求开始start");

			// 2.打开socket的流信息,准备下面的操作
			final InputStream is = server.getInputStream();
			byte b[] = new byte[1024];

			int readCount = is.read(b);

			String str = new String(b);

			str = str.trim();

			final String serverFileName = str;

			// 3.对流信息进行读写操作
			System.out.println("客户端传过来的信息是:" + str);

			if (readCount > 0) {
				new Thread() {

					@Override
					public void run() {

						System.out.println("线程"
								+ Thread.currentThread().getName() + "启动");

						try {

							FileInputStream fileInputStream = new FileInputStream(
									serverFileName);

							// 3.1 服务器回复客户端信息(response)
							OutputStream os = server.getOutputStream();

							byte[] bfile = new byte[1024];

							// 往客户端写
							while (fileInputStream.read(bfile) > 0) {
								os.write(bfile);
							}

							fileInputStream.close();

							os.close();

							// 4.关闭socket
							// 先关闭输入流
							is.close();

							// 最后关闭socket
							server.close();

						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}.start();
			}

			System.out.println("服务-----------请求结束over");
		}

	}
}

 以上的Server就是在原始的socket基础上加了线程,每一个Client请求过来后,整个Server主线程不必处于阻塞状态,接收请求后直接另起一个新的线程来处理和客户端的交互,就是往客户端发送二进制包。这个在新线程中虽然阻塞,但是对于服务主线程没有阻塞的影响,主线程依然通过死循环监听着客户端的一举一动。另一个客户端的线程发起请求后就再起一个新的线程对象去为客户端服务。执行效果如下

 

2个线程互不影响,各自下载各自的。当然从非常严格的意义来讲,str变量在十分高并发的情况下有线程安全问题,这个咱暂且忽略,就着眼于低并发的情况。这个问题是什么呢,就是如果客户端请求比较多了,那么为每一个客户端开辟一个新的线程对象来处理网络传输的请求,需要创建个线程对象,而且这个线程对象从时间上来讲还是处于长连接,这个就比较消费系统资源,这个打开进程管理器就可以看到。而且每一个线程内部都是阻塞的,也没有说完全利用好这个新创建的线程。还拿刚才上厕所举例子,好比现在不止一个坑位了,来了一个用户我这边就按照工程师的厕所坑位图建立一个新的坑位,客户来了,不用等待老坑位,用新创建的坑位就行了。等那个老坑位用完了,自然有垃圾回收器去消灭那个一次性的坑位的,腾出资源位置为了建立新的坑位。长时间连接的意思,相当于这个人上厕所的时间非常长,便秘??需要拉一天才能爽完……老的坑位一时半会儿回收不了,新的坑位需要有空间为其建造茅房以便满足客户端的“急切方便”需要。久而久之,线程数目一多,系统就挂了的概率就增多了(谁也别想上,全玩完了)。

4.  异步非阻塞

使用JDK1.4NIO可以适当的解决上面的问题,异步 I/O 是一种 没有阻塞地读写数据的方法。通常,在代码进行 read() 调用时,代码会阻塞直至有可供读取的数据。同样, write() 调用将会阻塞直至数据能够写入。异步 I/O 调用不会阻塞。相反,您将注册对特定 I/O 事件的兴趣可读的数据的到达、新的套接字连接,等等,而在发生这样的事件时,系统将会告诉您。异步 I/O 的一个优势在于,它允许您同时根据大量的输入和输出执行 I/O。同步程序常常要求助于轮询,或者创建许许多多的线程以处理大量的连接。使用异步 I/O,您可以监听任何数量的通道上的事件,不用轮询,也不用额外的线程。还是举上公共厕所例子,虽然这个例子有点臭臭的。您现在有“便便”的需求了,不用那么麻烦,看看公共厕所是否有人占领,也不用给您另起个新坑位,您就拿一根我们服务端定制的容器和一个很粗管子,这个坐便器的大小因您那个地方的尺寸而定,坐便器往您的那个地方一放,再将坐便器和管子一连接,OK,您就敞开了“爽”吧。不用担心,这个管子自然会连接到相应的肥料厂家,将您的排泄物有效回收加以利用的。您完了事,擦擦屁股,关上管子该干嘛还干嘛就行了。另一个人也有这个需求,没问题,每个要我们提供服务的人都用这根管子,和自己的坐便器就行了,管子很粗,谁来连这个管子都行,有多少都行啊。下面我们来看基于NIO的网络下载程序

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package server;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.IOException;   
  5. import java.net.InetSocketAddress;   
  6. import java.nio.ByteBuffer;   
  7. import java.nio.CharBuffer;   
  8. import java.nio.channels.FileChannel;   
  9. import java.nio.channels.SelectionKey;   
  10. import java.nio.channels.Selector;   
  11. import java.nio.channels.ServerSocketChannel;   
  12. import java.nio.channels.SocketChannel;   
  13. import java.nio.charset.Charset;   
  14. import java.nio.charset.CharsetDecoder;   
  15. import java.util.Iterator;   
  16.   
  17. /**  
  18.  *   
  19.  * @author liuyan  
  20.  *  
  21.  */  
  22. public class NIOServer {   
  23.     static int BLOCK = 500*1024;   
  24.   
  25.     /**  
  26.      * 处理客户端的内部类,专门负责处理与用户的交互  
  27.      */  
  28.     public class HandleClient {   
  29.         protected FileChannel channel;   
  30.         protected ByteBuffer buffer;   
  31.         String filePath;   
  32.            
  33.          /**  
  34.           * 构造函数,文件的管道初始化  
  35.           * @param filePath  
  36.           * @throws IOException  
  37.           */  
  38.         public HandleClient(String filePath) throws IOException {   
  39.                
  40.             //文件的管道   
  41.             this.channel = new FileInputStream(filePath).getChannel();   
  42.                
  43.             //建立缓存   
  44.             this.buffer = ByteBuffer.allocate(BLOCK);   
  45.             this.filePath = filePath;   
  46.         }   
  47.            
  48.         /**  
  49.          * 读取文件管道中数据到缓存中  
  50.          * @return  
  51.          */  
  52.         public ByteBuffer readBlock() {   
  53.             try {   
  54.                    
  55.                 //清除缓冲区的内容,posistion设置为0,limit设置为缓冲的容量大小capacity   
  56.                 buffer.clear();   
  57.                    
  58.                 //读取   
  59.                 int count = channel.read(buffer);   
  60.                    
  61.                 //将缓存的中的posistion设置为0,将缓存中的limit设置在原始posistion位置上   
  62.                 buffer.flip();   
  63.                 if (count <= 0)   
  64.                     return null;   
  65.             } catch (IOException e) {   
  66.                 e.printStackTrace();   
  67.             }   
  68.             return buffer;   
  69.         }   
  70.            
  71.         /**  
  72.          * 关闭服务端的文件管道  
  73.          */  
  74.         public void close() {   
  75.             try {   
  76.                 channel.close();   
  77.             } catch (IOException e) {   
  78.                 e.printStackTrace();   
  79.             }   
  80.         }   
  81.     }   
  82.   
  83.     protected Selector selector;   
  84.     protected String filename = "d:\\film\\60.rmvb"// a big file   
  85.     protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);   
  86.     protected CharsetDecoder decoder;   
  87.        
  88.     //构造服务端口,服务管道等等   
  89.     public NIOServer(int port) throws IOException {   
  90.         selector = this.getSelector(port);   
  91.         Charset charset = Charset.forName("GB2312");   
  92.         decoder = charset.newDecoder();   
  93.     }   
  94.   
  95.     // 获取Selector   
  96.     //构造服务端口,服务管道等等   
  97.     protected Selector getSelector(int port) throws IOException {   
  98.         ServerSocketChannel server = ServerSocketChannel.open();   
  99.         Selector sel = Selector.open();   
  100.         server.socket().bind(new InetSocketAddress(port));   
  101.         server.configureBlocking(false);   
  102.            
  103.         //刚开始就注册链接事件   
  104.         server.register(sel, SelectionKey.OP_ACCEPT);   
  105.         return sel;   
  106.     }   
  107.   
  108.     // 服务启动的开始入口   
  109.     public void listen() {   
  110.         try {   
  111.             for (;;) {   
  112.                    
  113.                 //?   
  114.                 selector.select();   
  115.                 Iterator<SelectionKey> iter = selector.selectedKeys()   
  116.                         .iterator();   
  117.                 while (iter.hasNext()) {//首先是最先感兴趣的连接事件   
  118.                     SelectionKey key = iter.next();   
  119.                        
  120.                     //   
  121.                     iter.remove();   
  122.                        
  123.                     //处理事件   
  124.                     handleKey(key);   
  125.                 }   
  126.             }   
  127.         } catch (IOException e) {   
  128.             e.printStackTrace();   
  129.         }   
  130.     }   
  131.   
  132.     // 处理事件   
  133.     protected void handleKey(SelectionKey key) throws IOException {   
  134.         if (key.isAcceptable()) { // 接收请求   
  135.                
  136.             //允许网络连接事件   
  137.             ServerSocketChannel server = (ServerSocketChannel) key.channel();   
  138.             SocketChannel channel = server.accept();   
  139.             channel.configureBlocking(false);   
  140.                
  141.             //网络管道准备处理读事件   
  142.             channel.register(selector, SelectionKey.OP_READ);   
  143.         } else if (key.isReadable()) { // 读信息   
  144.             SocketChannel channel = (SocketChannel) key.channel();   
  145.                
  146.             //从客户端读过来的数据块   
  147.             int count = channel.read(clientBuffer);   
  148.             if (count > 0) {   
  149.                    
  150.                 //读取过来的缓存进行有效分割,posistion设置为0,保证从缓存的有效位置开始读取,limit设置为原先的posistion上   
  151.                 //这样一来从posistion~limit这段缓存数据是有效,可利用的   
  152.                 clientBuffer.flip();   
  153.                    
  154.                 //对客户端缓存块进行编码   
  155.                 CharBuffer charBuffer = decoder.decode(clientBuffer);   
  156.                 System.out.println("Client >>download>>" + charBuffer.toString());   
  157.                    
  158.                 //对网络管道注册写事件   
  159.                 SelectionKey wKey = channel.register(selector,   
  160.                         SelectionKey.OP_WRITE);   
  161.                    
  162.                 //将网络管道附着上一个处理类HandleClient,用于处理客户端事件的类   
  163.                 wKey.attach(new HandleClient(charBuffer.toString()));   
  164.             } else{   
  165.                 //如客户端没有可读事件,关闭管道   
  166.                 channel.close();   
  167.             }   
  168.                    
  169.             clientBuffer.clear();   
  170.         } else if (key.isWritable()) { // 写事件   
  171.             SocketChannel channel = (SocketChannel) key.channel();   
  172.                
  173.             //从管道中将附着处理类对象HandleClient取出来   
  174.             HandleClient handle = (HandleClient) key.attachment();   
  175.                
  176.             //读取文件管道,返回数据缓存   
  177.             ByteBuffer block = handle.readBlock();   
  178.             if (block != null){   
  179.                 //System.out.println("---"+new String(block.array()));   
  180.                    
  181.                 //写给客户端   
  182.                 channel.write(block);   
  183.             }else {   
  184.                 handle.close();   
  185.                 channel.close();   
  186.             }   
  187.         }   
  188.     }   
  189.   
  190.     public static void main(String[] args) {   
  191.         int port = 12345;   
  192.         try {   
  193.             NIOServer server = new NIOServer(port);   
  194.             System.out.println("Listernint on " + port);   
  195.             while (true) {   
  196.                 server.listen();   
  197.             }   
  198.         } catch (IOException e) {   
  199.             e.printStackTrace();   
  200.         }   
  201.     }   
  202. }</SPAN>  
package server;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Iterator;

/**
 * 
 * @author liuyan
 *
 */
public class NIOServer {
	static int BLOCK = 500*1024;

	/**
	 * 处理客户端的内部类,专门负责处理与用户的交互
	 */
	public class HandleClient {
		protected FileChannel channel;
		protected ByteBuffer buffer;
		String filePath;
		
		 /**
		  * 构造函数,文件的管道初始化
		  * @param filePath
		  * @throws IOException
		  */
		public HandleClient(String filePath) throws IOException {
			
			//文件的管道
			this.channel = new FileInputStream(filePath).getChannel();
			
			//建立缓存
			this.buffer = ByteBuffer.allocate(BLOCK);
			this.filePath = filePath;
		}
		
		/**
		 * 读取文件管道中数据到缓存中
		 * @return
		 */
		public ByteBuffer readBlock() {
			try {
				
				//清除缓冲区的内容,posistion设置为0,limit设置为缓冲的容量大小capacity
				buffer.clear();
				
				//读取
				int count = channel.read(buffer);
				
				//将缓存的中的posistion设置为0,将缓存中的limit设置在原始posistion位置上
				buffer.flip();
				if (count <= 0)
					return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
			return buffer;
		}
		
		/**
		 * 关闭服务端的文件管道
		 */
		public void close() {
			try {
				channel.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	protected Selector selector;
	protected String filename = "d:\\film\\60.rmvb"; // a big file
	protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);
	protected CharsetDecoder decoder;
	
	//构造服务端口,服务管道等等
	public NIOServer(int port) throws IOException {
		selector = this.getSelector(port);
		Charset charset = Charset.forName("GB2312");
		decoder = charset.newDecoder();
	}

	// 获取Selector
	//构造服务端口,服务管道等等
	protected Selector getSelector(int port) throws IOException {
		ServerSocketChannel server = ServerSocketChannel.open();
		Selector sel = Selector.open();
		server.socket().bind(new InetSocketAddress(port));
		server.configureBlocking(false);
		
		//刚开始就注册链接事件
		server.register(sel, SelectionKey.OP_ACCEPT);
		return sel;
	}

	// 服务启动的开始入口
	public void listen() {
		try {
			for (;;) {
				
				//?
				selector.select();
				Iterator<SelectionKey> iter = selector.selectedKeys()
						.iterator();
				while (iter.hasNext()) {//首先是最先感兴趣的连接事件
					SelectionKey key = iter.next();
					
					//
					iter.remove();
					
					//处理事件
					handleKey(key);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 处理事件
	protected void handleKey(SelectionKey key) throws IOException {
		if (key.isAcceptable()) { // 接收请求
			
			//允许网络连接事件
			ServerSocketChannel server = (ServerSocketChannel) key.channel();
			SocketChannel channel = server.accept();
			channel.configureBlocking(false);
			
			//网络管道准备处理读事件
			channel.register(selector, SelectionKey.OP_READ);
		} else if (key.isReadable()) { // 读信息
			SocketChannel channel = (SocketChannel) key.channel();
			
			//从客户端读过来的数据块
			int count = channel.read(clientBuffer);
			if (count > 0) {
				
				//读取过来的缓存进行有效分割,posistion设置为0,保证从缓存的有效位置开始读取,limit设置为原先的posistion上
				//这样一来从posistion~limit这段缓存数据是有效,可利用的
				clientBuffer.flip();
				
				//对客户端缓存块进行编码
				CharBuffer charBuffer = decoder.decode(clientBuffer);
				System.out.println("Client >>download>>" + charBuffer.toString());
				
				//对网络管道注册写事件
				SelectionKey wKey = channel.register(selector,
						SelectionKey.OP_WRITE);
				
				//将网络管道附着上一个处理类HandleClient,用于处理客户端事件的类
				wKey.attach(new HandleClient(charBuffer.toString()));
			} else{
				//如客户端没有可读事件,关闭管道
				channel.close();
			}
				
			clientBuffer.clear();
		} else if (key.isWritable()) { // 写事件
			SocketChannel channel = (SocketChannel) key.channel();
			
			//从管道中将附着处理类对象HandleClient取出来
			HandleClient handle = (HandleClient) key.attachment();
			
			//读取文件管道,返回数据缓存
			ByteBuffer block = handle.readBlock();
			if (block != null){
				//System.out.println("---"+new String(block.array()));
				
				//写给客户端
				channel.write(block);
			}else {
				handle.close();
				channel.close();
			}
		}
	}

	public static void main(String[] args) {
		int port = 12345;
		try {
			NIOServer server = new NIOServer(port);
			System.out.println("Listernint on " + port);
			while (true) {
				server.listen();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 ServerSocketChannel相当于我们说的那个大粗管子,在它上面注册了很多这个管子感兴趣的事件,比如大便、小便、酒醉后吐的污浊都是它关心的。至于谁来控制管道应该关心的事件,是由管道通过Selector注册事件完成的,Selector相当于一个大管道的维护员了。管道必须得有服务商的厂家维护吧,不能滥用吧。Selector就是个管家,负责管道的事件监听的。XXXXBuffer相当于咱们说的坐便器,它是以块为单位进行管道疏通的,假如您的尺寸特别大,估计您排出的那个玩意也小不了,就配置一个大点的缓存传给服务那边,当然,您这边得到的服务端返回的加工后肥料,返给您的也是和您配置的尺寸有关系的。客户端的代码如下

Java代码 复制代码  收藏代码
  1. <SPAN style="FONT-SIZE: x-small">package client;   
  2.   
  3. import java.io.FileNotFoundException;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6. import java.net.InetSocketAddress;   
  7. import java.nio.ByteBuffer;   
  8. import java.nio.CharBuffer;   
  9. import java.nio.channels.SelectionKey;   
  10. import java.nio.channels.Selector;   
  11. import java.nio.channels.SocketChannel;   
  12. import java.nio.charset.Charset;   
  13. import java.nio.charset.CharsetEncoder;   
  14. import java.util.Iterator;   
  15. import java.util.concurrent.ExecutorService;   
  16. import java.util.concurrent.Executors;   
  17.   
  18. /**  
  19.  *   
  20.  * @author liuyan  
  21.  *  
  22.  */  
  23. public class NIOClient {   
  24.     static int SIZE = 2;   
  25.     final static int bufferSize = 500 * 1024;   
  26.     static InetSocketAddress ip = new InetSocketAddress("localhost"12345);   
  27.     static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();   
  28.   
  29.     static class Download implements Runnable {   
  30.         protected int index;   
  31.         String outfile = null;   
  32.   
  33.         public Download(int index) {   
  34.             this.index = index;   
  35.             this.outfile = "c:\\" + index + ".rmvb";   
  36.         }   
  37.   
  38.         public void run() {   
  39.   
  40.             FileOutputStream fout = null;   
  41.             // FileChannel fcout = null;   
  42.             try {   
  43.                 fout = new FileOutputStream(outfile);   
  44.                 // fcout = fout.getChannel();   
  45.             } catch (FileNotFoundException e1) {   
  46.                 // TODO Auto-generated catch block   
  47.                 e1.printStackTrace();   
  48.             }   
  49.   
  50.             try {   
  51.                 long start = System.currentTimeMillis();   
  52.   
  53.                 // 打开客户端socket管道   
  54.                 SocketChannel client = SocketChannel.open();   
  55.   
  56.                 // 客户端的管道的通讯模式   
  57.                 client.configureBlocking(false);   
  58.   
  59.                 // 选择器   
  60.                 Selector selector = Selector.open();   
  61.   
  62.                 // 往客户端管道上注册感兴趣的连接事件   
  63.                 client.register(selector, SelectionKey.OP_CONNECT);   
  64.   
  65.                 // 配置IP   
  66.                 client.connect(ip);   
  67.   
  68.                 // 配置缓存大小   
  69.                 ByteBuffer buffer = ByteBuffer.allocate(bufferSize);   
  70.                 int total = 0;   
  71.                 FOR: for (;;) {   
  72.   
  73.                     // 阻塞,返回发生感兴趣事件的数量   
  74.                     selector.select();   
  75.   
  76.                     // 相当于获得感兴趣事件的集合迭代   
  77.                     Iterator<SelectionKey> iter = selector.selectedKeys()   
  78.                             .iterator();   
  79.   
  80.                     while (iter.hasNext()) {   
  81.                            
  82.                         SelectionKey key = iter.next();   
  83.                            
  84.                         System.out.println("-----Thread "  
  85.                                 + index + "------------------"+key.readyOps());   
  86.   
  87.                         // 删除这个马上就要被处理的事件   
  88.                         iter.remove();   
  89.   
  90.                         // 感兴趣的是可连接的事件   
  91.                         if (key.isConnectable()) {   
  92.   
  93.                             // 获得该事件中的管道对象   
  94.                             SocketChannel channel = (SocketChannel) key   
  95.                                     .channel();   
  96.   
  97.                             // 如果该管道对象已经连接好了   
  98.                             if (channel.isConnectionPending())   
  99.                                 channel.finishConnect();   
  100.   
  101.                             // 往管道中写一些块信息   
  102.                             channel.write(encoder.encode(CharBuffer   
  103.                                     .wrap("d://film//1.rmvb")));   
  104.   
  105.                             // 之后为该客户端管道注册新的感兴趣的事件---读操作   
  106.                             channel.register(selector, SelectionKey.OP_READ);   
  107.                         } else if (key.isReadable()) {   
  108.   
  109.                             // 由事件获得通讯管道   
  110.                             SocketChannel channel = (SocketChannel) key   
  111.                                     .channel();   
  112.   
  113.                             // 从管道中读取数据放到缓存中   
  114.                             int count = channel.read(buffer);   
  115.                             System.out.println("count:" + count);   
  116.                             if (count > 0) {   
  117.   
  118.                                 // 统计读取的字节数目   
  119.                                 total += count;   
  120.   
  121.                                 // 这样一来从posistion~limit这段缓存数据是有效,可利用的   
  122.                                 // buffer.flip();   
  123.   
  124.                                 buffer.clear();   
  125.   
  126.                                 // 往输出文件中去写了   
  127.                                 if (count < bufferSize) {   
  128.   
  129.                                     byte[] overByte = new byte[count];   
  130.   
  131.                                     for (int index = 0; index < count; index++) {   
  132.                                         overByte[index] = buffer.get(index);   
  133.                                     }   
  134.   
  135.                                     fout.write(overByte);   
  136.                                 } else {   
  137.                                     fout.write(buffer.array());   
  138.                                 }   
  139.   
  140.                             } else {   
  141.   
  142.                                 // 关闭客户端通道   
  143.                                 client.close();   
  144.   
  145.                                 // 退出大循环   
  146.                                 break FOR;   
  147.                             }   
  148.                         }   
  149.                     }   
  150.                 }   
  151.   
  152.                 // 计算时间   
  153.                 double last = (System.currentTimeMillis() - start) * 1.0 / 1000;   
  154.                 System.out.println("Thread " + index + " downloaded " + total   
  155.                         / 1024 + "kbytes in " + last + "s.");   
  156.             } catch (IOException e) {   
  157.                 e.printStackTrace();   
  158.             }   
  159.         }   
  160.     }   
  161.   
  162.     public static void main(String[] args) throws IOException {   
  163.   
  164.         long startTime = System.currentTimeMillis();   
  165.   
  166.         // 启用线程池   
  167.         ExecutorService exec = Executors.newFixedThreadPool(SIZE);   
  168.         for (int index = 1; index <= SIZE; index++) {   
  169.             exec.execute(new Download(index));   
  170.         }   
  171.         exec.shutdown();   
  172.   
  173.         long endTime = System.currentTimeMillis();   
  174.   
  175.         long timeLong = endTime - startTime;   
  176.   
  177.         System.out.println("下载时间:" + timeLong);   
  178.   
  179.     }   
  180. }</SPAN>  
package client;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
 * @author liuyan
 *
 */
public class NIOClient {
	static int SIZE = 2;
	final static int bufferSize = 500 * 1024;
	static InetSocketAddress ip = new InetSocketAddress("localhost", 12345);
	static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();

	static class Download implements Runnable {
		protected int index;
		String outfile = null;

		public Download(int index) {
			this.index = index;
			this.outfile = "c:\\" + index + ".rmvb";
		}

		public void run() {

			FileOutputStream fout = null;
			// FileChannel fcout = null;
			try {
				fout = new FileOutputStream(outfile);
				// fcout = fout.getChannel();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				long start = System.currentTimeMillis();

				// 打开客户端socket管道
				SocketChannel client = SocketChannel.open();

				// 客户端的管道的通讯模式
				client.configureBlocking(false);

				// 选择器
				Selector selector = Selector.open();

				// 往客户端管道上注册感兴趣的连接事件
				client.register(selector, SelectionKey.OP_CONNECT);

				// 配置IP
				client.connect(ip);

				// 配置缓存大小
				ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
				int total = 0;
				FOR: for (;;) {

					// 阻塞,返回发生感兴趣事件的数量
					selector.select();

					// 相当于获得感兴趣事件的集合迭代
					Iterator<SelectionKey> iter = selector.selectedKeys()
							.iterator();

					while (iter.hasNext()) {
						
						SelectionKey key = iter.next();
						
						System.out.println("-----Thread "
								+ index + "------------------"+key.readyOps());

						// 删除这个马上就要被处理的事件
						iter.remove();

						// 感兴趣的是可连接的事件
						if (key.isConnectable()) {

							// 获得该事件中的管道对象
							SocketChannel channel = (SocketChannel) key
									.channel();

							// 如果该管道对象已经连接好了
							if (channel.isConnectionPending())
								channel.finishConnect();

							// 往管道中写一些块信息
							channel.write(encoder.encode(CharBuffer
									.wrap("d://film//1.rmvb")));

							// 之后为该客户端管道注册新的感兴趣的事件---读操作
							channel.register(selector, SelectionKey.OP_READ);
						} else if (key.isReadable()) {

							// 由事件获得通讯管道
							SocketChannel channel = (SocketChannel) key
									.channel();

							// 从管道中读取数据放到缓存中
							int count = channel.read(buffer);
							System.out.println("count:" + count);
							if (count > 0) {

								// 统计读取的字节数目
								total += count;

								// 这样一来从posistion~limit这段缓存数据是有效,可利用的
								// buffer.flip();

								buffer.clear();

								// 往输出文件中去写了
								if (count < bufferSize) {

									byte[] overByte = new byte[count];

									for (int index = 0; index < count; index++) {
										overByte[index] = buffer.get(index);
									}

									fout.write(overByte);
								} else {
									fout.write(buffer.array());
								}

							} else {

								// 关闭客户端通道
								client.close();

								// 退出大循环
								break FOR;
							}
						}
					}
				}

				// 计算时间
				double last = (System.currentTimeMillis() - start) * 1.0 / 1000;
				System.out.println("Thread " + index + " downloaded " + total
						/ 1024 + "kbytes in " + last + "s.");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {

		long startTime = System.currentTimeMillis();

		// 启用线程池
		ExecutorService exec = Executors.newFixedThreadPool(SIZE);
		for (int index = 1; index <= SIZE; index++) {
			exec.execute(new Download(index));
		}
		exec.shutdown();

		long endTime = System.currentTimeMillis();

		long timeLong = endTime - startTime;

		System.out.println("下载时间:" + timeLong);

	}
}

 效果和上一个程序的效果差不多,只是时间上和内存资源占有率上有所提高,当然本机仅仅启动了几个线程,如果客户端启动更多线程,NIO的方式节约资源的效果是明显的,宕机概率小于阻塞IO方式很多。

5.  总结

其实NIO想写得更多,但是看到网络上已经有很多资料了,就不再展开了,非一篇所能尽述的了的。当然了,NIO也是有场景的,比如适合与长连接的请求,以为NIO维护管道、缓存块、时间选择器等等也需要资源消耗的,而且比传统IO的对象们要重量级。所以原始IO也并不是一无是处,现在还是有很多socket中间件还是已然使用第二种方式。

代码在附件中~~~

猜你喜欢

转载自kakaluyi.iteye.com/blog/1124567