I have been busy recently and have not written a blog for a long time.
This topic is actually something I have always wanted to talk about. socket communication has always been a very troublesome issue. let's talk about socket transmission. we have prepared a demo to show you how to transmit text, objects, images, and files using TCP/IP. I am going to discuss a topic about secure UDP file transmission later. If you are interested, please pay attention to it.
For network data transmission, we generally think of two protocols, namely TCP/IP and UDP, which have their own characteristics. Here, we will expand on TCP/IP.
I believe everyone is familiar with the basic data of text transmission. the key to transmission is the object. in network transmission, the transmitted content is actually byte, that is, byte. You will find that all file data, such as their smallest unit, is actually byte.
For small ones, such as small images, we can serialize them into byte arrays and transmit them through the network. to serialize a binary file, use a namespace.
Using System. runtime. serialization;
Using System. runtime. serialization. formatters. Binary;
Binaryformatter BF= NewBinaryformatter ();
Memorystream MS= NewMemorystream ();
BF. serialize (MS, image );//Serialize an image to a memory stream
The transmitted data is serialized to the byte in the memory stream. After receiving the data from the other end, the file bytecode is converted to the restored file through deserialization.
Binaryformatter BF = New Binaryformatter ();
S. Seek ( 0 , Seekorigin. Begin );
Image img = (Image) BF. deserialize (s );
We can say that we can adopt the serialization/deserialization mode for small objects, but what about large files? If it is serialized to the memory, the memory will increase, which is obviously not what we want. Next we will discuss the transmission of large files.
--------------------------------
Demo