C# C/S应用程序 反序列化时出错 错误源是mscorlib

w_angfei 2013-09-18 12:24:20
服务器可以成功的反序列化客户端发过来的User对象,但是反序列化ScanfAppTaskInfo对象时提示:输入流是无效的二进制格式,其 中User对象和ScanfAppTaskInfo对象定义在了同一个ClassLibrary中,并且生成的ClassLibrary.dll文件客户 端和服务器同时都引用了,定义User类和ScanfAppTaskInfo类的时候也都同时加上了[Serializable],序列化和反序列化 User对象和ScanfAppTaskInfo对象用的是相同的方法,那反序列化ScanfAppTaskInfo对象时出错会是什么原因呢?下面是分 别是序列化和反序列化User对象和ScanfAppTaskInfo对象的代码
User类的定义
namespace ClassLibrary
{
[Serializable]
public class User
{


private string uname = "";
private string name = "";
private string pwd = "";
private string department = "";
//private Role role = null;

public string Username
{
get { return uname; }
set { uname = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}

public string Password
{
get { return pwd; }
set { pwd = value; }
}

//public Role Role
//{
// get { return role; }
// set { role = value; }
//}

public string Department
{
get { return department; }
set { department = value; }
}
}
}

ScanfAppTaskInfo类的定义
namespace ClassLibrary
{
[Serializable]
public class ScanfAppTaskInfo
{
//private User user=null;
private string use = "";
private string des="";
private string cls="";
private string filename="";
private string scanfRes="";
private string appOpn="";
private int pages=0;
private int counts=0;
private DataTable dtper = null;

//public User User
//{
// get { return user; }
// set { user = value; }
//}
public string Use
{
get { return use; }
set { use = value; }
}
public string Des
{
get { return des; }
set { des = value; }
}
public string Cls
{
get { return cls; }
set { cls = value; }
}
public string Filename
{
get { return filename; }
set { filename = value; }
}
public string ScanfRes
{
get { return scanfRes; }
set { scanfRes = value; }
}
public string AppOpn
{
get { return appOpn; }
set { appOpn = value; }
}
public int Pages
{
get { return pages; }
set { pages = value; }
}
public int Counts
{
get { return counts; }
set { counts = value; }
}

public DataTable Dtperson
{
get { return dtper; }
set { dtper = value; }
}

}
}


初始化User对象并序列化发给服务器端
user.Username = tbName.Text.Trim();
user.Password = tbPwd.Text.Trim();
//contactWithServer.User = user;
try
{
byte[] command = Encoding.ASCII.GetBytes("Login"); //命令
byte[] cmd = new byte[20];
for (int i = 0; i < command.Length; i++)
{
cmd[i]=command[i];
}
memStream = new MemoryStream();
formatter.Serialize(memStream, user);
byte[] data = memStream.ToArray(); //将User对象发送给服务器
memStream.Flush();
byte[] buffer = new byte[cmd.Length + data.Length];
Array.Copy(cmd,0,buffer,0,cmd.Length);
Array.Copy(data,0,buffer,cmd.Length,data.Length);
contactWithServer.sendMsg(buffer,buffer.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
finally
{
memStream.Close();
}

服务器端反序列化User对象
[code=csharp]buff = new byte[data.Length - leng];
for (int j = 0; j < data.Length - leng; j++)
buff[j] = data[leng + j];
memStream = new MemoryStream(buff);
memStream.Seek(0, SeekOrigin.Begin);
obj = formatter.Deserialize(memStream);
memStream.Close();
if (obj != null && obj is User)
user = (User)obj;

客户端初始化ScanfAppTaskInfo对象并序列化并发给服务器
sataskinfo.Use = cbusing.Text.Trim();
sataskinfo.Des = cbdestination.Text.Trim();
sataskinfo.Cls = cbclassfied.Text.Trim();
sataskinfo.Pages = (int)numericpages.Value;
sataskinfo.Counts = (int)numericcount.Value;
sataskinfo.Filename = tbfilename.Text.Trim();
sataskinfo.ScanfRes = tbScanreason.Text.Trim();

try
{
byte[] command = Encoding.ASCII.GetBytes("CmfScanfApp"); //命令
byte[] cmd = new byte[20];
for (int i = 0; i < command.Length; i++)
{
cmd[i] = command[i];
}
memStream = new MemoryStream();
formatter.Serialize(memStream, sataskinfo);
byte[] data = memStream.ToArray(); //将User对象发送给服务器
memStream.Flush();
byte[] buffer = new byte[cmd.Length + data.Length];
Array.Copy(cmd, 0, buffer, 0, cmd.Length);
Array.Copy(data, 0, buffer, cmd.Length, data.Length);
MessageBox.Show(buffer.Length.ToString(),"djkkkkkkkkkkk");
contanctWithServer.sendMsg(buffer, buffer.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
finally
{
memStream.Close();
}[/code]
服务器端反序列化ScanfAppTaskInfo对象
  try
{
buffer = new byte[data.Length - leng];
for (int j = 0; j < data.Length - leng; j++)
buffer[j] = data[leng + j];
memStream = new MemoryStream(buffer);
memStream.Seek(0, SeekOrigin.Begin);
obj = formatter.Deserialize(memStream);
memStream.Close();
if (obj != null && obj is ScanfAppTaskInfo)
sataskinfo = (ScanfAppTaskInfo)obj;
MessageBox.Show(sataskinfo.ScanfRes);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
MessageBox.Show(ex.Source);
}

实在是看不出来这序列化和反序列化这两个类有什么区别,大家帮忙看看
...全文
301 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
w_angfei 2013-09-18
  • 打赏
  • 举报
回复
问题找见了。。。。我在接受的时候定义了个局部的变量,而反序列化时用的却是局部的,哎,太不细心了,其余操作都是正确的
w_angfei 2013-09-18
  • 打赏
  • 举报
回复
引用 1 楼 devmiao 的回复:
可能是你的字节流和你定义的类型不匹配。
我用的不是字节流,是内存流。。
devmiao 2013-09-18
  • 打赏
  • 举报
回复
可能是你的字节流和你定义的类型不匹配。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