Student
Student

Reputation: 4563

Netty Client Connection

I have a question when creating a client connection in 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);

Here, why don't we have a bind method that binds the channel to the port (at client side) from where the client connection is initiated ? the only thing we need to provide is to give the server address and port as below:

channel = bootstrap.connect(new InetSocketAddress(host, port));

does this create a new channel at client side or server side? what port this channel is binded in client side?

We do the binding when doing a server side BootStrap as below

 ServerBootstrap b = ...;
 channel = b.bind(b.getOption("localAddress"));

I am confused and not able to understand from which port the client is sending the data to server and what channel is used?

Upvotes: 0

Views: 5482

Answers (2)

Yoav Slomka
Yoav Slomka

Reputation: 372

When you create a client connection using connect(SocketAddress remoteAddress)) you create a channel on the client side. The connect method binds on a local address and then connects to a remote address. when not specifying a local address in the connect method, the method will bind on the local ip and a random port. if you wish to decide which local port to use, you need to use the connect(SocketAddress remoteAddress, SocketAddress localAddress) method.

Upvotes: 0

trustin
trustin

Reputation: 12351

You should use ClientBootstrap.connect(remoteAddress, localAddress) to specify the local address of the socket you are going to create. Alternatively, you can call ClientBootstrap.bind(localAddress).sync(), and then call ClientBootstrap.connect(remoteAddress) to achieve the same thing.

Upvotes: 5

Related Questions