Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

winsock question

Status
Not open for further replies.

wduty

Programmer
Jun 24, 2000
271
US
How do I get the local port of a connected socket?
--Will Duty
wduty@radicalfringe.com

 
getsockname() gives you (via a passed in pointer) access to a sockaddr structure for the socket - then just cast that to a sockaddr_in and access the sin_port member.
 
I tried that but the compiler says you can't make that cast. I assume you mean something like this?

sockaddr sa;
int sasize = sizeof(sa);
getsockname(msMain, &sa, &sasize);
sockaddr_in sintemp = (sockaddr_in)sa;
int iLocalPort = sintemp.sin_port;
 
You have a sample in the following article from MSDN
"Example Code for Implementing a Winsock Service with an RnR Publication"

seems like you have to use the ntoh function first.

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top