How to determine the local port? - Unix

This is a discussion on How to determine the local port? - Unix ; 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: ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to determine the local port?

  1. How to determine the local port?

    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


  2. Re: How to determine the local port?

    In article <1116888157.122313.139910@g44g2000cwa.googlegroups. com>,
    anketm@gmail.com wrote:

    > 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.


    Someone already answered you in comp.unix.programmer. Please don't
    multi-post. If you *really* think your question is appropriate to
    multiple groups (I don't think this question qualifies, BTW), you should
    cross-post a single message rather than posting multiple messages. I
    have no idea how you do this in Google's forum-style interface, but with
    a real newsreader you would do it by listing all the groups in the
    "Newsgroups:" header.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***

+ Reply to Thread