简体   繁体   English

UDP 协议的套接字创建协议号

[英]Protocol number for socket creation for UDP protocol

I am in the process of making a socket for a client in C.我正在为 C 中的客户端制作套接字。 I would like to use the UDP protocol, but I can't seem to find it in the man pages.我想使用 UDP 协议,但我似乎无法在手册页中找到它。

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

The man 2 socket manual page only describes the general usage of the socket() system call. man 2 socket手册页仅描述了socket()系统调用的一般用法。 It does not go into much detail about each possible protocol.它没有 go 详细介绍每个可能的协议。 For that, there are several different manual pages under section 7 .为此,第7节下有几个不同的手册页。

For the IP protocol, you can check man 7 ip , which states:对于 IP 协议,您可以查看man 7 ip ,其中指出:

An IP socket is created using socket(2):

    socket(AF_INET, socket_type, protocol);

Valid socket types include SOCK_STREAM to open a stream socket,
SOCK_DGRAM to open a datagram socket, and SOCK_RAW to open a
raw(7) socket to access the IP protocol directly.

protocol is the IP protocol in the IP header to be received or
sent.  Valid values for protocol include:

   - 0 and IPPROTO_TCP for tcp(7) stream sockets;
   - 0 and IPPROTO_UDP for udp(7) datagram sockets;
   - IPPROTO_SCTP for sctp(7) stream sockets; and
   - IPPROTO_UDPLITE for udplite(7) datagram sockets.

So for an UDP socket you want:因此,对于您想要的 UDP 插座:

int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

Or equivalently:或等效地:

int fd = socket(AF_INET, SOCK_DGRAM, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM