简体   繁体   中英

What is the difference between stream parameter and protocol parameter in socket function?

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

As per i understood,

int domain;

This parameter is used for passing the address family or protocol family. I belive that there is no major difference betweent the address family and protocol family. We are using this for specifying the address type (IPV4 or IPv6).

int type;

This parameter is used for connection type such as UDP,TCP in network layer.

int protocol;

This parameter is used for the specifying the protocol such as TCP,UDP.

and my question is

what is the difference between the 2nd and 3rd parameter? If we use the TCP protocol we will use the SOCK_STREAM , and if we use the UDP protocol we will use the SOCK_DGRAM in 2nd parameter. So why there is a need for two parameters in this case? Why not we use a single parameter instead of two?

As per the man page the protocol is the sub type of a socket, for most of types there is no sub type but some types may additional subtypes.

For example you can specify protocol number if you have type raw socket.

int fd = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);

-MS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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