keytool -genkey -alias mysocket -keyalg RSA -keystore mysocket.jks
7.使用nio实现socket通信
java.nio包是Java在1.4之后增加的,用来提高I/O操作的效率。在nio包中主要包括以下几个类或接口:
* Buffer:缓冲区,用来临时存放输入或输出数据。
* Charset:用来把Unicode字符编码和其它字符编码互转。
* Channel:数据传输通道,用来把Buffer中的数据写入到数据源,或者把数据源中的数据读入到Buffer。
* Selector:用来支持异步I/O操作,也叫非阻塞I/O操作。
把对象转为字节数组
- public static byte[] toBytes(Object object) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = null;
- try {
- oos = new ObjectOutputStream(baos);
- oos.writeObject(object);
- byte[] bytes = baos.toByteArray();
- return bytes;
吧字节数组转为对象
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- ObjectInputStream ois = null;
- try {
- ois = new ObjectInputStream(bais);
- Object object = ois.readObject();
- return object;
server端的写法
- Selector selector = null;
- ServerSocketChannel serverSocketChannel = null;
- try {
- selector = Selector.open();
- serverSocketChannel = ServerSocketChannel.open();
- serverSocketChannel.configureBlocking(false);
- serverSocketChannel.socket().setReuseAddress(true);
- serverSocketChannel.socket().bind(new InetSocketAddress(10000));
- serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
- while (selector.select() > 0) {
- Iterator<SelectionKey> it = selector.selectedKeys().iterator();
- while (it.hasNext()) {
- SelectionKey readyKey = it.next();
- it.remove();
- execute((ServerSocketChannel) readyKey.channel());
- private static void execute(ServerSocketChannel serverSocketChannel) throws IOException {
- SocketChannel socketChannel = null;
- try {
- socketChannel = serverSocketChannel.accept();
- MyRequestObject myRequestObject = receiveData(socketChannel);
- logger.log(Level.INFO, myRequestObject.toString());
-
- MyResponseObject myResponseObject = new MyResponseObject(
- "response for " + myRequestObject.getName(),
- "response for " + myRequestObject.getValue());
- sendData(socketChannel, myResponseObject);
- MyRequestObject myRequestObject = null;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ByteBuffer buffer = ByteBuffer.allocate(1024);
-
- try {
- byte[] bytes;
- int size = 0;
- while ((size = socketChannel.read(buffer)) >= 0) {
- buffer.flip();
- bytes = new byte[size];
- buffer.get(bytes);
- baos.write(bytes);
- buffer.clear();
- }
- bytes = baos.toByteArray();
- Object obj = SerializableUtil.toObject(bytes);
- myRequestObject = (MyRequestObject)obj;
- sendData(SocketChannel socketChannel, MyResponseObject myResponseObject) throws IOException {
- byte[] bytes = SerializableUtil.toBytes(myResponseObject);
- ByteBuffer buffer = ByteBuffer.wrap(bytes);
- socketChannel.write(buffer);
client的写法
- socketChannel = SocketChannel.open();
- SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);
- socketChannel.connect(socketAddress);
-
- MyRequestObject myRequestObject = new MyRequestObject("request_" + idx, "request_" + idx);
- logger.log(Level.INFO, myRequestObject.toString());
- sendData(socketChannel, myRequestObject);
-
- MyResponseObject myResponseObject = receiveData(socketChannel);
- logger.log(Level.INFO, myResponseObject.toString());
- private void sendData(SocketChannel socketChannel, MyRequestObject myRequestObject) throws IOException {
- byte[] bytes = SerializableUtil.toBytes(myRequestObject);
- ByteBuffer buffer = ByteBuffer.wrap(bytes);
- socketChannel.write(buffer);
- socketChannel.socket().shutdownOutput();
- private MyResponseObject receiveData(SocketChannel socketChannel) throws IOException {
- MyResponseObject myResponseObject = null;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- try {
- ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
- byte[] bytes;
- int count = 0;
- while ((count = socketChannel.read(buffer)) >= 0) {
- buffer.flip();
- bytes = new byte[count];
- buffer.get(bytes);
- baos.write(bytes);
- buffer.clear();
- }
- bytes = baos.toByteArray();
- Object obj = SerializableUtil.toObject(bytes);
- myResponseObject = (MyResponseObject) obj;
- socketChannel.socket().shutdownInput();
return myResponseObject;
上面的例子都需要先运行server再运行client。