There are 65536 udp and 65536 tcp ports on each IP address, and you can have sockets on all local sockets (listening 0.0.0.0) or just one, including just localhost (127.0.0.1). Usually, you just use all or localhost. This is IPV4; IPV6 goes wild in lots of ways besides longer (larger) numbers for IP, like anycast addresses.
The tool lsof from Perdue will tell you about all open files, including sockets that can be listening, connected or just open.
- Connected is bound to a remote IP and port on the same IP Protocol (TCP or UDP).
- Listening is more a TCP thing, but an open UDP port handler can spin off connected sockets.
- An open UDP socket can take in UDP packets from all IPs and ports, and can send to the UDP protocol all IPs and ports. DNS is a great example -- sitting there reading packets from everyone, and for every packet read, sending out one packet, either an answer or a forwarded question. It has to keep trackof forwarded questions (recursion) so it can forward answers when they arrive. UDP does not include auto-retransmit, but DNS is an inquiry, so you can just ask again. The first query is not a waste, as it may have stored the answer more closely.
BTW, UDP can be used with broadcast IP on send to send one packet to all open ports of the specified number on all IP of that network. It can be used with MBone IPs to multicast, where lost packets can be recovered on intermediate hosts.