EDIT: Restating the problem, if I am listening to port 54321 and a local process listening to port 12345 connects to me, creating socket s
, how do I actually find the port it is listening on?
sockaddr_in addr;
int len = sizeof(addr);
getpeername(s, (sockaddr*)&addr, &len);
cout << string(inet_ntoa(addr.sin_addr)) << ":" << ntohs(addr.sin_port) << endl;
Shouldn't the output be 127.0.0.1:12345
? Instead I get 127.0.0.1:62305
, or some other arbitrary port number. Is this an error on my part, or is it supposed to be this way?