FastSocket learning notes ~ Develop your own transmission protocol, fastsocket learning notes

Source: Internet
Author: User

FastSocket learning notes ~ Develop your own transmission protocol, fastsocket learning notes

For TCP or UDP, they are used as the transport layer protocol and have their own standards or formats. Before we look at the TCP format, let's take a look at the basic knowledge of the computer, in bytes, it is a small unit in the computer world, which we can handle. For example, a UTF-8 English letter represents a byte ), A Chinese character or a unicode English letter represents two bytes, or it occupies two bytes of storage space in the computer. In the programming world, Int32, Int16, and Int64 are type suffixes that indicate the number of digits it occupies (bit, 8bit = 1 byte ), that is, an int32 number occupies 4 bytes of storage space. In fact, this is what we have seen and memorized in previous teaching books, but it may have never been used,Cannot be usedIt is not a good thing. Let's take a look at it.

TCP protocol format

Meaning of TCP Fields

Source Port & Destination Port
If we compare the IP address to the address, the Port can be said to be the door. Imagine a building with a front door, a rear door, a side door, a spam door, a dead door, and so on. ...... A single IP address also has multiple ports with various functions, and each port is listened to by different services, just like a gatekeeper. The following are some of the frequently-used ports and their corresponding services that interest you. You can find them in the/etc/services file of Linux.
Ftp-data 20/tcp
Ftp 21/tcp
Telnet 23/tcp
Smtp 25/tcp mail
Www 80/tcp http # WorldWideWeb HTTP
Www 80/udp # HyperText Transfer Protocol
Pop-3 110/tcp # POP version 3
Pop-3 110/udp
In fact, the port number can be used with any service you like, but in order to avoid "finding the wrong door" (unless you intentionally want to hide it) people have fixed the port numbers of some commonly used services (Well known services. However, in the TCP data transmission process, more than one package may be processed at the same time, and multiple ports will be created to avoid burst. When two hosts transmit data, both the source port and destination port must be known to TCP.
Sequence Number
Sending sequence number. When data is transferred from one host to another, the sender creates an initial number for the packet and then increments accordingly according to the number of sent bits. the incremental value is used as the serial number. In this way, the receiver can check whether the data is received completely based on the serial number.
Acknowledgement Number
Response sequence number. When the receiving end receives the TCP packet, it passes the verification and then generates a response serial number according to the sending serial number to send a response packet to the sending end, so that the receiving end will know that the packet has been successfully received.
However, if the TTL value of a packet expires due to network conditions or other reasons, the receiving end will resend the packet that is thought to have been lost before receiving the response number. However, if a response is received only after the packet is resold, the receiving end will judge whether the packet is resold Based on the serial number. If yes, it is easy to discard it without any processing.
Data Offset
This is used to record the Fixed Length of the header, which is similar to the IHL of IP packets. If options is not set, the length is 20 bytes, Which is 0x14 in hexadecimal notation.
Reserved
This is the reserved range that has not yet been used.
Contral Flag
Control tag. There are six of them: Urgent data
If URG is 1, it indicates that this is a packet carrying urgent information.
Acknowledgment field significant
If ACK is 1, it indicates that this packet belongs to the packet to be responded. Generally, it is 1.
Push function
If PSH is 1, the data carried by this packet will be directly uploaded to the upper-layer application without the need for TCP processing.
Reset
If RST is 1, retransmission is required. Indicates that the packets need to be reset and then re-transmitted.
Synchronize sequence number
If SYN is 1, both parties are required to communicate synchronously.
No more data for sender (Finish)
If the FIN value is 1, the transfer is over, and then both parties send an end response to formally terminate a TCP transfer process.
Window
We all know what MS Windows is, but the Window here is not the "Window" of the operating system. It is generally called "Sliding Window )". Why should we use Windows?
As you can see, the TCP packet uses the SQN and ACK serial numbers to ensure the correctness of the transfer, however, it would be too slow and unacceptable if each packet had to wait for the response from the previous packet to be sent. In this way, we can use the Sliding Window to divide a range at both ends of the transfer and specify the maximum number of packets that can be sent at a time.
After TCP transmission is established, both ends will restore the window setting value to the initial value. For example, three packets are transferred each time. Then, the sender sends three packets at a time, and then the window moves the three packets to fill the gap in the sent packets. If the receiving end can process the received three packets at a time, the window value of the sending end is 3. However, if the receiving end is too busy or other factors affect the processing of only two packets, then there will be one packet in the window, and the sending end will be told that the window value is 2. At this time, the sender sends only two packets, and the window moves the two packets to fill the gap in sending. You understand why the window will slide.
In fact, the Window value is calculated based on the number of bytes.
Chechsum
When the data is to be sent out, the sender calculates the data size of the packet, and then obtains the test value. The packet is sent together. When the receiving end receives the packet, the data size is calculated to check whether or not the data size is verified. value consistency if the result is not commensurate, the packet is deemed as incomplete and the other party is required to resend the packet.
Urgent Pointer
Remember when we talked about Control Flag, we mentioned a URG mark. If URG is set to one, it will indicate the location of the emergency information. However, this situation is rare. For example, when the data traffic exceeds the bandwidth, the system requires the network host to suspend data sending. All Hosts must take priority in receiving such information.
Option
This option is rarely used. When programs that require synchronous operations such as Telnet need to process the Interactive Mode of the terminal, the option will be used to specify the size of the data packet, Because telnet uses few data packets but requires immediate response.
The length of Option is 0 or an integer multiple of 32 bits. If it is not enough, it is filled with full.

Implement your own protocol format

[BodyLength int32] [id int32] [type int16] [name char (20)] [body byte []

Next we will test a protocol and assign values to it to simulate the sending and receiving processes of the client and server.

String name = "zzl"; var message = SerializeHelper. serializeToBinary (new UserDTO {ID = 1, Name = "repositoryUncle"}); int bodyLength = 4 + 4 + 2 + name. length + message. length; byte [] buffer = new byte [bodyLength]; byte [] body = new byte [bodyLength]; # region (sender) writes the byte stream // write message length Buffer. blockCopy (BitConverter. getBytes (bodyLength), 0, buffer, 0, 4); // write id. buffer. blockCopy (BitConverter. getBytes (101), 0, buffer, 4, 4); // write response type. buffer. blockCopy (BitConverter. getBytes (short) 4), 0, buffer, 8, 2); // write response name. buffer. blockCopy (Encoding. ASCII. getBytes (name), 0, buffer, 10, name. length); // write response message. buffer. blockCopy (message, 0, buffer, 10 + name. length, message. length); # endregion # region (receiver) throttling var messageLength = BitConverter. toInt32 (buffer, 0); var id = BitConverter. toInt32 (buffer, 4); var type = BitConverter. toInt16 (buffer, 8); var nameVal = Encoding. ASCII. getString (buffer, 10, name. length); var dataLength = messageLength-10-name. length; Buffer. blockCopy (buffer, 10 + name. length, body, 0, dataLength); var obj = (UserDTO) SerializeHelper. deserializeFromBinary (body); # endregion

During the test, we can set a breakpoint to check whether the values of messageLength, id, type, nameVal, obj, and other elements are correctly parsed.

The last thing to remember is that the Protocol is not difficult and the byte stream is not mysterious. The key lies inPersistence, continuous exploration and researchWith these points, you can handle the trouble!

 

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.