This section describes criteria to help you choose the appropriate
socket type for an application program.
If you are communicating with an existing application program,
you must use the same protocols as the existing application program.
For example, if you communicate with an application that uses TCP,
you must use stream sockets. For other application programs, you should
consider the following factors:
- Reliability. Stream sockets provide the
most reliable connection. Datagram sockets are unreliable, because
packets can be discarded, corrupted, or duplicated during transmission.
This may be acceptable if the application program does not require
reliability, or if the application program implements the reliability
on top of the sockets interface. The trade-off is the increased performance
available with datagram sockets.
- Performance. The overhead associated with
reliability, flow control, packet reassembly, and connection maintenance
degrade the performance of stream sockets in comparison with datagram
sockets.
- Data transfer. Datagram sockets impose a
limit on the amount of data transferred in a single transaction. If
you send less than 2048 bytes at a time, use datagram sockets. As
the amount of data in a single transaction increases, use stream sockets.