[C #] exploring data transmission object 1,

Source: Internet
Author: User

[C #] exploring data transmission object 1,

 

What is "reading a string from the current stream. The character string has a length prefix. A 7-digit character is encoded as an integer ."

To explore:

Write a simple program:

FileStream fs = new FileStream ("d: \ qqq.txt", FileMode. Create );
Var bw = new BinaryWriter (fs, Encoding. UTF8); // It is easy to understand without coding.
Bw. Write ("hello world! ");
Console. WriteLine ("successfully written ");
Bw. Close ();
Fs. Close ();


FileStream fs2 = new FileStream ("d: \ qqq.txt", FileMode. Open );
Var br = new BinaryReader (fs, Encoding. UTF8 );
Console. WriteLine (br. ReadString ());

 

1. Specify or default encoding when writing files, so that the characters can be converted to the corresponding encoding of the character set (ASCII octal hexadecimal)

During transmission or storage, it is saved as a standard binary system (which is then converted to binary system, and binary system is used for network transmission)

2. When you open it in the editor, the default 10-digit conversion will be made to the display text corresponding to the character set. (The computer is very clever and well-behaved. Girls like such boyfriends)

Ps:

 

 

Let's take a look at the file qqq.txt,

Start with UE) hexadecimal 0C = 10 hexadecimal 12, which is exactly the character length.

Let's take a look at BinaryWriter. Write.

 

Read/write echo.

The same is true for other heavy loads.

=================================== Advanced research ====== ====================================

Byte [] buffer = new byte [512];
While (size = netstream. Read (buffer, 0, buffer. Length)> 0)
{
Fs. Write (buffer, 0, size );
Len + = size;
}

 

And

Var br = new BinaryReader (netstream );
Console. WriteLine (br. ReadString ());

 

The two methods for retrieving data from the same stream have different results. Why?

Read Only returns the byte [] data object. readstring can only get the data in BinaryWriter.

Why does readstring contain garbled characters when BinaryWriter does not write content?

To test several cases

 

If BinaryWriter is not written, the receiving end reads the first part of the data into readstring and the remaining data is read.

 

 

 

After the transmission starts, netstream will be monitored.

 

 

 

After the sending end is added with using, you do not need to worry about whether the length of the bytes received by the server is greater than 0. (assume that there are two methods for the transmission listener: 1. Whether there is data, 2. close and dispose are in a higher status. close them directly)

 

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.