Unix Programming - How to determine the local port?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > How to determine the local port?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to determine the local port?
anketm@gmail.com

2005-05-23, 5:59 pm

I am writing a simple client using UDP sockets.
I am wondering if there is any way to determine what local port a
socket was bound to when bind() is not explicitly called.

This is what my program looks like:

sock = socket(PF_INET, SOCK_DGRAM, 0);
sendto(sock, message, strlen(message) + 1, 0, (struct sockaddr *)
&server, sizeof(server));

Note that bind() was not called, and the socket was arbitrarily(?)
bound to a local port by the kernel. How do I determine what port it
was bound to?

Thanks.
Anket Mathur

Måns Rullgård

2005-05-23, 8:49 pm

anketm@gmail.com writes:

> I am writing a simple client using UDP sockets.
> I am wondering if there is any way to determine what local port a
> socket was bound to when bind() is not explicitly called.
>
> This is what my program looks like:
>
> sock = socket(PF_INET, SOCK_DGRAM, 0);
> sendto(sock, message, strlen(message) + 1, 0, (struct sockaddr *)
> &server, sizeof(server));
>
> Note that bind() was not called, and the socket was arbitrarily(?)
> bound to a local port by the kernel. How do I determine what port it
> was bound to?


getsockname

--
Måns Rullgård
mru@inprovide.com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com