SocketChannel 使用open或connect/finishConnect


首先给出一个通用的服务端代码:

package NonBlocking;

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

public class TestOpenAndConnectServer {
    static ServerSocketChannel serverSocketChannel = null;
    static Selector selector = null;

    public static void main(String[] args) throws IOException {
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress(8888));

        selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        int result = 0; int i = 1;
        while ((result = selector.select()) > 0) {
            System.out.println(String.format("selector %dth loop, ready event number is %d", i++, result));
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while(iterator.hasNext()){
                SelectionKey sk = iterator.next();

                if (sk.isAcceptable()) {
                    ServerSocketChannel ss = (ServerSocketChannel)sk.channel();
                    SocketChannel socketChannel = ss.accept();
                    socketChannel.configureBlocking(false);  //也切换非阻塞
                    socketChannel.register(selector, SelectionKey.OP_READ);  //注册read事件
                	System.out.println("接受到新的客户端连接");
                } else if (sk.isReadable()) {
                    System.out.println("有数据可读");
                }

                iterator.remove();
            }
        }
    }
}

使用connect效果

使用connect的客户端代码:

package NonBlocking;

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.SocketChannel;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;

public class TestOpenAndConnectClient {
    static SocketChannel socketChannel = null;
    static Selector selector = null;

    public static void main(String[] args) throws IOException {
        socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        boolean connected = socketChannel.connect(new InetSocketAddress("127.0.0.1",8888));
        System.out.println(connected);
        selector = Selector.open();

        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        int result = 0; int i = 1;
        while((result = selector.select()) > 0) {
            System.out.println(String.format("selector %dth loop, ready event number is %d", i++, result));
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey sk = iterator.next();

                if (sk.isConnectable()) {
                    SocketChannel sc = (SocketChannel)sk.channel();
                    if (sc.finishConnect()) {
                        System.out.println("与服务端建立连接成功");
                    } else {
                        System.out.println("与服务端建立连接失败");
                    }
                    sc.register(selector, SelectionKey.OP_READ);
                } else if (sk.isReadable()) {
                    System.out.println("有数据可读");
                }

                iterator.remove();
            }
        }
    }
}

在服务端已经运行后,再运行客户端,效果如下:

false
selector 1th loop, ready event number is 1
与服务端建立连接成功

如果服务端没有运行,再运行客户端,效果如下:可见在执行sc.finishConnect()抛出了异常。

false
selector 1th loop, ready event number is 1
Exception in thread "main" java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
	at NonBlocking.TestOpenAndConnectClient.main(TestOpenAndConnectClient.java:34)

正如connect的api文档所解释:

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation. If the connection is established immediately, as can happen with a local connection, then this method returns true. Otherwise this method returns false and the connection operation must later be completed by invoking the finishConnect method.

一个channel在非阻塞模式下执行connect后,如果连接能马上建立好则返回true,否则完成false。如果返回false,那么只能通过之后调用finishConnect来判断连接是否完成。

finishConnect的api文档:

If the connection operation failed then invoking this method will cause an appropriate IOException to be thrown.

如果连接操作已经失败了,那么将会抛出异常。

If this channel is in non-blocking mode then this method will return false if the connection process is not yet complete.

如果连接操作还未完成好,那么将返回false。

但由于我们是通过Selector来触发事件,所以connect事件到来时,这个连接要么已经成功,要么已经失败(抛出异常的形式告诉我们)。

在select循环外使用finishConnect

客户端修改部分代码如下:

package NonBlocking;

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.SocketChannel;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;

public class TestOpenAndConnectClient {
    static SocketChannel socketChannel = null;
    static Selector selector = null;

    public static void main(String[] args) throws IOException {
        socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        boolean connected = socketChannel.connect(new InetSocketAddress("127.0.0.1",8888));
        System.out.println("socketChannel.connect result is "+connected);
        System.out.println("socketChannel.finishConnect result is "+socketChannel.finishConnect());  //加了这句
        selector = Selector.open();

        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        SelectionKey sek = socketChannel.keyFor(selector);
        int result = 0; int i = 1;
        while((result = selector.select()) > 0) {
            System.out.println(String.format("selector %dth loop, ready event number is %d", i++, result));
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey sk = iterator.next();

                if (sk.isConnectable()) {
                    SocketChannel sc = (SocketChannel)sk.channel();
                    if (sc.finishConnect()) {
                        System.out.println("与服务端建立连接成功");
                    } else {
                        System.out.println("与服务端建立连接失败");
                    }
                    sc.register(selector, SelectionKey.OP_READ);
                } else if (sk.isReadable()) {
                    System.out.println("有数据可读");
                }

                iterator.remove();
            }
        }
    }
}

如果服务端没有运行,再运行客户端,效果如下:可见还在执行sc.finishConnect()抛出了异常。

