UDP-Broadcast on all interfaces

First of all, you should consider broadcast obsolete, specially INADDR_BROADCAST (255.255.255.255). 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

C# little endian or big endian?

C# itself doesn’t define the endianness. Whenever you convert to bytes, however, you’re making a choice. The BitConverter class has an IsLittleEndian field to tell you how it will behave, but it doesn’t give the choice. The same goes for BinaryReader/BinaryWriter. My MiscUtil library has an EndianBitConverter class which allows you to define the endianness; … Read more

Get destination address of a received UDP packet

I’ve constructed an example that extracts the source, destination and interface addresses. For brevity, no error checking is provided. // sock is bound AF_INET socket, usually SOCK_DGRAM // include struct in_pktinfo in the message “ancilliary” control data setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)); // the control data is dumped here char cmbuf[0x100]; // the remote/source sockaddr … Read more