Discussion:
[asio] Howto bind *client* to a specific port?
for-gmane
2010-10-01 15:03:22 UTC
Permalink
A NewbieQ:

From a client app I want to connect to a server app on a remote host (say port 6666).
A requirement is that the client shall be bound to a defined port (say port 5555).
How to do this in asio?
Does the client need to use an acceptor? (ok, this maybe sounds non-sensical, but who knows... :-)
for-gmane
2010-10-01 20:51:21 UTC
Permalink
Post by for-gmane
From a client app I want to connect to a server app on a remote host (say port 6666).
A requirement is that the client shall be bound to a defined port (say port 5555).
How to do this in asio?
Does the client need to use an acceptor? (ok, this maybe sounds
non-sensical, but who knows... :-)
Solved as follows:
...
tcp::socket s(io_service);
s.open(tcp::v4());
s.set_option(tcp::socket::reuse_address(true));
s.bind(tcp::endpoint(tcp::v4(), uLocalPortToUse));
s.connect(*iterator);
...
for-gmane
2010-10-02 12:15:01 UTC
Permalink
Post by for-gmane
Post by for-gmane
From a client app I want to connect to a server app on a remote host (say port 6666).
A requirement is that the client shall be bound to a defined port (say port 5555).
How to do this in asio?
Does the client need to use an acceptor? (ok, this maybe sounds
non-sensical, but who knows... :-)
...
tcp::socket s(io_service);
s.open(tcp::v4());
s.set_option(tcp::socket::reuse_address(true));
s.bind(tcp::endpoint(tcp::v4(), uLocalPortToUse));
s.connect(*iterator);
...
Just a followup question:

I tried to replace the above s.bind(...) with this one:
s.bind(tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), uLocalPortToUse));
but then, the s.connect(...) call to internet hosts fails.
Is this behaviour correct?
(Ie. connecting from 127.0.0.1 seems not possible to non-local adresses; OTOH this make sense, isn't it?)
OvermindDL1
2010-10-02 22:45:21 UTC
Permalink
Post by for-gmane
Post by for-gmane
From a client app I want to connect to a server app on a remote host (say port 6666).
A requirement is that the client shall be bound to a defined port (say port 5555).
How to do this in asio?
Does the client need to use an acceptor? (ok, this maybe sounds
non-sensical, but who knows... :-)
...
tcp::socket s(io_service);
s.open(tcp::v4());
s.set_option(tcp::socket::reuse_address(true));
s.bind(tcp::endpoint(tcp::v4(), uLocalPortToUse));
s.connect(*iterator);
...
 s.bind(tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),
uLocalPortToUse));
but then, the s.connect(...) call to internet hosts fails.
Is this behaviour correct?  (Ie. connecting from 127.0.0.1 seems not
possible to non-local adresses; OTOH this make sense, isn't it?)
Correct, 127.0.0.1 is not on the network, it is the local loopback
connection (the entire 127/8 range is I think), hence you can only
connect to something on it from the local loopback connection (like
127.0.0.2 or whatnot). If you want it externally visible, you must
put it on a connection that your network card manages (maybe
192.168.1.1 or so? Whatever you are).

Loading...