Socket function details (with a new understanding) __ function

Source: Internet
Author: User
Tags unix domain socket

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.