Netty客户端连接

Stu*_*ent 0 netty

在netty中创建客户端连接时我有一个问题.

Configuring a channel
Options are used to configure a channel:

 ClientBootstrap b = ...;

 // Options for a new channel
 b.setOption("remoteAddress", new InetSocketAddress("example.com", 8080));
 b.setOption("tcpNoDelay", true);
 b.setOption("receiveBufferSize", 1048576);
Run Code Online (Sandbox Code Playgroud)

在这里,为什么我们没有绑定方法将通道绑定到客户端连接启动的端口(在客户端)?我们唯一需要提供的是提供服务器地址和端口,如下所示:

channel = bootstrap.connect(new InetSocketAddress(host, port));
Run Code Online (Sandbox Code Playgroud)

这会在客户端或服务器端创建新的通道吗?这个频道在客户端绑定的端口是什么?

我们在执行服务器端BootStrap时执行绑定,如下所示

 ServerBootstrap b = ...;
 channel = b.bind(b.getOption("localAddress"));
Run Code Online (Sandbox Code Playgroud)

我很困惑,无法理解客户端将数据发送到服务器的端口以及使用的通道是什么?

tru*_*tin 5

您应该使用ClientBootstrap.connect(remoteAddress, localAddress)指定要创建的套接字的本地地址.或者,您可以调用ClientBootstrap.bind(localAddress).sync(),然后调用ClientBootstrap.connect(remoteAddress)以实现相同的功能.