[英]What's the difference between Linux SOCK_RAW and SOCK_STREAM?
I've been studying networking with c code and cryptography lately and upon pondering random questions I stumbled across a block of code that's used for packet sniffing and I had a question on the actual socket that gets used in the function recvfrom()
. 我最近一直在用c代码和密码学研究网络,在思考随机问题后,我偶然发现了一段用于数据包嗅探的代码,我对函数
recvfrom()
使用的实际套接字有疑问。 The socket gets initialized through the following sock function rawSock = socket(AF_INET, SOCK_RAW, 0)
. 套接字通过以下sock函数
rawSock = socket(AF_INET, SOCK_RAW, 0)
初始化。
I understand that SOCK_STREAM
and SOCK_RAW
are macros that represent an integer; 我知道
SOCK_STREAM
和SOCK_RAW
是表示整数的宏; but the question isn't about the values, it's about the results. 但问题不在于价值,而在于结果。
When would I use SOCK_STREAM
over SOCK_RAW
and vice versa? 我何时使用
SOCK_STREAM
不是SOCK_RAW
,反之亦然?
I understand basic client and server communications using SOCK_STREAM
. 我理解使用
SOCK_STREAM
基本客户端和服务器通信。 I'm working with C and in Linux 我正在使用C和Linux
For the prototype 对于原型
int socket(int domain, int type, int protocol);
The types can be 类型可以
SOCK_STREAM Provides sequenced, reliable, two-way, connection-
based byte streams. An out-of-band data transmission
mechanism may be supported.
or 要么
SOCK_RAW Provides raw network protocol access.
In one line, SOCK_STREAM
is for connection oriented sockets, where the underlying OS creates and manages the headers for L4 (TCP), L3 and L2. 在一行中,
SOCK_STREAM
用于面向连接的套接字,其中底层操作系统创建和管理L4(TCP),L3和L2的标头。 OTOH SOCK_RAW
provides more fine-grained control over header and packet construction, where the user has to construct and supply the headers and can also manage the contents. OTOH
SOCK_RAW
提供了对标头和数据包构造的更细粒度的控制,用户必须构建和提供标头,并且还可以管理内容。
To elaborate: 详细说明:
Sockets of type SOCK_STREAM are full-duplex byte streams.
SOCK_STREAM类型的套接字是全双工字节流。 They do not preserve record boundaries.
它们不保留记录边界。 A stream socket must be in a connected state before any data may be sent or received on it.
在可以在其上发送或接收任何数据之前,流套接字必须处于连接状态。 A connection to another socket is created with a connect(2) call.
使用connect(2)调用创建与另一个套接字的连接。 Once connected, data may be transferred using read(2) and write(2) calls or some variant of the send(2) and recv(2) calls.
连接后,可以使用read(2)和write(2)调用或send(2)和recv(2)调用的某些变体来传输数据。 When a session has been completed a close(2) may be performed.
当会话已经完成时,可以执行关闭(2)。 Out-of-band data may also be transmitted as described in send(2) and received as described in recv(2).
也可以如发送(2)中所述发送带外数据,并如recv(2)中所述接收带外数据。
and 和
SOCK_RAW sockets allow sending of datagrams to correspondents named in sendto(2) calls.
SOCK_RAW套接字允许向sendto(2)调用中指定的通信方发送数据报。 Datagrams are generally received with recvfrom(2), which returns the next datagram along with the address of its sender.
数据报通常与recvfrom(2)一起接收,它返回下一个数据报以及其发送者的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.