1. concept:
A socket is a network IPC interface that allows a process to communicate with other processes. through this interface, other processes run transparently on the same computer or on different computers. that is to say, it supports inter-process communication within the computer and inter-process communication between the computer.
The socket interface can adopt many different network protocols, but we mainly discuss the network communication standard: TCP/IP protocol stack.
2. Socket descriptor:
In Linux, the use of socket descriptors is similar to the use of file descriptors in Linux systems. In essence, it is also implemented using file descriptors to process file descriptor functions (such as read and write) you can also process the socket descriptor.
Socket is the abstraction of the Communication endpoint. To create a socket, you can call the socket function:
- Header file: <sys/socket. h>
- Prototype: int socket (INT domain, int type, int Protocol );
- Returned value: Socket descriptor is returned successfully.-1 is returned if an error occurs.
- Parameters:
- Domain: describes the characteristics of communication, expressed in address family. Common address families include:
- Af_inet: IPv4 internet
- Af_inet6: IPv6 Internet
- Af_unix/af_local: Local UNIX
- Af_unspec: Unspecified
- Type: Describes the socket type. In implementation, You can freely add support for other types. Common socket types include:
- Sock_dgram: fixed-length, connectionless and unreliable packet transmission.
- Sock_raw: IP datagram interface.
- Sock_seqpacket: Fixed Length, ordered, and reliable connection-oriented message transmission.
- Sock_stream: ordered, reliable, and bidirectional connection-oriented byte stream.
- Protocol: usually 0, indicating that the default protocol is selected based on the given domain and type.
- Af_inet + sock_stream the default protocol is TCP (Transmission Control Protocol)
- Af_inet + sock_dgram the default protocol is UDP (User Datagram Protocol)
The call of socket is similar to the call of open. When this descriptor is no longer needed, close is called to close it.
3. UDP and TCP:
① UDP: a connectionless service is provided. A datagram is a self-contained packet. Sending data is like sending a mail to someone:
- Each letter contains the recipient's address;
- A large number of mails can be mailed, but the delivery order cannot be guaranteed;
- Emails may be lost on the road;
- Each letter may be delivered to different recipients.
In comparison:
② TCP: provides a connected service. Protocol Communication using this service is like making a call:
- A connection needs to be established by phone;
- Each connection is an end-to-end communication channel;
- The session does not contain address information.
4. Differences between socket types (type parameters:
- Sock_dgram does not need to be connected during communication, but only needs to send a packet to the socket of the specified address.
- Sock_stream requires that a connection be established between the local socket and the remote socket with which the communication is established before data is exchanged.
- Sock_seqpacket is similar to sock_stream, but the socket obtains the packet-based service rather than the byte stream service.
- Sock_raw provides a datagram interface for users to directly access the following network layer (IP layer ). when this interface is used, the application is responsible for constructing its own protocol header because the transmission protocol (TCP/UDP) is bypassed. when creating an original socket, you must have the superuser privilege to prevent malicious programs from bypassing the built-in security mechanism to create packets.
5. bidirectional communication:
Socket communication is bidirectional. You can use the shutdown function to disable input/output on the socket:
- Header file: <sys/socket. h>
- Prototype: int Shutdown (INT sockfd, int how );
- Return Value: 0 is returned for success, and-1 is returned for error.
- Parameters:
- Sockfd: Socket descriptor created through the socket function.
- How:
- Shut_rd: Close the read end and cannot read data from the socket.
- Shut_wr: The write end is disabled and data cannot be sent from the socket.
- Shut_rdwr: data cannot be read and sent at the same time.
- Note: There is a difference between shutdown and close, and the function is not equal.
- First, close releases the endpoint only when the last reference is closed. For example, copy a socket using DUP, the socket will not be released until the last file descriptor that references it is closed. shutdown allows a socket to be inactive, regardless of its reference count.
- Second, disable one direction in two-way communication.