There are different types of Internet sockets.
You may have seen SOCK_DGRAM
, SOCK_STREAM
, and SOCK_RAW
before. The following are the brief definitions of the most popular ones:
Stream sockets (
SOCK_STREAM
): These types of sockets use Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP). It ensures that sent data is sequenced and unduplicated and it is a reliable socket. A sample usage would besocket(PF_INET, SOCK_STREAM, 0);
.Datagram sockets (
SOCK_DGRAM
): These types of sockets use User Datagram Protocol (UDP). These sockets are known as connectionless sockets. It should be noted that they are unreliable and the data may arrive out of sequence. There may be duplication as well. A sample usage would besocket(PF_INET, SOCK_DGRAM, 0);
.Raw sockets (
SOCK_RAW
): These types of socket neither use TCP nor UDP. It directly communicates with the IP layer. This may be useful to build a new protocol and is a lower-level approach.