I don’t mean how to connect to a socket. What should I know about UDP programming?
- Do I need to worry about bad data in my socket?
- I should assume if I send 200bytes I may get 120 and 60 bytes separately?
- Should I worry about another connection sending me bad data on the same port?
- If data doesnt arrive typically how long may I (typically) not see data for (250ms? 1 second? 1.75sec?)
What do I really need to know?
When you’re sending UDP datagrams your read size will equal your write size. This is because UDP is a datagram protocol, vs TCP’s stream protocol. However, you can only write data up to the size of the MTU before the packet could be fragmented or dropped by a router. For general internet use, the safe MTU is 576 bytes including headers.
You don’t have a connection, you have a port. You will receive any data sent to that port, regardless of where it’s from. It’s up to you to determine if it’s from the right address.
Data can be lost forever, data can be delayed, and data can arrive out of order. If any of those things bother you, use TCP. Writing a reliable protocol on top of UDP is a very non trivial task and there is no reason to do so for almost all applications.