真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

在.NET中利用Remoting的數(shù)據(jù)進(jìn)行序列化方法

 一、 使用序列化類

創(chuàng)新互聯(lián)于2013年開始,先為黎城等服務(wù)建站,黎城等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為黎城企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

序列化一個(gè)類,并通過網(wǎng)絡(luò)傳輸需要三步:

1、將要序列化的類創(chuàng)建成一個(gè)library對(duì)象。

2、編寫一個(gè)發(fā)送程序來創(chuàng)建要序列化類的實(shí)例,并發(fā)送。

3、編寫一個(gè)接收程序從流中讀取數(shù)據(jù),并重新創(chuàng)建原來的序列化類。

① 編寫要序列化的類

每個(gè)要通過網(wǎng)絡(luò)傳輸數(shù)據(jù)的類必須在原代碼文件里使用[Serializable]標(biāo)簽。這表明,類中所有的數(shù)據(jù)在傳輸時(shí)都將要被序列化。下面展示了如何創(chuàng)建一個(gè)可以序列化的類。

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; } }

為了使用該類來傳輸數(shù)據(jù),必須現(xiàn)創(chuàng)建一個(gè)library文件: csc /t:library SerialEmployee.cs

② 編寫一個(gè)傳輸程序

創(chuàng)建數(shù)據(jù)類以后,可以創(chuàng)建一個(gè)程序來傳輸數(shù)據(jù)??梢允褂肂inaryFormatter和SoapFormatter類來序列化數(shù)據(jù)。

BinaryFormatter將數(shù)據(jù)序列化為二進(jìn)制流。通常在實(shí)際數(shù)據(jù)中,增加一些信息,例如類名和版本號(hào)信息。

也可以使用SoapFormatter類使用XML格式來傳輸數(shù)據(jù)。使用XML的好處就是可以在任何系統(tǒng)和程序間傳遞數(shù)據(jù)。

第一必須創(chuàng)建一個(gè)流的實(shí)例來傳遞數(shù)據(jù)??梢允侨魏晤愋偷牧?,包括FileStream,MemoryStream,NetworkStream。然后,可以創(chuàng)建一個(gè)序列化類,使用Serialize()方法來通過流對(duì)象傳遞數(shù)據(jù):

Stream str = new FileStream( "testfile.bin", FileMode.Create, FileAccess.ReadWrite); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(str, data); Iformatter類創(chuàng)建了一個(gè)用來序列化的類的實(shí)例(BinaryFormatter或者SoapFormatter),使用Serialize()類來將數(shù)據(jù)序列化

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(); } }

SoapFormatter類包含在System.Runtime.Serialization.Formatters.Soap命名空間,BinaryFormatter類包含在System.Runtime.Serialization.Formatters.Binary命名空間,Iformatter接口包含在System.Runtime.Serialization命名空間。

編譯代碼:CSC /r:SerialEmployee.dll SoapTest.cs

運(yùn)行SoapTest.exe程序后,可以查看產(chǎn)生的soaptest.xml文件

1 Blum Katie Jane 12 35000.5 2 Blum Jessica 9 23700.3

查看soaptest.xml文件,我們可以發(fā)現(xiàn)在序列化類中SOAP是如何定義每個(gè)數(shù)據(jù)元素。一個(gè)值得注意的重要XML數(shù)據(jù)特點(diǎn)如下:

"http://schemas.microsoft.com/clr/assem/SerialEmployee%2C%20Version%3D0.Â0.0.0.%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">

這里,XML中定義的數(shù)據(jù)使用了序列化數(shù)據(jù)類的實(shí)際類名。如果接收程序使用了另一個(gè)不同的類名,會(huì)和從流中讀取的XML數(shù)據(jù)不匹配。類不匹配,讀取將會(huì)失敗。

下面的代碼展示了如何序列化數(shù)據(jù),將數(shù)據(jù)傳送到遠(yuǎn)程系統(tǒng)。

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(); } }

因?yàn)锽inaryFormatter和SoapFormatter類需要一個(gè)Stream對(duì)象來傳遞序列化的數(shù)據(jù),所以要使用一個(gè)TCP Socket對(duì)象或者一個(gè)TcpClient對(duì)象來傳遞數(shù)據(jù),不能直接使用UDP。

③編寫一個(gè)接收程序

using System;

using System.Net;

using System.Net.Sockets;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

class BinaryDataRcvr { public static void Main() { TcpListener server = new TcpListener(9050);

server.Start();

TcpClient client = server.AcceptTcpClient();

NetworkStream strm = client.GetStream();

IFormatter formatter = new BinaryFormatter();

SerialEmployee emp1 = (SerialEmployee)formatter.Deserialize(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 = (SerialEmployee)formatter.Deserialize(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(); } } Winxp系統(tǒng):c:\Windows  c:\Windows\system32

 二、 程序改進(jìn)

在前面的程序中有一個(gè)假設(shè):發(fā)送者的所有數(shù)據(jù)都被接收者接收。如果數(shù)據(jù)丟失,調(diào)用Deserialize()方法會(huì)發(fā)生錯(cuò)誤。一個(gè)簡(jiǎn)單的解決方法是將序列化數(shù)據(jù)放到MemoryStream對(duì)象中。MemoryStream對(duì)象將所有的序列化數(shù)據(jù)保存在內(nèi)存中,可以很容易得到序列化數(shù)據(jù)的大小。當(dāng)傳遞數(shù)據(jù)時(shí),將數(shù)據(jù)大小和數(shù)據(jù)一起傳遞。

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(); } } 接收數(shù)據(jù)程序如下:

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(); } }


分享文章:在.NET中利用Remoting的數(shù)據(jù)進(jìn)行序列化方法
當(dāng)前路徑:http://weahome.cn/article/ggssjc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部