What is SOCK_DGRAM and SOCK_STREAM?

echo9 picture echo9 · Apr 28, 2011 · Viewed 79.3k times · Source

I just came across this strange thing I got to see application is that by default they use SOCK_STREAM function. Why is it so? Is this SOCK_STREAM just creating multiple streams? Or is it the standard SOCK_STREAM function available for creating TCP stream(s)?

I thought tsunami is based on UDP, but still having some features like that of TCP, e.g. TCP fairness, friendlyness, etc.

Could somebody please shed some light on this issue? I am totally confused over this.

Answer

Michael J picture Michael J · May 30, 2012

TCP almost always uses SOCK_STREAM and UDP uses SOCK_DGRAM.

TCP (SOCK_STREAM) is a connection-based protocol. The connection is established and the two parties have a conversation until the connection is terminated by one of the parties or by a network error.

UDP (SOCK_DGRAM) is a datagram-based protocol. You send one datagram and get one reply and then the connection terminates.

  • If you send multiple packets, TCP promises to deliver them in order. UDP does not, so the receiver needs to check them, if the order matters.

  • If a TCP packet is lost, the sender can tell. Not so for UDP.

  • UDP datagrams are limited in size, from memory I think it is 512 bytes. TCP can send much bigger lumps than that.

  • TCP is a bit more robust and makes more checks. UDP is a shade lighter weight (less computer and network stress).

Choose the protocol appropriate for how you want to interact with the other computer.