c - c socket() 函数中的类型和协议(protocol)有什么区别?

标签 c sockets tcp

我正在阅读并试图了解 C,我尝试用 UDP 编写一个 Java 聊天 >TCP 几年前,尽管我成功了……但我做不到。 我想对套接字进行编程,并且正在阅读大量文档,但总有一部分不清楚,每个踢文档都有缺陷。 例如,有一个关于

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

我将使用的域显然是AF_INET,如果我想要一个TCP Socket,我认为类型应该是SOCK_STREAM,但是什么是协议(protocol)?文档说它应该是 0...为什么???这是什么?

最佳答案

来自套接字的手册页:

The protocol specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0. However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner. The protocol number to use is specific to the “communication domain” in which communication is to take place; see protocols(5). See getprotoent(3) on how to map protocol name strings to protocol numbers.

根据协议(protocol)手册页:

This file is a plain ASCII file, describing the various DARPA internet protocols that are available from the TCP/IP subsystem. It should be consulted instead of using the numbers in the ARPA include files, or, even worse, just guessing them. These numbers will occur in the protocol field of any IP header.

Each line is of the following format:

protocol number aliases ...

...

/etc/protocols The protocols definition file.

在我的 linux 机器上的/etc/protocols 文件中:

ip      0       IP              # internet protocol, pseudo protocol number
hopopt  0       HOPOPT          # hop-by-hop options for ipv6
icmp    1       ICMP            # internet control message protocol
igmp    2       IGMP            # internet group management protocol
ggp     3       GGP             # gateway-gateway protocol
ipencap 4       IP-ENCAP        # IP encapsulated in IP (officially ``IP'')
st      5       ST              # ST datagram mode
tcp     6       TCP             # transmission control protocol
cbt     7       CBT             # CBT, Tony Ballardie <A.Ballardie@cs.ucl.ac.uk>
egp     8       EGP             # exterior gateway protocol
igp     9       IGP             # any private interior gateway (Cisco: for IGRP)
bbn-rcc 10      BBN-RCC-MON     # BBN RCC Monitoring
...

根据 getprotocol 的手册页:

The getprotobyname() function returns a protoent structure for the entry from the database that matches the protocol name name. A connection is opened to the database if necessary.

...

The protoent structure is defined in as follows:

struct protoent {
    char  *p_name;       /* official protocol name */
    char **p_aliases;    /* alias list */
    int    p_proto;      /* protocol number */
}

因此,如果您将“ip”传递给 getprotobyname(),它将返回 0,这是您正在使用的数字。但是直接使用 0 总是安全的,即使您不知道协议(protocol)的名称。

关于c - c socket() 函数中的类型和协议(protocol)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475039/

相关文章:

c - tun 程序未创 build 备节点

c - C 中的标量乘以 vector 乘法

android - 如何使用 Scarlet 在 Android 上通过 WebSocket 进行连接?

c# - C#服务接收器程序崩溃

Linux:如何更改进程可以打开的最大文件数?

delphi - 使用Delphi 2010编写TCP IP消息系统(客户端/服务器)的建议

c - TOR 上的基本 C Windows TCP 套接字,recv() 挂起?

c - TCL_LINK_STRING 导致段错误(核心已转储)

混淆将字符串作为参数传递给 C 函数

go - Golang 中带有 TCP/IP 套接字的 channel 无法正常工作