Tcp / ip
To understand the socket first you have to familiarize yourself with the TCP/IP protocol family, TCP/IP (transmission Control protocol/internet Protocol) is the transmission protocol /Network protocol, Defines the standards for how hosts connect to the Internet and how data is transferred between them .
In the literal sense TCP/IP is the general term for TCP and IPs, but in fact the TCP/IP protocol refers to the entire TCP/IP protocol family. Unlike the ISO model's seven tiers, the TCP/IP protocol Reference Model classifies all TCP/IP series protocols into four layers of abstraction
Application layer: Tftp,http,snmp,ftp,smtp,dns,telnet, etc.
Transport Layer: TCP,UDP
Network layer: IP,ICMP,OSPF,EIGRP,IGMP
Data Link layer: SLIP,CSLIP,PPP,MTU
Each abstraction layer is built on the service provided on the lower tier and serves the upper tier, which looks like this
Estimated interested in opening this article of the students have a certain understanding of this, plus I also smattering, so do not explain in detail, interested students can search the internet for information
Wikipedia
Baidu Encyclopedia
In the TCP/IP protocol, two Internet hosts are connected through two routers and corresponding layers. Applications on each host perform read operations with each other through a number of data channels
Socket
We know that two processes if need to communicate the most basic premise can be able to uniquely identify a process, in the local process communication we can use PID to uniquely identify a process, but the PID is only local only, the network of two process PID collision probability is very large, this time we need to separate its path, We know that the IP address can uniquely identify the host, and the TCP layer protocol and port number can uniquely identify a process for the host, so we can use the IP address + protocol + port number to uniquely identify a process in the network.
The ability to uniquely identify the processes in the network, they can use the socket to communicate, what is the socket it? We often translate the socket into sockets, the socket is an abstraction layer between the application layer and the transport layer, which abstracts the complex operations of the TCP/IP layer into a few simple interfaces to provide a layer to invoke the implemented process to communicate in the network.
Socket originated from UNIX, in Unix everything is file philosophy, socket is an "open-read/write-off" mode implementation, the server and the client maintain a "file", after establishing a connection open, you can write to their own files for the other side to read or read the contents of the other side, Closes the file at the end of the communication.
Socket Communication Process
The socket is an implementation of "open-read/write-off" mode, which uses the TCP protocol communication socket as an example, and its interaction flow is probably
Server creates sockets based on address type (IPV4,IPV6), socket type, protocol
Server binds IP address and port number to socket
Server socket Listener Port number request, ready to receive the client's connection, this time the server socket is not opened
Client creates socket
Client opens socket, attempts to connect to server socket based on server IP address and port number
The server socket receives a client socket request, passively opens, and begins receiving client requests until the client returns the connection information. When the socket enters the blocking state, the so-called blocking is the Accept () method until the client returns the connection information, and begins receiving the next client understanding request
Client connection successful, sending connection status information to server
Server Accept method returned, connection successful
The client writes information to the socket
Server Read information
Client shutdown
Server-side shutdown
Three-time handshake
In the TCP/IP protocol, the TCP protocol establishes a reliable connection through a three-time handshake
First handshake: The client tries to connect to the server, sends a SYN packet to the server (synchronization sequence number Synchronize Sequence Numbers), SYN=J, the client enters Syn_send state waits for the server to confirm
Second handshake: The server receives the client SYN packet and confirms (ACK=J+1), and sends a SYN packet (SYN=K) to the client, which is the Syn+ack packet, when the server enters the SYN_RECV state
Third handshake: The third handshake: the client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and server enter the established state, complete three handshake
In a closer look, the part of the server socket that connects to the client socket is actually the three-time handshake.
Socket Programming API
As mentioned above, the socket is the implementation of "open-read/write-off" mode, a simple look at what APIs the socket provides for application use, or the TCP protocol as an example to see the socket API under UNIX, other languages are similar (PHP even names are almost the same), Here I will briefly explain the method function and parameters, the specific use of interested students can see the blog reference links or Internet search
int socket (int protocol);
Assigns a descriptor for a socket and the resources it uses according to the specified address family, data type, and protocol.
Domain: Protocol family, commonly used are af_inet, Af_inet6, af_local, Af_route where af_inet represents the use of IPv4 address
Type:socket types, commonly used socket types are, Sock_stream, Sock_dgram, Sock_raw, Sock_packet, Sock_seqpacket, etc.
Protocol: protocol. Commonly used protocols are, IPPROTO_TCP, IPPTOTO_UDP, IPPROTO_SCTP, IPPROTO_TIPC, etc.
int bind (struct sockaddr *addr, socklen_t addrlen);
Assign a specific address in the address family to the socket
Sockfd:socket description Word, which is the socket reference
Addr: The protocol address to bind to the SOCKFD
Addrlen: Length of Address
Usually when the server is started to bind a well-known address (such as IP address + port number) to provide services, the client can be used to connect the server, and the client does not specify, there is a system automatically assigned a port number and its own IP address combination. This is why the server usually calls bind () before listen, and the client does not invoke it, but instead generates one randomly from the system at Connect ().
int listen (int backlog);
Monitor socket
SOCKFD: The socket descriptor to listen on
Backlog: The maximum number of connections that the corresponding socket can queue
int Connect (struct sockaddr *addr, socklen_t addrlen);
Connect a socket
SOCKFD: Socket description Word for Client
Addr: The server's socket address
Length of the Addrlen:socket address
int accept (struct sockaddr *addr, socklen_t *addrlen);
After the TCP server has heard the client request, call the Accept () function to take the receive request
SOCKFD: Socket descriptor for server
Addr: Client's socket address
Length of the Addrlen:socket address
ssize_t Read (void *buf, size_t count);
Read Socket contents
Fd:socket Description Word
BUF: Buffer
Count: Buffer length
ssize_t Write (void *buf, size_t count);
Writing to the socket is actually sending the content
Fd:socket Description Word
BUF: Buffer
Count: Buffer length
int close (int fd);
The socket is marked as close so that the reference count of the corresponding socket descriptor-1, when the reference count is 0, triggers the TCP client to send a terminating connection request to the server.
Excerpt from: http://www.cnblogs.com/dolphinX/p/3460545.html
Simple understanding of sockets