首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 泛型集合序列化和反序列化有关问题

2012-03-07 
C# 泛型集合序列化和反序列化问题各位高手,请指定一下,我这个是用Udp协议做的通信程序,现在时发送和接收泛

C# 泛型集合序列化和反序列化问题
各位高手,请指定一下,我这个是用Udp协议做的通信程序,现在时发送和接收泛型集合,发送没问题,接收的时候不知道怎么反序列化????报的错是“没有对象“201326592”的映射。”,原因好像是序列化后没有反回来吧!!

/发送数据  
  public void SendData()
  {
  try
  {
  //定义对方主机和端口
  IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(txtHisIP.Text), int.Parse(txtHisPort.Text));
  //实例化并赋值 
  Users user = new Users();
  user.Name = txtName.Text.Trim();
  user.Sex = txtSex.Text.Trim();
  user.Age = int.Parse(txtAge.Text.Trim());
  user.Address = txtAddress.Text.Trim();
  user.Phone = ulong.Parse(txtPhone.Text.Trim());
  user.Content = txtContent.Text.Trim();
  List<Users> list = new List<Users>();
  list.Add(user);
  //序列化集合
  FileStream fs = new FileStream("C:\\go.txt", FileMode.Open); //定义一个文件流
  BinaryFormatter bf = new BinaryFormatter(); //二进制方式
  bf.Serialize(fs, list); //序列化保存配置文件对象list
  fs.Seek(0, SeekOrigin.Begin);
  string ss = "";
  //读取出来
  using (StreamReader sr = new StreamReader(fs))
  {
  ss = sr.ReadToEnd();
  sr.Close();
  }

  #region 类序列化
  ////类序列化
  //xml = new XmlSerializer(typeof(Users));
  //ms = new MemoryStream();

  //xml.Serialize(ms, user);
  //ms.Seek(0, SeekOrigin.Begin); //将当前流的位置设为指定值
  //string str = Encoding.UTF8.GetString(ms.ToArray()); 
  #endregion
  //string to byte[]
  byte[] by = Encoding.UTF8.GetBytes(ss);
  //发送
  udpClient.Send(by, by.Length, ipPoint);

  toolsStatus.Text = "已发送!";
  fs.Close(); //关闭流
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }

接收:
//接收任何地址的数据 接收
  IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
  //接收数据,储存在byte数组里
  byte[] bytes = udpClient.Receive(ref point);
  //byte to string
  string str = Encoding.UTF8.GetString(bytes); //乱码问题
  //反序列化集合
  FileStream fs = new FileStream("F:\\go.txt", FileMode.Create);
  using (StreamWriter sw = new StreamWriter(fs))
  {
  sw.Write(str); //写入
  sw.Close();
  }
  fs.Close();

  FileStream fsf = new FileStream("F:\\go.txt", FileMode.Open);
  BinaryFormatter bf = new BinaryFormatter();
  List<Users> list = bf.Deserialize(fsf) as List<Users>; //获取

[解决办法]
http://www.cnblogs.com/lindping/archive/2011/01/15/2004836.html
------解决方案--------------------


你发送和读取,用string读取都是错误的。
1. 序列化之后的 byte[] 应该直接Send ,保存用 System.IO.File.WriteAllBytes 保存二进制数据文件
而不用做任何编码转换。
2. 读取的话也是直接用 byte[] 转成 MemoryStream 然后反序列化
 

//读取出来
using (StreamReader sr = new StreamReader(fs))
{
ss = sr.ReadToEnd();
sr.Close();
}

... 

byte[] bytes = udpClient.Receive(ref point);
//byte to string
string str = Encoding.UTF8.GetString(bytes); //乱码问题
//反序列化集合
FileStream fs = new FileStream("F:\\go.txt", FileMode.Create);


[解决办法]
首先你的Users对象必须是再Client端和Server端共享的DLL来的,而且要可序列化,根据你的问题我建了三个Project:
1.ClassLibrary1.dll,放User class,在Client端和Server都同时引用它。
[Serializable]
public class Users
{
public int Age { get; set; }
public string Name { get; set; }

public object Sex { get; set; }

public object Address { get; set; }

public ulong Phone { get; set; }

public object Content { get; set; }
}
2.client project

 class Program
{
private static UdpClient udpClient;
static void Main(string[] args)
{
udpClient = new UdpClient();
SendData();

Console.Read();
}


public static void SendData()
{
try
{
//定义对方主机和端口
IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9800);
//实例化并赋值 
Users user = new Users
{
Name = "test",
Sex = "1",
Age = 27,
Address = "127.0.0.1",
Phone = 13698756236,
Content = "test"
};
List<Users> list = new List<Users>();
list.Add(user);
//序列化集合
Stream s = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter(); //二进制方式
bf.Serialize(s, list); //序列化保存配置文件对象list

byte[] by = new byte[s.Length];
s.Position = 0;
s.Read(by, 0, by.Length);

//发送
udpClient.Send(by, by.Length, ipPoint);

Console.WriteLine("success!~");

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

}
}

3.Server:
class Program
{
private static UdpClient udpClient;
static void Main(string[] args)
{
udpClient = new UdpClient(9800); //这里Server端的监听端口必须是Client发送的端口

ReceiveData();
Console.Read();
}



private static void ReceiveData()
{
IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
//接收数据,储存在byte数组里
byte[] bytes = udpClient.Receive(ref point);

Stream s = new MemoryStream(bytes);

BinaryFormatter bf = new BinaryFormatter();
List<Users> list = bf.Deserialize(s) as List<Users>; //获取

Console.WriteLine(list[0].Name + " >> " + list[0].Address);
}
}

[解决办法]
private void 序列号ToolStripMenuItem_Click(object sender, EventArgs e)


{
//将对象序列化到文件中去
//创建文件流对象
FileStream fs = new FileStream("data.dat", FileMode.OpenOrCreate, FileAccess.Write);
//创建格式化器(序列化器);
BinaryFormatter bf = new BinaryFormatter();
//进行序列化或反序列化操作
bf.Serialize(fs, ManagerHelper.School);
fs.Close();
}

private void 反序列化ToolStripMenuItem_Click(object sender, EventArgs e)
{
//创建文件流对象
FileStream fs = new FileStream("data.dat", FileMode.Open, FileAccess.Read);
//创建格式化器(序列化器);
BinaryFormatter bf = new BinaryFormatter();
//进行序列化或反序列化操作
SortedList obj = (SortedList)bf.Deserialize(fs);
fs.Close();
ManagerHelper.School = obj;
}

热点排行