Re: How to determine the local port?



anketm@xxxxxxxxx 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.
>
> 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)


.



Relevant Pages

  • Re: freeing port - winsock
    ... When I create and destroy many sockets without specifying the local port to bind, It seems that this local port is not free after using the function closesocket. ... It's goal is to create a socket, connect to a remote server on port 80, close the socket and to loop on these actions. ... int host ... SOCKET sock = INVALID_SOCKET; ...
    (comp.programming)
  • Re: Sockets -- "bind" on a TCP client socket.
    ... It creates the socket, and then does a "bind" ... socket to any port on any interface. ... call to get the local port number. ...
    (comp.unix.questions)
  • Re: socket from python to ruby
    ... recv = self.sckt.recv ... Since you are using TCP and calling 'connect', it's a client socket. ... If you really need to bind the local port for an outbound connection, ...
    (comp.lang.ruby)
  • How to determine the local port?
    ... I am writing a simple client using UDP sockets. ... Note that bind() was not called, and the socket was arbitrarily ... bound to a local port by the kernel. ...
    (comp.lang.c)
  • How to determine the local port?
    ... I am writing a simple client using UDP sockets. ... Note that bind() was not called, and the socket was arbitrarily ... bound to a local port by the kernel. ...
    (comp.os.linux.networking)