1
votes

I'm working on a socket project and now I'll make a UDP listener for server-side. I have looked some examples and I can't understand this; Why we have to bind UDP socket like "socket.Bind(new IPEndPoint(IPAddress.Any, 3000));"? Namely: in the TCP socket, we bind only our local ip but in UDP socket, we are binding IPAddress.Any. Why we are doing this?

EDIT: What are the wildcard and particular IP Adresses? How can we use them?

2
I think this article could clarify the ANY for UDP.ja_mesa
'In the TCP socket we bind only [to] our local IP' isn't correct. You usually bind a listening socket to ANY as well. Not a real question.user207421
@EJP OK. I need to understand why we need to user ANY Ip or special Ip. Can you help me?Mehmet Fatih Marabaoğlu

2 Answers

1
votes

I found this and I think I was looking for it :)

The wildcard is a special local IP address. It usually means "any" and can only be used for bind operations.

The value of this IP address is 0.0.0.0. If you have two network adapters, one with IP address 1.1.1.1 and one with IP address 2.2.2.2, then you can create a listening socket and bind it to 1.1.1.1 so that the socket will not bind to 2.2.2.2. You can also create a listening socket and bind it to 2.2.2.2, so that it will not bind to 1.1.1.1. If you do not care and want your socket to bind to all network cards, then you bind it to the wildcard address.

Another special value would be 127.0.0.1, meaning that only clients on the same computer could connect to your server.

Link: What does wildcard address in InetSocketAddress mean?

0
votes

You can bind both TCP and UDP sockets to either the wildcard address (INADDR_ANY or IN6ADDR_ANY_INIT) or to a specific address. There is no reason why you would typically bind a TCP socket to a specific IP address while you would bind a UDP socket to a wildcard address.