码迷,mamicode.com
首页 > 编程语言 > 详细

Java--NIO-TCP Socket

时间:2017-02-28 13:50:53      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:break   key   localhost   ace   color   bind   package   figure   select   

1、首先我们使用SocketChannel,实现socket客户端

package com.seeyon.nio.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Created by yangyu on 2017/2/22.
 */
public class Client {

    public static void main(String[] args) {

        try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 8088))) {
            socketChannel.configureBlocking(false);
            ByteBuffer byteBuffer = ByteBuffer.allocate(512);
            socketChannel.write(ByteBuffer.wrap("this is client send message".getBytes()));

            while (true) {
                byteBuffer.clear();
                int readBytes = socketChannel.read(byteBuffer);
                if (readBytes > 0) {
                    byteBuffer.flip();
                    System.out.println(new String(byteBuffer.array(), 0, readBytes));
                    socketChannel.close();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

 

2、使用ServerSocketChannel实现服务端,并且使用Selector

package com.seeyon.nio.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by yangyu on 2017/2/22.
 */
public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        serverChannel.socket().bind(new InetSocketAddress(8088));
        serverChannel.configureBlocking(false);
        Selector selector = Selector.open();
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();

            Set<SelectionKey> readyKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = readyKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();
                if (key.isAcceptable()) {
                    System.out.println("accept");
                    ByteBuffer byteBuffer = ByteBuffer.allocate(512);
                    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    int readKeys = socketChannel.read(byteBuffer);
                    if (readKeys > 0) {
                        byteBuffer.flip();
                        System.out.println(new String(byteBuffer.array(), 0, readKeys));
                    }
                    socketChannel.write(ByteBuffer.wrap("接收到了".getBytes()));

                } else if (key.isReadable()) {
                    System.out.println("read");

                } else if (key.isWritable()) {
                    System.out.println("write");

                }
            }
        }
    }
}

 

Java--NIO-TCP Socket

标签:break   key   localhost   ace   color   bind   package   figure   select   

原文地址:http://www.cnblogs.com/eoss/p/6478150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!