程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java--NIO-TCP Socket

Java--NIO-TCP Socket

編輯:關於JAVA

Java--NIO-TCP Socket。本站提示廣大學習愛好者:(Java--NIO-TCP Socket)文章只能為提供參考,不一定能成為您想要的結果。以下是Java--NIO-TCP Socket正文


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");

                }
            }
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved