gpt4 book ai didi

c - Linux SOCK_RAW 和 SOCK_STREAM 有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 11:12:00 25 4
gpt4 key购买 nike

我最近一直在研究网络与 c 代码和密码学,在思考随机问题时,我偶然发现了一段用于数据包嗅探的代码,我对函数 中使用的实际套接字有疑问从()接收()。套接字通过以下 sock 函数 rawSock = socket(AF_INET, SOCK_RAW, 0) 进行初始化。

我理解 SOCK_STREAMSOCK_RAW 是表示整数的宏;但问题不在于值(value)观,而在于结果。

我什么时候可以使用 SOCK_STREAM 而不是 SOCK_RAW ,反之亦然?

我了解使用 SOCK_STREAM 的基本客户端和服务器通信。我正在使用 C 和 Linux

最佳答案

阅读man page .

原型(prototype)

int socket(int domain, int type, int protocol);

类型可以是

   SOCK_STREAM     Provides sequenced, reliable, two-way, connection-
based byte streams. An out-of-band data transmission
mechanism may be supported.

   SOCK_RAW        Provides raw network protocol access.

在一行中,SOCK_STREAM 用于面向连接的套接字,其中底层操作系统创建和管理 L4 (TCP)、L3 和 L2 的 header 。 OTOH SOCK_RAW 提供了对 header 和数据包构造的更细粒度的控制,用户必须在其中构造和提供 header ,还可以管理内容。

详细说明:

Sockets of type SOCK_STREAM are full-duplex byte streams. 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. Once connected, data may be transferred using read(2) and write(2) calls or some variant of the send(2) and recv(2) calls. When a session has been completed a close(2) may be performed. Out-of-band data may also be transmitted as described in send(2) and received as described in recv(2).

SOCK_RAW sockets allow sending of datagrams to correspondents named in sendto(2) calls. Datagrams are generally received with recvfrom(2), which returns the next datagram along with the address of its sender.

关于c - Linux SOCK_RAW 和 SOCK_STREAM 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50888934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com