Sending and receiving UDP packets?

The receiver must set port of receiver to match port set in sender DatagramPacket. For debugging, try listening on port > 1024 (e.g. 5000 or 9000). Ports < 1024 are normally used by system services and need admin access to bind on such a port. If the receiver sends packet to the hard-coded port it’s … Read more

UDP-Broadcast on all interfaces

First of all, you should consider broadcast obsolete, specially INADDR_BROADCAST ( Your question highlights exactly one of the reasons that broadcast is unsuitable. It should die along with IPv4 (hopefully). Note that IPv6 doesn’t even have a concept of broadcast (multicast is used, instead). INADDR_BROADCAST is limited to the local link. Nowadays, it’s only visible … Read more

Setting the source IP for a UDP socket

Nikolai, using a separate socket and bind(2) for each address or messing with routing tables is often not a feasible option e.g. with dynamic addresses. A single IP_ADDRANY-bound UDP server should be able to appear to respond on the same dynamically-assigned IP address a packet is received on. Luckily, there is another way. Depending on … Read more

How to do Network discovery using UDP broadcast

It’s very simple to make same thing in C# Server: var Server = new UdpClient(8888); var ResponseData = Encoding.ASCII.GetBytes(“SomeResponseData”); while (true) { var ClientEp = new IPEndPoint(IPAddress.Any, 0); var ClientRequestData = Server.Receive(ref ClientEp); var ClientRequest = Encoding.ASCII.GetString(ClientRequestData); Console.WriteLine(“Recived {0} from {1}, sending response”, ClientRequest, ClientEp.Address.ToString()); Server.Send(ResponseData, ResponseData.Length, ClientEp); } Client: var Client = new UdpClient(); … Read more

Difference between TCP and UDP?

TCP is a connection oriented stream over an IP network. It guarantees that all sent packets will reach the destination in the correct order. This imply the use of acknowledgement packets sent back to the sender, and automatic retransmission, causing additional delays and a general less efficient transmission than UDP. UDP is a connection-less protocol. … Read more

What are the use cases of SO_REUSEADDR?

For TCP, the primary purpose is to restart a closed/killed process on the same address. The flag is needed because the port goes into a TIME_WAIT state to ensure all data is transferred. If two sockets are bound to the same interface and port, and they are members of the same multicast group, data will … Read more