1、
SOCK_DGRAM UDP packets
SOCK_STREAM TCP
不同的协议下的 套接字 数据包
面向数据的
面向连接的
套接字
2、
数据 UDP
文件 TCP
https://www.quora.com/Whats-the-difference-between-SOCK_RAW-and-SOCK_STREAM
Basically SOCK_DGRAM is used for UDP packets, SOCK_STREAM for TCP. As far as reliability issues, it's UDP vs. TCP issues - no guarantee for UDP, guaranteed delivery for TCP.
SOCK_STREAM for data streaming and SOCK_DGRAM for messaging. Note that UDT sockets are connection oriented in all cases.
https://github.com/LabAdvComp/UDR/blob/49ca8b5ab63b46c835c6d90ffc5ab4f1bf26ea0c/udt/doc/doc/socket.htm
https://github.com/LabAdvComp/UDR/blob/49ca8b5ab63b46c835c6d90ffc5ab4f1bf26ea0c/udt/src/core.h
static UDTSOCKET socket(int af, int type = SOCK_STREAM, int protocol = 0);
https://github.com/LabAdvComp/UDR/blob/49ca8b5ab63b46c835c6d90ffc5ab4f1bf26ea0c/udt/src/core.cpp
if ((UDT_STREAM == m_iSockType) && (m_pRcvBuffer->getRcvDataSize() > 0))
s_UDTUnited.m_EPoll.enable_read(m_SocketID, m_sPollID);
else if ((UDT_DGRAM == m_iSockType) && (m_pRcvBuffer->getRcvMsgNum() > 0))
s_UDTUnited.m_EPoll.enable_read(m_SocketID, m_sPollID);
https://stackoverflow.com/questions/5815675/what-is-sock-dgram-and-sock-stream
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.