socket () function usage Detailed: create a socket

Whether it is Windows or  Linux , use the  socket () function to create a socket. socket () parameters at two platforms is the same, except that the return value.

In the " What socket is " one we talked about the difference between Windows and Linux in the treatment of the socket.

Everything in Linux files, each file has a file descriptor type integer; socket is a file, there are file descriptors. Using socket () function to create a socket later, the return value is an int file descriptor.

Windows distinguishes between socket and ordinary files, which the socket connection is treated as a network, call socket () later, the return value is the SOCKET type that represents a socket.

socket under Linux () function

Use <sys / socket.h> header file at the Linux socket () function to create a socket, prototype:

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

1) af address family (Address Family), that is, IP address type, commonly used AF_INET and AF_INET6. AF is the "Address Family" shorthand, INET is "Inetnet" shorthand. AF_INET address indicates IPv4, e.g. 127.0.0.1; AF_INET6 an IPv6 address, such as 1030 :: C9B4: FF12: 48AA: 1A2B.

We need to remember 127.0.0.1that it is a special IP address, local address representation, we will be frequently used later in the tutorial.

You can also use the prefix PF, PF is "Protocol Family" is short for it and AF is the same. For example, PF_INET equivalent to AF_INET, PF_INET6 equivalent to AF_INET6.

2) type of data transmission / socket type, commonly used SOCK_STREAM (stream format socket / socket connection oriented) and SOCK_DGRAM (datagram socket / socket connection No), we have in " socket What types " section was introduced.

3) protocol represents transmission protocol, commonly used IPPROTO_TCP and IPPTOTO_UDP, respectively TCP transport protocol and the UDP transport protocol.

With the type of address and data transmission, not enough to decide which protocol uses it? Why do we need a third argument it?

As you think, and has af type two parameters under normal circumstances can create a socket, the operating system will automatically deduce the protocol type, unless encountered such a situation: There are two different protocols support the same address type and data transfer type. If we do not specify which protocol to use, the operating system is no way automatic deduction.

This tutorial uses an IPv4 address, a value of the parameter af PF_INET. If you use SOCK_STREAM transmission data, to meet these two conditions only protocol TCP, and therefore it can be to call socket () function:

you tcp_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP) // IPPROTO_TCP 表示 TCP 协议

This socket is called TCP socket.

If you are using SOCK_DGRAM transmission, then meet these two conditions only protocol UDP, so it is possible to call socket () function:

int udp_socket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); // IPPROTO_UDP 表示 UDP 协议

This socket is called UDP socket.

In both cases above, only one protocol to meet the conditions, the value of protocol can be set to 0, the system will automatically deduce what protocol should be used, as follows:

int tcp_socket = socket (AF_INET, SOCK_STREAM, 0); // create a TCP socket
int udp_socket = socket (AF_INET, SOCK_DGRAM, 0); // create a UDP socket

Later tutorial adopt this simplified wording.

Create a socket under Windows

Under Windows also uses socket () function to create a socket, prototype is:

SOCKET socket(int af, int type, int protocol);

In addition to different types of return values, others are the same. Windows Sockets not to treat as an ordinary file, but returns SOCKET type of handle. Consider the following example:

SOCKET sock = socket (AF_INET, SOCK_STREAM, 0); // create a TCP socket

Guess you like

Origin blog.csdn.net/Qsir/article/details/93739021