socketChannel.connect result is false
socketChannel.finishConnect result is false
selector 1th loop, ready event number is 1
Exception in thread "main" java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
	at NonBlocking.TestOpenAndConnectClient.main(TestOpenAndConnectClient.java:35)

在服务端已经运行后,再运行客户端,效果如下:

socketChannel.connect result is false
socketChannel.finishConnect result is true

Process finished with exit code 0

可见虽然connect返回了false,但马上finishConnect也返回了true。但是之后的循环就直接退出了,一行都没有执行。原因应该是finishConnect在连接成功时会消耗一次OP_CONNECT事件,而注册事件时又只注册OP_CONNECT事件,所以select阻塞了一下后,只有返回0了,导致循环直接退出。

  • 看来要使用select循环的话,就不应该在循环外使用finishConnect了。

使用open

package NonBlocking;

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

public class TestOpenAndConnectClient {
    static SocketChannel socketChannel = null;
    static Selector selector = null;

    public static void main(String[] args) throws IOException {
        socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8888));
        socketChannel.configureBlocking(false);

        selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_READ);
        int result = 0; int i = 1;
        while((result = selector.select()) > 0) {
            System.out.println(String.format("selector %dth loop, ready event number is %d", i++, result));
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey sk = iterator.next();

                if (sk.isReadable()) {
                    System.out.println("有数据可读");
                }

                iterator.remove();
            }
        }
    }
}

运行后没有啥效果,因为一直等待读就绪,但没有可读数据过来。

如果之前没有运行服务端,再运行客户端:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
	at sun.nio.ch.Net.connect0(Native Method)
	at sun.nio.ch.Net.connect(Net.java:454)
	at sun.nio.ch.Net.connect(Net.java:446)
	at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
	at java.nio.channels.SocketChannel.open(SocketChannel.java:189)
	at NonBlocking.TestOpenAndConnectClient.main(TestOpenAndConnectClient.java:15)

This convenience method works as if by invoking the open() method, invoking the connect method upon the resulting socket channel, passing it remote, and then returning that channel.

看来此方法,就是一个方便方法,相当于在返回的channel上调用了connect,并返回给你这个channel。从上面的报错信息也能看出。

总结

  • 调用connect后除非连接能马上建立能返回true,否则就返回false。连接是否能建立要通过后续的finishConnect判断。
  • 在select循环里,先检测SelectionKey是否isConnectable为true,如果是则进入分支,再执行SocketChannel.finishConnect。若连接成功,finishConnect返回真;若连接失败,则抛出异常。
  • 调用connect后底层开始TCP的三次握手,握手在一定时间内结束,握手结果要么成功,要么失败。
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Selector实现SocketChannel传输文件,可以按照以下步骤进行操作: 1. 创建一个Selector对象,并将SocketChannel注册到该Selector上。可以使用`Selector.open()`方法创建Selector对象,然后使用`channel.register(selector, SelectionKey.OP_WRITE)`将SocketChannel注册到Selector上,指定感兴趣的事件为写操作。 2. 打开文件并创建一个文件通道(FileChannel)来读取文件内容。可以使用`FileInputStream`和`FileChannel.open()`方法来打开文件并获取文件通道。 3. 在循环中,使用Selector的`select()`方法等待就绪的事件。如果有就绪的事件,可以使用`selector.selectedKeys()`方法获取就绪的SelectionKey集合。 4. 遍历就绪的SelectionKey集合,处理每个就绪的事件。如果事件是写操作(`SelectionKey.OP_WRITE`),则从文件通道读取数据,并将数据写入SocketChannel中。可以使用`FileChannel.read()`方法从文件通道读取数据,然后使用`SocketChannel.write()`方法将数据写入SocketChannel。 5. 检查是否已经将文件的所有数据发送完毕。可以使用FileChannel的`position()`方法获取当前文件的读取位置,如果读取位置等于文件大小,则表示所有数据已经发送完毕。 6. 关闭文件通道和SocketChannel,并释放资源。 下面是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; public class FileTransferExample { public static void main(String[] args) { try { Selector selector = Selector.open(); SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress("localhost", 12345)); socketChannel.register(selector, SelectionKey.OP_CONNECT); while (true) { selector.select(); Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); keyIterator.remove(); if (key.isConnectable()) { SocketChannel channel = (SocketChannel) key.channel(); if (channel.isConnectionPending()) { channel.finishConnect(); } channel.register(selector, SelectionKey.OP_WRITE); } if (key.isWritable()) { FileChannel fileChannel = FileChannel.open(Path.of("path/to/file")); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = fileChannel.read(buffer); if (bytesRead == -1) { fileChannel.close(); socketChannel.close(); System.out.println("File transfer completed."); return; } buffer.flip(); socketChannel.write(buffer); buffer.clear(); } } } } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个简单的示例代码,并没有处理所有的异常情况。在实际开发中,还需要考虑处理连接错误、读写错误等异常情况,并进行适当的错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值