Data This article describes two classes, BinaryFormatter and SoapFormatter, that transmit serialized data over the network. These classes can convert an instance of a class into a stream of bytes over the network to a remote system, or they can be converted back to the original data.
First, use serialization class
Serializing a class and transferring over the network requires three steps:
1, the class that will be serialized is created as a library object.
2. Write a sending program to create an instance of the class to serialize and send it.
3. Write a receiving program to read the data from the stream and recreate the original serialization class.
① write the class to serialize
Each class that transmits data over the network must use the [Serializable] tag in the original code file. This indicates that all data in the class will be serialized when it is transmitted. The following shows how to create a class that can be serialized.
Using System;
[Serializable]
public class Serialemployee
{
public int EmployeeID
public string LastName;
public string FirstName;
public int yearsservice;
public double Salary;
Public Serialemployee ()
{
EmployeeID = 0;
LastName = null;
FirstName = null;
Yearsservice = 0;
Salary = 0.0;
}
}
In order to use this class to transmit data, you must now create a library file:
Csc/t:library SerialEmployee.cs
② Write a transport program
After you create a data class, you can create a program to transfer data. You can use the BinaryFormatter and SoapFormatter classes to serialize data.
BinaryFormatter serializes data into a binary stream. Usually, in the actual data, add some information, such as class name and version number information.
You can also use the SoapFormatter class to transfer data using XML format. The advantage of using XML is that data can be passed between any system and program.
First, you must create an instance of the stream to pass the data. Can be any type of stream, including Filestream,memorystream,networkstream. You can then create a serialization class that uses the serialize () method to pass data through the Stream object:
Stream str = new FileStream ("Testfile.bin", FileMode.Create, FileAccess.ReadWrite);
IFormatter formatter = new BinaryFormatter ();
Formatter. Serialize (str, data);
The IFormatter class creates an instance of the class to serialize (BinaryFormatter or SoapFormatter) and uses the Serialize () class to serialize the data
Using System;
Using System.IO;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Soap;
Class Soaptest
{
public static void Main ()
{
Serialemployee emp1 = new Serialemployee ();
Serialemployee emp2 = new Serialemployee ();
Emp1. EmployeeID = 1;
Emp1. LastName = "Blum";
Emp1. FirstName = "Katie Jane";
Emp1. Yearsservice = 12;
Emp1. Salary = 35000.50;
Emp2. EmployeeID = 2;
Emp2. LastName = "Blum";
Emp2. FirstName = "Jessica";
Emp2. Yearsservice = 9;
Emp2. Salary = 23700.30;
Stream str = new FileStream ("Soaptest.xml", FileMode.Create,
FileAccess.ReadWrite);
IFormatter formatter = new SoapFormatter ();
Formatter. Serialize (str, EMP1);
Formatter. Serialize (str, EMP2);
Str. Close ();
}
}
The SoapFormatter class is contained in the System.Runtime.Serialization.Formatters.Soap namespace, and the BinaryFormatter class is contained in the System.Runtime.Serialization.Formatt ERs. Binary namespaces, IFormatter interfaces are included in the System.Runtime.Serialization namespace.
After you run the SoapTest.exe program, you can view the resulting Soaptest.xml file
<soap-env:envelope xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Â
Xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" Xmlns:soap-enc=â
"http://schemas.xmlsoap.org/soap/encoding/" Xmlns:soap-env=â
"Http://schemas.xmlsoap.org/soap/envelope/" Xmlns:clr=â
"Http://schemas.microsoft.com/soap/encoding/clr/1.0" Soap-env:encodingstyle=â
"http://schemas.xmlsoap.org/soap/encoding/" >
<SOAP-ENV:Body>
<a1:serialemployee id= "Ref-1" Xmlns:a1=â
"Http://schemas.microsoft.com/clr/assem/SerialEmployee%2C%20Version%3D0. Â
0.0.0%2c%20culture%3dneutral%2c%20publickeytoken%3dnull ">
<EmployeeID>1</EmployeeID>
<lastname id= "Ref-3" >Blum</LastName>
<firstname id= "ref-4" >katie jane</firstname>
<YearsService>12</YearsService>
<Salary>35000.5</Salary>
</a1:SerialEmployee>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<soap-env:envelope xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Â
Xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" Xmlns:soap-enc=â
"http://schemas.xmlsoap.org/soap/encoding/" Xmlns:soap-env=â
"Http://schemas.xmlsoap.org/soap/envelope/" Xmlns:clr=â
"Http://schemas.microsoft.com/soap/encoding/clr/1.0" Soap-env:encodingstyle=â
"http://schemas.xmlsoap.org/soap/encoding/" >
<SOAP-ENV:Body>
<a1:serialemployee id= "Ref-1" Xmlns:a1=â
"Http://schemas.microsoft.com/clr/assem/SerialEmployee%2C%20Version%3D0. Â
0.0.0%2c%20culture%3dneutral%2c%20publickeytoken%3dnull ">
<EmployeeID>2</EmployeeID>
<lastname id= "Ref-3" >Blum</LastName>
<firstname id= "Ref-4" >Jessica</FirstName>
<YearsService>9</YearsService>
<Salary>23700.3</Salary>
</a1:SerialEmployee>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Looking at the Soaptest.xml file, we can see how soap defines each data element in the serialization class. A noteworthy key XML Data feature is as follows:
<a1:serialemployee id= "Ref-1" Xmlns:a1=â
"Http://schemas.microsoft.com/clr/assem/SerialEmployee%2C%20Version%3D0. Â0.0.0.%2c%20culture%3dneutral%2c%20publickeytoken%3dnull ">
Here, the data defined in XML uses the actual class name of the serialized data class. If the receiving program uses another different class name, it does not match the XML data read from the stream. Class does not match, the read will fail.
The following code shows how to serialize data and transfer data to a remote system.
Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Binary;
Class Binarydatasender
{
public static void Main ()
{
Serialemployee emp1 = new Serialemployee ();
Serialemployee emp2 = new Serialemployee ();
Emp1. EmployeeID = 1;
Emp1. LastName = "Blum";
Emp1. FirstName = "Katie Jane";
Emp1. Yearsservice = 12;
Emp1. Salary = 35000.50;
Emp2. EmployeeID = 2;
Emp2. LastName = "Blum";
Emp2. FirstName = "Jessica";
Emp2. Yearsservice = 9;
Emp2. Salary = 23700.30;
TcpClient client = new TcpClient ("127.0.0.1", 9050);
IFormatter formatter = new BinaryFormatter ();
NetworkStream STRM = client. GetStream ();
Formatter. Serialize (STRM, EMP1);
Formatter. Serialize (STRM, EMP2);
Strm. Close ();
Client. Close ();
}
}
Because the BinaryFormatter and SoapFormatter classes require a Stream object to pass serialized data, you cannot use UDP directly to use a TCP socket object or a TcpClient object to pass data.
There is a hypothesis in the previous program that all data for the sender is received by the receiver. If the data is lost, the call to the deserialize () method can cause an error. A simple workaround is to put the serialized data into the MemoryStream object. The MemoryStream object saves all serialized data in memory, and can easily get the size of the serialized data. When data is passed, the data size and data are passed together.
Using System;
Using System.IO;
Using System.Net;
Using System.Net.Sockets;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Soap;
Class Betterdatasender
{
public void SendData (NetworkStream strm, Serialemployee EMP)
{
IFormatter formatter = new SoapFormatter ();
MemoryStream MEMSTRM = new MemoryStream ();
Formatter. Serialize (MEMSTRM, EMP);
byte[] data = MEMSTRM. GetBuffer ();
int memsize = (int) memstrm. Length;
byte[] size = bitconverter.getbytes (memsize);
Strm. Write (Size, 0, 4);
Strm. Write (data, 0, memsize);
Strm. Flush ();
Memstrm. Close ();
}
Public Betterdatasender ()
{
Serialemployee emp1 = new Serialemployee ();
Serialemployee emp2 = new Serialemployee ();
Emp1. EmployeeID = 1;
Emp1. LastName = "Blum";
Emp1. FirstName = "Katie Jane";
Emp1. Yearsservice = 12;
Emp1. Salary = 35000.50;
Emp2. EmployeeID = 2;
Emp2. LastName = "Blum";
Emp2. FirstName = "Jessica";
Emp2. Yearsservice = 9;
Emp2. Salary = 23700.30;
TcpClient client = new TcpClient ("127.0.0.1", 9050);
NetworkStream STRM = client. GetStream ();
SendData (STRM, EMP1);
SendData (STRM, EMP2);
Strm. Close ();
Client. Close ();
}
public static void Main ()
{
Betterdatasender BDS = new Betterdatasender ();
}
}
The receiving data program is as follows:
Using System;
Using System.IO;
Using System.Net;
Using System.Net.Sockets;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Soap;
Class BETTERDATARCVR
{
Private Serialemployee RecvData (NetworkStream strm)
{
MemoryStream MEMSTRM = new MemoryStream ();
byte[] data = new BYTE[4];
int recv = STRM. Read (data, 0, 4);
int size = Bitconverter.toint32 (data, 0);
int offset = 0;
while (Size > 0)
{
data = new byte[1024];
recv = Strm. Read (data, 0, size);
Memstrm. Write (data, offset, recv);
Offset + + recv;
size = recv;
}
IFormatter formatter = new SoapFormatter ();
Memstrm. Position = 0;
Serialemployee emp = (serialemployee) formatter. Deserialize (MEMSTRM);
Memstrm. Close ();
return EMP;
}
Public BETTERDATARCVR ()
{
TcpListener Server = new TcpListener (9050);
Server. Start ();
TcpClient client = server. AcceptTcpClient ();
NetworkStream STRM = client. GetStream ();
Serialemployee emp1 = RecvData (STRM);
Console.WriteLine ("Emp1.") EmployeeID = {0} ", Emp1. EmployeeID);
Console.WriteLine ("Emp1.") LastName = {0} ", Emp1. LastName);
Console.WriteLine ("Emp1.") FirstName = {0} ", Emp1. FirstName);
Console.WriteLine ("Emp1.") Yearsservice = {0} ", Emp1. Yearsservice);
Console.WriteLine ("Emp1.") Salary = {0}\n ", emp1. Salary);
Serialemployee emp2 = RecvData (STRM);
Console.WriteLine ("Emp2.") EmployeeID = {0} ", emp2. EmployeeID);
Console.WriteLine ("Emp2.") LastName = {0} ", emp2. LastName);
Console.WriteLine ("Emp2.") FirstName = {0} ", emp2. FirstName);
Console.WriteLine ("Emp2.") Yearsservice = {0} ", emp2. Yearsservice);
Console.WriteLine ("Emp2.") Salary = {0} ", emp2. Salary);
Strm. Close ();
Server. Stop ();
}
public static void Main ()
{
BETTERDATARCVR BDR = new BETTERDATARCVR ();
}
}
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.