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?

    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

+ Reply to Thread