Socket network programming learning notes (4): TCP Message boundary Processing

Source: Internet
Author: User
In the previous articles, I talked about Socket socket and the use of socket assistant classes to communicate between the server and the client. In the middle, I did not process the sent information. This article describes the information boundary issues during TCP communication.

When receiving information through a socket or its helper class, it reads all the links from the cache at one time. As long as the cache you set is large enough, it can read so much information, this will lead to such a situation. If the server sends messages to the client consecutively, for example, if I continuously send strings "Message 1", "Message 2", "Message 3", "Message 4", and "Message 5 ", I expected that the client could also receive these five complete strings. If we use the method described in the first two articles to test the strings on the same machine, it would be normal, there will be fewer exceptions in network information transmission on the same machine. However, if you deploy the client and server on different machines, some unexpected exceptions may occur. You will find that all received characters are broken, and a string such as "3 Message 4" appears. In this way, we cannot restore the information sent by the server. This is the message boundary problem. There are many methods to solve this problem. Here are some of them:

1. fixed-size messages

This is the simplest but most expensive solution to TCP messages. It is to design a protocol that always transmits messages with a fixed length. By setting all messages to a fixed size, when a complete message is received from a remote device, TCP receivingProgramYou can understand the sending status. This means that short messages must be extended, resulting in a waste of network bandwidth resources.

2. Use Message Size Information

This scheme allows the use of variable-length messages. The only drawback is that the remote settings of the acceptor must understand the exact length of each variable-length message. The specific method is to send the message length together when sending the message. When receiving the message, the client can know the message length and then read the message.

3. Use message tag

This scheme uses a predefined character (or multiple characters) to specify the end of a message. In this way, different messages are separated. However, this method must be used to check each character received in order to identify it as the end mark, which may lead to a reduction in system performance for large messages, but for C # language, some classes are provided to simplify the process, that is, system. i/O namespace stream class. Next we will also focus on this method. As for the second method, It will be described in the next article in combination with the entity information transmitted in the message.

In the previous article, we mentioned the networkstream class, which is used to send and receive messages. Here, I will mention another two stream classes, streamreader and streamwriter. These two classes can also be used to send and receive text messages over TCP.

When we obtain a networkstream object connected to the socket, we can use the following method to obtain the streamwriter and streamreader objects.

1 Networkstream NS = S. getstream ();
2 Streamreader SR =   New Streamreader (NS );
3 Streamwriter SW =   New Streamwriter (NS );

In this way, we can use streamwriter to send messages and use streamreader to receive messages:

1 // Send message
2 String Welcome =   " Welcome to my test sever " ;
3
4 Sw. writeline (welcome );
5 Sw. Flush ();

Receive message:

1 // Receive messages
2 String Data =   "" ;
3 Data = Sr. Readline ();

This is simpler than the previous practice, and it also solves the TCP Message boundary problem.

However, you must pay attention to the following points when using these methods:

1. In this method, message tags are used to solve the boundary problem. The tags here are line breaks. That is to say, the writeline () in streamwriter and the Readline () in streamreader must be used in pairs, otherwise, when Readline () is used in the client to read the information, it will not end, blocking the execution of the program and waiting for the line break.

2. In addition, ensure that the sent message itself should not contain line breaks. If the message itself has line breaks, these line breaks will be incorrectly marked by the Readline () method, affecting data integrity.

TCP Message boundary processing is now mentioned here. Due to insufficient understanding, errors may inevitably occur. Please correct them in time. In the next article, we will discuss the issue of transmitting object classes.

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.