sockets are a communication mechanism (a contract between two parties of communication), whereby processes between different hosts can communicate. we can use the correlation function in the socket to complete the communication process .
The characteristics of a socket are determined by three properties, which are: domain, type, and protocol (protocol).
Sockets for Fields
The domain specifies the network media used in socket communication. The most common socket domain is af_inet, which refers to Internet networks, which are used by many Linux LANs and, of course, by the Internet itself.
Socket type
stream Sockets (Sock_stream):
Stream sockets are used to provide connection-oriented, reliable data transfer services. The service will ensure that the data can be error-free, non-repeatable, and sequentially received. A streaming socket is capable of achieving reliable data services because it uses the transmission Control protocol, the TCP (the Protocol) transmission.
datagram Sockets (SOCK_DGRAM):
Datagram sockets provide a non-connected service. The service does not guarantee the reliability of data transmission, it is possible to lose or duplicate data during transmission, and can not guarantee the sequential receipt of data. Datagram sockets use the UDP (User Datagram Protocol) protocol for data transfer. Due to the fact that datagram sockets cannot guarantee the reliability of data transmission, it is necessary to do the corresponding processing in the program in case of possible loss.
Original socket (SOCK_RAW):
The difference between the original socket and the standard socket (the standard socket refers to the flow socket and datagram sockets described earlier) is that the original socket can read and write IP packets that are not processed by the kernel, and the stream sockets can read only the data of the TCP protocol, and the datagram socket can only read the data of the UDP protocol. Therefore, if you want to access other protocols to send data, you must use the original socket.
Socket Protocol (Protocol category)
As long as the underlying transport mechanism allows more than one protocol to provide the required socket type, we can select a specific protocol for the socket. The default is usually used (that is, the last parameter to fill "0").
Creating sockets
The socket system call creates a socket and returns a descriptor that can be used to access the socket.
header files required : #include <sys/socket.h>
int socket (int family,int type,int protocol);
features :
Create a Socket socket (descriptor) for network traffic
Parameters :
Family: protocol family (Af_unix, Af_inet, Af_inet6, pf_packet, etc.)
The most common socket fields are Af_unix and af_inet, which are used for local sockets implemented through UNIX and Linux file systems, which are used for UNIX network sockets. Af_inet sockets can be used for programs that communicate through a TCP/IP network, including the Internet. The Winsock interface of the Microsoft Windows system also provides access to this socket domain.
Type: Socket type (SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, etc.)
protocol: The Protocol category (0, Ipproto_tcp, IPPROTO_UDP, and so on), set to 0, means the default protocol is used.
return value :
SUCCESS: Socket
Failed (<0)
To create a UDP socket sample:
int SOCKFD;SOCKFD = socket (af_inet, SOCK_DGRAM, 0), if (SOCKFD < 0) {perror ("socket"); exit (-1);}
Socket Address
Each socket (endpoint) has its own address format, and for Af_unix sockets, its address is described by the struct sockaddr_un, which is defined in the header file Sys/un.h, as follows:
struct Sockaddr_un {sa_family_t sun_family;//socket field char sun_path[];//name};
In the af_inet domain, the socket address structure is specified by sockaddr_in, which is defined in the header file Netinet/in.h:
struct SOCKADDR_IN {short int sin_family;//socket field unsigned short int sin_port;//port struct in_addr sin_addr;}
The IP address structure in_addr is defined as follows:
struct IN_ADDR {unsigned long int s_addr;};
Linux Network Programming--Introduction to Sockets