How to determine the local port?

A

anketm

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
 
K

Keith Thompson

I am writing a simple client using UDP sockets.

Standard C doesn't support UDP sockets, or any kind of networking.

Try a newsgroup specific your system, (possibly comp.unix.programmer).
 
C

CBFalconer

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?

This is all off topic here. See the references in my sig below,
especially the first two.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
 
E

Eric Sosman

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.

The local Port is Taylor's Ten-Year-Old Tawny. The
well-known Ports include Graham's 1977 and Taylor's 1983.
The reserved Port is the Fonseca 1963, the limited (alas!)
supply of Taylor 1948 having been exhausted.
 
C

CBFalconer

Eric said:
The local Port is Taylor's Ten-Year-Old Tawny. The
well-known Ports include Graham's 1977 and Taylor's 1983.
The reserved Port is the Fonseca 1963, the limited (alas!)
supply of Taylor 1948 having been exhausted.

So have some Madiera, m'dear :)

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top