Let's take a look at the prototype of the socket function:
Socket PASCAL FAR socket (int af, int type, int protocol);
The typical invocation method is:
unsigned int socksrv = socket (af_inet, sock_stream, 0);
The 1.AF is the acronym for Address family, which is actually a field domain, and this AF is primarily used to differentiate between creating IPv4 sockets or IPv6 sockets. Of course, by the way, if it's in Unix, the first argument can be Af_unix, which means that the socket is neither a IPv4 socket nor a IPv6 socket, but a UNIX domain socket that is not a network form. Can be used for interprocess communication in a non-network form. In many embedded systems, the communication between processes is done through the UNIX domain sockets, which are not in network form. However, we have to understand that the client and server side of the UNIX domain socket must be on the same machine and more efficient than the network sockets when communicating with the client and server.
2. I always thought that this type value determines whether it is a TCP socket or a UDP socket, but it is not. The type value determines whether the stream socket or datagram socket or other. Note that stream sockets are not necessarily TCP, and datagram sockets are not necessarily UDP.
3. What does it mean when protocol this value is usually 0, 0? means that if type is a stream socket and protocol is 0, then it is the default stream socket---TCP socket. Similarly, if type is a datagram socket and protocol is 0, then the default datagram socket---UDP socket.
The return value is actually an unsigned shaping that identifies and indexes the socket. The return value can be used to determine whether a socket was created successfully.