NIO中关于ByteBuffer的简单使用

1.和NIOServer建立连接new Socket(ip, port)
* 2.获得Scanner.next(System.in):输入内容
* 3. 将要发送的字符串内容进行处理
*    a. 获得int len = str.getByte().length
*    b. 将len 进行byte 处理,依次发给server
*    dos.write((byte)temps.length&0xff);
dos.write((byte)temps.length>>8&0xff);
*    c. 将数据本身发送给NioServer
*    注意:因为NIOserver使用ByteBuffer来接收数据,所以,必须在客户端采用字节流方式,并且进行Byte运算后,才可以发送到网络流

2. 服务器类比较多,此处至贴出相关byteBuffer处理的代码

客户端代码

/**
 * 
 */
package client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


/**
 * @author sean
 * 
 * 测试NIOServer的客户端类
 * 1.和NIOServer建立连接new Socket(ip, port) 
 * 2.获得Scanner.next(System.in):输入内容
 * 3. 将要发送的字符串内容进行处理
 *    a. 获得int len = str.getByte().length
 *    b. 将len 进行byte 处理,依次发给server
 *    	dos.write((byte)temps.length&0xff);
		dos.write((byte)temps.length>>8&0xff);
 *    c. 将数据本身发送给NioServer
 *    注意:因为NIOserver使用ByteBuffer来接收数据,所以,必须在客户端采用字节流方式,并且进行Byte运算后,才可以发送到网络流
 *    
 * *
 */
public class ClientDemo {

	static Socket socket;	
	static DataOutputStream dos;
	
	static DataInputStream dis;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			socket = new Socket("127.0.0.1", 9999);		
			if(socket == null){
				System.exit(0);
			}
			dos = new DataOutputStream(socket.getOutputStream());
			dis = new DataInputStream(socket.getInputStream());	

			//获取一个屏幕读取类
			Scanner sc = new Scanner(System.in);			
			
			do{
				System.out.println("please enter content:");
				String msg = sc.next();
				System.out.println("length:" + msg.getBytes().length);
				
				//此处将内容采用utf-8方式得到字节数据
				byte temps[]=msg.getBytes("UTF-8");
				
				//将length进行位运算,整理成short型,代表将要发送字符串内容的长度,方便
				//server端根据长度来使用byte[]	来接收处理
				dos.write((byte)temps.length&0xff);
				dos.write((byte)temps.length>>8&0xff);
				dos.write(temps);
				System.out.println("printwrite send ok: " + msg);
				
				if(msg.equalsIgnoreCase("bye")){
					break;
				}
			}
			while(true);			
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			closeSocket();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			closeSocket();
		}		
	}

	/**
	 * 关闭当前socket
	 * 依次关闭is, os, socket
	 * socket通道最后关闭
	 */
	private static void closeSocket() {
		// TODO Auto-generated method stub
		if(dis != null){
			try {
				dis.close();
				dis = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
		if(dos != null){
			try {
				dos.close();
				dos = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
		if(socket != null){
			try {
				socket.close();
				socket = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
}





Server代码, 只是给出相关的代码
public void handleRead(SelectionKey key) {

// TODO Auto-generated method stub
if (this.selectionKey != key) {
closeChannel();
this.selectionKey = key;
this.channel = (SocketChannel) key.channel();
}

try {
int ret = channel.read(inBuffer);


if (ret < 0) {
closeChannel();
}

int result = parseInput(inBuffer);

if (result < 0) {
closeChannel();
return;
}

this.lastAccessTime = System.currentTimeMillis();

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

}

/**
* byteBuffer变成message
* 并且调用logicprocess
* @param inBuffer
* @return
*/
private int parseInput(ByteBuffer inBuffer) {
// TODO Auto-generated method stub
int result = 0;
List<Message> list = server.getProtocol().parseDataInput(inBuffer, this);

if(list == null){
return -1;
}

for(int i=0; i<list.size(); i++){
server.getLogicProcess().serverProcess(list.get(i));
}

return result;
}

/**
*
*/
private void closeChannel() {
// TODO Auto-generated method stub
selectionKey.cancel();
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/* 
	 * 此处将ByteBuffer进行解析,
	 * 打包成message格式
	 */
	@Override
	public List<Message> parseDataInput(ByteBuffer inBuffer, SocketSession session) {
		// TODO Auto-generated method stub
		if(inBuffer == null){
			return null;
		}
		System.out.println(inBuffer);
		
		//此处注意需要复制出一个tempBuf,并且用flip(),否则报错underflowException
		ByteBuffer tempBuf = inBuffer.duplicate();
		inBuffer.flip();
		
		List<Message> list = new ArrayList<Message>();
		
		//判断是否还有剩余字节,并且字节长度不少于包头长度
		while(inBuffer.hasRemaining()){
			if(inBuffer.remaining() < HEAD_LEN){
				break;
			}
			
			//此处获得客户端发送内容的长度,构造相关长度的字节数组
			byte[] data = new byte[2];
			inBuffer.get(data);
			
			//此处将Byte数组,转换成short型
			int len = ChangeTools.ByteToInt2(data, 0);
			
			//读取整个内容的长度
			data = new byte[len];
			inBuffer.get(data);
			
			//包头数组,复制到包头数组
			byte[] headData = new byte[HEAD_LEN];
			System.arraycopy(data, 0, headData, 0, HEAD_LEN);
			
			HeadModel head = parseHead(headData);
			
			
			//获取包体数据
			byte[] bodyData = new byte[head.bodyLen];
			System.arraycopy(headData, 0, data, 0, headData.length);
			System.arraycopy(bodyData, 0, data, headData.length, bodyData.length);
			
			Message msg = MessageFactory.createServerMessage(session, data, headData, bodyData, head.cmdType,
					head.playerId,head.tag);
			
			if(msg == null){
				return null;
			}
			list.add(msg);			
		}
		
		//? 此处的作用需要加深理解
		if (list.size() > 0) {
			inBuffer.compact();
			inBuffer.clear();

			inBuffer.position(inBuffer.position());
			inBuffer.limit(inBuffer.limit());
		}
		
		return list;
	}

你可能感兴趣的:(java,.net,socket,dos,OS)