Transfer object (1)

Source: Internet
Author: User
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

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.