Reputation: 3132
I am setting up a simple server using sys.net.Socket (cpp, linux).
The server is bound like this:
hostSocket.bind(new Host("0.0.0.0"), 20301);
And I connect to the server like this:
clientSocket.connect(new Host("localhost"), 20301);
If the ports do not match, the connection won't work, so that works as expected.
However, when I "accept" the connection on the server side, and want to print information about the client, I get a random port whenever a new connection is incoming, just never 20301:
var connectedClient : Socket = hostSocket.accept();
trace("Incoming connection from " + connectedClient.peer().host.toString()
+ " on port " + connectedClient.peer().port);
Now I get results like this:
Incoming connection from 127.0.0.1 on port 50977
Incoming connection from 127.0.0.1 on port 50978
Incoming connection from 127.0.0.1 on port 50979
What is going on here? Why is the displayed port not 20301?
Upvotes: 2
Views: 699
Reputation: 1557
Both server and client sockets need to bind to some local address (ip,port) for a connection to happen.
The client simply binds to a local free port, and will very likely change a lot, depending on all other connections happening on your machine.
Finally, Haxe sockets are (sometimes indirectly) wrappers over POSIX sockets; the spec for connect()
says:
If the socket has not already been bound to a local address,
connect()
shall bind it to an address which, ..., is an unused local address.
Upvotes: 2