[Computer network] article a quick overview of Socket

table of Contents

A, socket Introduction

Two, socket communication flow

Three, socket and http

The difference between 3.1 socket and http

3.2 socket and http application scenarios which


A, S OCKET Profile

Port (port) is accompanied by the birth of the concept of the transport layer. It may be an IP communication network layer is distributed to each communication channel. UDP protocol and TCP protocol although very different in the work, but they have established a communication from one port to another port.

As we enter the transport layer, we can call the operating system API, to build a socket (socket) .

 

Socket is a programming interface to the operating system, it is used to represent a network communications. Application to Call module enables network protocol processing system kernel by the socket, and these kernel modules will be responsible for the implementation of specific network protocols. In this way, we can let the kernel to receiving details of the network protocol, and we only need to provide content to be transmitted on it, the kernel will help us control the format and further to the bottom package.

 

Socket can be viewed as a communication connection endpoint in the two programs, a program piece of information into the Socket, the Socket sends this information to the Socket another, so that this information can be conveyed to other programs. Such as servers and clients interact through socket. The server needs to bind to a port number on the machine, the client needs to declare its own address which is connected to which port, so that the server and client can be connected.

 

Generate a socket, there are three main parameters: the purpose of communication IP address , using the transport layer protocol (TCP or UDP) and port number used . Socket is intended to "socket." By combining these three parameters, with a "socket" the Socket binding, the application layer and the transport layer can be a socket interface, from different applications to distinguish a communication process or the network connection, data transmission is concurrent services.

 

Therefore, in practice, we do not need to know the specifics of how to form a UDP packet, but only need to provide information (IP address, port number, information to be transmitted, etc.) to create a socket, then the socket will help the operating system kernel before transmission based on information we provide constitute a qualified UDP packet (packets and frames as well as a lower layer).

 

Two, Socket communication flow

A program on Host A to a piece of information into the Socket, the content is accessed Socket Host A's network management software, and this information is sent to Host B via Host A's network interface card, a network interface card Host B receives to this information, transmitted to Host B network management software, network management software, this information will save Socket Host B, and then the program B can read this information in the Socket.

To communicate via the Internet, at least one pair of sockets , a socket running on the client side, called the ClientSocket , another socket running on the server, called serverSocket .     

The connection start manner and the target local socket to be connected, the connection process between the socket can be divided into three steps: server listening , client requests , connection confirmation .     

  • Server monitoring: server-side Socket not locate a specific client Socket, but in a state of waiting for a connection, real-time monitoring of network status.
  • The client requests: refers to the connection request made by the client socket, the goal is to connect to the server socket. To this end, the client socket must describe the server socket to connect to it, pointing out the address and port number of the server-side socket, then made a connection request to the server socket on.
  • Connection confirmation: means that when the server socket to listen to or receives a connection request from a client socket, it responds to the request of the client socket to create a new thread, the server socket describe to the client, once the client to confirm this description, a good connection is established. And the server socket continues in listening state, continues to receive requests from other clients connected to the socket.

socket communication step:

  1. Socket server to create the address type (ipv4, ipv6), socket type, protocol type (UDP / TCP)
  2. Server socket binding ip address and port number
  3. Server socket listening port number request, ready to send to the receiving client connection, this time the socket server has not been opened
  4. The client creates socket
  5. Open client socket, ip according to the server using the address and port number connexct () method attempts to connect to the server socket
  6. Socket server receives a client request socket, passive open to begin receiving client requests until the client return connection information. This time server socket into the blocked state, i.e. so-called blocking accept () method has been returned to the client after the connection information returned, the client starts receiving the next connection request
  7. Client connection is successful, sending information to the server connection status
  8. Server accept () method returns a successful connection
  9. The client send () method writes information to the socket
  10. Server recv () method for reading information
  11. Client closes
  12. Server-side closed

We can see from the above process, socket is on the TCP / IP protocol encapsulation and applications. In the TCP / IP protocol, TCP three-way handshake protocol to establish a reliable connection.

  1. The first handshake: a client attempts to connect to the server, the server transmits the packet syn (SEQ ID NO synchronized Synchronize Sequence Numbers), syn = j, the client waits for the server to enter SYN_SEND acknowledgment.
  2. Second handshake: a client server receives syn packets and acknowledgment (ack = j + 1), while the client sends a SYN packet (syn = k), i.e., SYN + ACK packet, then the server enters a state SYN_RECV.
  3. Third handshake: the client receives the SYN + ACK packet to the server, the server sends an acknowledgment packet ACK (ack = k + 1), this packet is sent, the client and server into the ESTABLISHED state, complete the three-way handshake.

Socket server and client socket connection is actually established part of the "three-way handshake."

 

Three, socket and http

3.1 socket and http difference

socket connection:

socket does not belong to the scope of the protocol, but a call interface (the API) , is a TCP / IP protocol encapsulation. Performing physical connection between the server and the client, and data transmission. Socket network protocol in the transport layer , there are TCP / UDP protocol two (of course there are TCP / IP protocol suite other protocols).

socket connection is a long connection , in theory, the client and the server once a connection is established will not take the initiative to cut off; but due to various environmental factors may cause disconnection, such as: server-side or client host is down, the network long time between data transmission failure, or both, the network firewall may disconnect the connection to release network resources. So when a socket connection is not transmitting data, the need to maintain the connection to send a heartbeat message.

Data transmission may be custom socket, as the byte level, a small amount of data can be encrypted, data security, information for real-time interaction between the Client / Server.

 

http connections:

HTTP is based on TCP / IP protocol is an application layer protocol , defined in the specification of the content data transmission.

HTTP is based on a request - response and is in the form of a short connector , i.e., the client sends a request to the server, the server will respond to a connection that is broken.

HTTP is a stateless protocol, for which no state characteristics, in practical applications they need to have the form of the state, it tends to resolve this issue by session / cookie technology.

HTTP slow transmission speed, large data packets, data transmission security is poor, such as real-time interaction, server performance pressure.

 

3.2 socket and http application scenarios which

comparing an instant socket for general communications and real-time is high, such as push, chat, maintaining long heartbeat connections;

http generally used for real-time requirements are not so high, such as information feedback, upload pictures, access news information.

 

The reason for using http without the use of socket in some cases is the socket Once connected, it is always connected, it will cause blocking IO, with this relative, as well as non-blocking IO.

  • Blocking IO refers client requests the server side, server-side processing is completed, then the return value to the client. In this case the client has been in the state blocked. When a client too, concurrent requests, the server-side processing, however, the server would have been stuck there waiting, affecting the user experience. Therefore, the proposed non-blocking IO.
  • Non-blocking IO means to return immediately after the client requests the server, the server is responsible for some listeners accept the request, the client receives the requests sent that tells the client has accepted well, and other treatment after the completion of the data returned client-side processing data during the service the client will not have to wait for obstruction.

Blocking and non-blocking concern is the state of the program while waiting for the result of the call (the message, the return value) of.

Blocking call refers to the results before the call returns, the current thread is suspended. Only the calling thread does not return until after the result.

Before non-blocking call refers not get the results immediately, the call does not block the current thread.


 Reference: https://www.linuxidc.com/Linux/2018-09/154366.htm
                   https://blog.csdn.net/weixin_36691991/article/details/104963784?fps=1&locationNum=2

Published 54 original articles · won praise 47 · views 10000 +

Guess you like

Origin blog.csdn.net/cy973071263/article/details/105128839