- what IP? is entered in the public network ip, if the inetHost parameter is entered
What vendor is the - server from? Ali Yun? Tencent Cloud?
- Server executes
ifconfig
screenshot and paste
use ifconfig to check whether the so-called public network ip is in the network card configuration.
may be on the cloud. The public network IP you are talking about is not the IP, on your machine but the IP, mapped from the public network IP to your CVM. So, you failed to bind IP with netty
.
although I have adopted the answer, I still want to answer this question.
first of all, for a program, the IP it binds can only be the IP address of a network card on its machine (whether physical or virtual machine), which you can run ifconfig
on the machine.
secondly, the so-called binding means to specify which destination IP IP packets the program can listen to. For example, the machine has two network cards An and BIP, IP address is AIP and BIP, your program binds AIP, then the operating system will only forward the IP packets destined to AIP to your program. 0.0.0.0
is special and represents the ability to forward IP packets to your program whose destination IP is any IP on the machine.
finally, why can your machine be accessed through the public network IP? This is because the cloud service provider has made a NAT, for you, and your machine does not know this address, and it does not belong to any network card on your machine, so you cannot bind it.
PS. You should learn some basic network knowledge. You can find a book by CCNA or HCNA.
The bootstrap class of
Netty provides a container for the application's network layer configuration, which involves binding a process to a specified port, or connecting a process to another process running on a specified port on a specified host.
"server" and "client" actually represent different network behaviors: whether to listen for incoming connections or establish connections to one or more processes.
connection-oriented protocols: keep in mind that, strictly speaking, "connection" is a connection-only protocol, such as TCP, which ensures the orderly delivery of messages between two connection endpoints.
therefore, there are two types of bootstraps: one for the client (simply called Bootstrap),) and the other for the server. No matter which protocol your application uses or what type of data it processes, the only thing that determines which bootstrap class it uses is whether it is a client or a server.
the first difference between these two types of bootstrap classes has been discussed: ServerBootstrap will bind to a port because the server must listen for connections, while Bootstrap will be used by client applications that want to connect to remote nodes
Code case: client using NIO TCP transport
EventLoopGroup group = new NioEventLoopGroup();
//BootstrapChannel
Bootstrap bootstrap = new Bootstrap();
//EventLoopGroupChannelEventLoop
bootstrap.group(group)
//Channel
.channel(NioSocketChannel.class)
//ChannelChannelInboundHandler
.handler(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf)
throws Exception {
System.out.println("Received data");
}
});
ChannelFuture future = bootstrap.connect(
//
new InetSocketAddress("www.myself.com",80)
);
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess()){
System.out.println("Connection established");
} else {
System.out.println("Connection attempt failed");
channelFuture.cause().printStackTrace();
}
}
});
implement server boot
NioEventLoopGroup group = new NioEventLoopGroup();
//ServerBootstrap
ServerBootstrap bootstrap = new ServerBootstrap();
//EventLoopGroupChannelEventLoop
bootstrap.group(group)
//Channel
.channel(NioServerSocketChannel.class)
.childHandler(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf)
throws Exception {
System.out.println("Received data");
}
});
//ServerBootstrapChannel
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess()){
System.out.println("Server bound");
} else {
System.out.println("Bound attempt failed");
channelFuture.cause().printStackTrace();
}
}
});
client boot
//ServerBootstrapChannel
ServerBootstrap bootstrap = new ServerBootstrap();
//EventLoopGroupChannelEventLoop
bootstrap.group(new NioEventLoopGroup(),new NioEventLoopGroup())
//Channel
.channel(NioServerSocketChannel.class)
//ChannelInitializerImplChannelPipeline
.childHandler(new ChannelInitializerImpl());
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));
future.sync();*/
/*//AttributeKey
final AttributeKey<Integer> id = AttributeKey.valueOf("ID");
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
//AttributeKey
Integer idValue = ctx.channel().attr(id).get();
//do something with the idValue
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf)
throws Exception {
System.out.println("Received data");
}
});
//ChannelOptionconnectbindChannel
bootstrap.option(ChannelOption.SO_KEEPALIVE,true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,5000);
//id
bootstrap.attr(id,123456);
ChannelFuture future = bootstrap.connect(new InetSocketAddress("www.myself.com",80));
future.syncUninterruptibly();