2.服务端绑定网卡
在多网卡环境下,我们可能通过指定网卡所对应的 IP 提供 Server 服务。此时,你所需要做的就是选择带host
参数的AioQuickServer
构造方法。
例如:当前服务器存在两个IP,分别为:127.0.0.1
和192.168.3.29
。
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=6463<RXCSUM,TXCSUM,TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether f8:4d:89:8c:43:c8
inet6 fe80::c98:fb16:21e8:8859%en0 prefixlen 64 secured scopeid 0xe
inet 192.168.3.29 netmask 0xffffff00 broadcast 192.168.3.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
倘若我们在构造AioQuickServer
示例时指定了host
为192.168.3.29
,便可将端口号为:8888 的 Server 服务绑定在 IP 为 192.168.3.29
对应的网卡上。
new AioQuickServer("192.168.3.29",8888, new StringProtocol(), processor);
1
如果客户端此时使用localhost
去请求端口:8888,则会触发异常:
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Net.java:482)
at sun.nio.ch.Net.connect(Net.java:474)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:647)
at org.smartboot.socket.enhance.EnhanceAsynchronousSocketChannel.doConnect(EnhanceAsynchronousSocketChannel.java:321)
at org.smartboot.socket.enhance.EnhanceAsynchronousSocketChannel.connect(EnhanceAsynchronousSocketChannel.java:224)
at org.smartboot.socket.transport.AioQuickClient.start(AioQuickClient.java:146)
at org.smartboot.socket.transport.AioQuickClient.start(AioQuickClient.java:197)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9