Encapsulation of socket data

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

What is a socket?

The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a 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 of the host, so we can use the IP address + protocol + port number uniquely identify a process in the network, can uniquely identify the process in the network, they can use the socket to communicate.

Establish a network communication connection with at least a pair of port numbers (sockets). Socket is the nature of the programming Interface (API), TCP/IP encapsulation, TCP/IP also to provide the interface for programmers to do network development, this is the socket programming interface; HTTP is a sedan that provides a specific form of encapsulation or display data; the socket is the engine, Provides the ability to communicate on the network.

The difference between a socket and HTTP

In the network Seven Layer architecture, HTTP belongs to the Application layer protocol, the diagram


The socket is an abstraction layer between the application layer and the transport layer, which is not originally present in the seven-tier architecture: Figure



How does the socket communication process proceed?

The communication flow of the socket:



Process interpretation:

1. Server-Side Create socket

2. Server-side binding socket and port number

3. Server-side monitoring of the port number

4. The server side initiates the Accept () receives the connection request from the client side, at this time has the connection enters the subsequent execution, does not have the link to block in this

5. Client side Create socket

6. Client Side server-side (three-time TCP handshake) based on server side IP and port connection

7. If the 6th step connection succeeds, a connection will be received on the server side if the connection is called Conn

8. Client side sends data to server side

9. Server side reads the data sent from the client side from the Conn

10. Can be actively disconnected at either end

Problems with transmitting data in the socket

Sticky pack: When the server side uses a buff to receive data, it finds that the buff has other data in addition to a complete package.

Half Pack: The server side did not receive a complete package and only received a portion.

Packet and unpacking of sockets

In the actual development will often encapsulate their own data, generally divided into head and Body,head in addition to encapsulating additional information, but also encapsulate the length of the body inside. This will result in the following data:

head{//Header information (6 bytes total)

One byte//first flag (1 bytes)

Two byte//second flag (1 bytes)

length int32//size of data stored body (4 bytes)

}

body{//message body (total occupancy?) + 4 bytes)

name []byte//name (occupied? bytes)

Age Int32//Ages (occupies 4 bytes)

}

data{

Head

Body

}

The server side wants to dock the received data []byte to prevent sticky and semi-packet occurrences.

Note: Data data structure should be consistent on both server side and client side

The encapsulation process:

Convert a packaged data data into []byte, then send.

Unpacking process:

1. The loop reads the data from the Conn, every time the loop is judged whether the received data is >=6, if so, then the head is received, if not, continue to perform the next cycle, know to meet the conditions

2. After the head is received, the first 6 bytes of data are parsed into the head of data, minus 6 for the length received from the conn (get the length of the body data received), to determine whether the result is >=head length, if no, the loop continues to receive data If, the data is parsed into the body of the length, and the data is then re-parsed and stored

This solves the problem of sticking and half-pack.

Golang Code Implementation

Jane's code is really a pain in the egg, forget it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.