套接字,接收序列化异常
Posted
技术标签:
【中文标题】套接字,接收序列化异常【英文标题】:Sockets, recieving Serialization Exception 【发布时间】:2015-02-19 23:25:59 【问题描述】:首先,我已经阅读了 *** 和其他地方的许多指南和帖子,但仍然没有解决这个问题。我最近一直在尝试了解套接字,事实证明这是一个挑战。我已经了解如何使用带有字符串等的套接字,但我知道想要开始发送对象,以便更好地组织我的程序。我正在做一个聊天客户端;服务器将连接多个客户端,我猜它的工作方式有点像 IRC。我已经制作了服务器的基础知识;我有方法来检查收到的数据包类型(所有数据包都继承自Packet
),并且有一个非常基本的 switch 语句,它根据收到的数据包采取行动。在此之前,我显然需要将套接字中收到的byte[]
转换为Packet
。当我尝试反序列化字节数组时,问题就出现了。我在调用 .Deserialize(); 的行上得到了 SerilizationException以下是正文:
System.Runtime.Serialization.SerializationException 未被处理 用户代码 HResult=-2146233076 消息=找不到程序集 '客户端,版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = null'。
我不明白为什么会这样;我尝试编写不同的反序列化数组的方法,添加随机 if 语句以确保我没有传递一个空数组(我不是),但不能让它为我的一生工作。我希望有人能指出我做错了什么?我打算通过套接字发送对象都错了吗?我只是错过了代码中的一些基本内容吗?
我已经包含了我的客户端的准系统(它所做的只是发送一个带有字符串的MessagePacket
,我的服务器也包含在内。现在,这个程序无论如何都不是最漂亮的,所以我提前道歉; 这对我来说都是一个学习曲线!
服务器:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Chat_Application.Packets;
namespace Chat_Application
class Server
private const int port = 1090;
private readonly ManualResetEvent allDone;
private readonly Dictionary<String, Socket> connections; // will hold all client sockets
private readonly IPAddress ipAddress;
private readonly IPEndPoint ipEndPoint;
private readonly Thread listenThread; // seperate thread to run the server
private readonly Socket serverSocket;
public Server()
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connections = new Dictionary<string, Socket>();
ipAddress = IPAddress.Parse(GetLocalIPv4(NetworkInterfaceType.Ethernet));
ipEndPoint = new IPEndPoint(ipAddress, port);
listenThread = new Thread(StartListen);
allDone = new ManualResetEvent(false);
//called to start the new thread
public void Start()
listenThread.Start();
//TODO: implement this method
public void Stop()
throw new NotImplementedException();
//method that is run on the new server thread
public void StartListen()
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(20);
String statusUpdate = "\n<INFO> Socket bound, listening for connections...";
Program.mainWin.AddConsoleText(statusUpdate);
while (true)
allDone.Reset();
serverSocket.BeginAccept(AcceptConnectionAsync, serverSocket);
allDone.WaitOne();
//Asynchronus method to recieve connections. The logic of the way things are done is not great, it is only temporary while I learn better ways to do things
public void AcceptConnectionAsync(IAsyncResult ar)
var bufferBytes = new byte[1024];
Packet packet = null;
allDone.Set();
var listener = (Socket) ar.AsyncState;
var client = listener.EndAccept(ar);
client.Receive(bufferBytes);
packet = ConvertPacket(bufferBytes);
switch (CheckPacketType(packet))
case PacketType.Message:
packet = ConvertPacket(bufferBytes);
var messagePacket = (MessagePacket) packet;
String userMessage = "(" + messagePacket.userName + ") " +
Encoding.UTF8.GetString(messagePacket.message);
Program.mainWin.AddConsoleText(userMessage);
break;
case PacketType.Connection:
packet = ConvertPacket(bufferBytes);
var connectionPacket = (Connect) packet;
String connectionMessage = "\n<CONNECT>New connection established to " +
connectionPacket.userName;
connections.Add(connectionPacket.userName, client);
Program.mainWin.AddConsoleText(connectionMessage);
break;
case PacketType.Command:
// TODO Implement this case, add new packet type
packet = ConvertPacket(bufferBytes);
var commandPacket = (MessagePacket) packet;
break;
case PacketType.Disconnect:
packet = ConvertPacket(bufferBytes);
var disconnectPacket = (Dissconnect) packet;
String disconnectMessage = "\n<DISCONNECT>User " + disconnectPacket.userName +
" Disconnected succesffuly";
connections.Remove(disconnectPacket.userName);
if (disconnectPacket.goodDissconnect)
Program.mainWin.AddConsoleText(disconnectMessage);
else
String message = "\n<DISCONNECT>User " + disconnectPacket.userName +
" Disconnected unexpectedly";
Program.mainWin.AddConsoleText(message);
break;
case PacketType.Request:
// TODO Implement this case, add new packet type
packet = ConvertPacket(bufferBytes);
var requestPacket = (MessagePacket) packet;
break;
case PacketType.Default:
MessageBox.Show("Packet Error", "Empty Packet Recieved");
break;
default:
break;
//used to check what type of packet has been recieved
private PacketType CheckPacketType(Packet packet)
switch (packet.type)
case PacketType.Message:
return PacketType.Message;
break;
case PacketType.Connection:
return PacketType.Connection;
break;
case PacketType.Command:
return PacketType.Command;
break;
case PacketType.Disconnect:
return PacketType.Disconnect;
break;
case PacketType.Request:
return PacketType.Request;
break;
default:
break;
return PacketType.Default;
//converts the byte array to a packet
private Packet ConvertPacket(byte[] bytes)
Packet packet = null;
//try to convert byte array to a Packet, the error occurs on the last line; when trying to de-serialize
/*try
*/
var memStream = new MemoryStream();
var binForm = new BinaryFormatter();
memStream.Write(bytes, 0, bytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
packet = (Packet) binForm.Deserialize(memStream);
//
/*catch (Exception) // MAKE THIS THE APPROPRIATE EXCEPTION
MessageBox.Show("An unexpected error has occured when atempting to convert a packet!");
*/
if (packet != null)
return packet;
else
MessageBox.Show("Error converting packet!");
return null;
//gets the local ip
public string GetLocalIPv4(NetworkInterfaceType _type)
var output = "";
foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
foreach (var ip in item.GetIPProperties().UnicastAddresses)
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
output = ip.Address.ToString();
return output;
客户:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Chat_Application.Packets;
namespace Client
public partial class ClientWindow : Form
public ClientWindow()
InitializeComponent();
private void ClientWindow_Load(object sender, EventArgs e)
MessagePacket packet = new MessagePacket("Chris", "Hello Server!");
byte[] data;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Parse(GetLocalIPv4(NetworkInterfaceType.Ethernet)), 1090));
packet.type = PacketType.Message;
data = ConvertPacket(packet);
//to check if the packet has been converted correctly
if (data != null)
socket.Send(data);
public string GetLocalIPv4(NetworkInterfaceType _type)
string output = "";
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
output = ip.Address.ToString();
return output;
private byte[] ConvertPacket(Packet packet)
byte[] bytes = new byte[1024];
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
formatter.Serialize(ms, packet);
bytes = ms.ToArray();
return bytes;
【问题讨论】:
【参考方案1】:这个例外对我来说似乎很清楚。当您尝试反序列化数据时,显然找不到序列化数据时引用的Client
程序集。
很遗憾,您的代码示例不包含有关您在何处以及如何声明 Packet
类的信息。但根据您报告的行为,在我看来,您可能已将同一个 .cs 文件链接到两个不同的项目,结果每个程序集都以自己的 Packet
类型的私有副本结束。
如果是这样,那么至少有几个很好的解决方案:
-
仅在一个程序集中定义类型,并根据需要引用该程序集。例如,您可以在
Client.dll
程序集中定义它,然后将Client.dll
程序集添加为Server.dll
程序集的引用。或者,您可以构建包含该类型的第三个程序集,并让您的 Client.dll
和 Server.dll
程序集引用该第三个程序集。
请改用XmlSerializer
。 XML 不包含需要特定程序集来定义相关类型的类型信息。这在使用XmlSerializer
映射到在.NET 之外处理的XML 时更常用(即,无法共享一个通用程序集来定义类型),但这种方法也可以解决您的特定问题。
就我个人而言,我会选择选项 #1,并将类型放入客户端和服务器共享的 DLL 中。
【讨论】:
所以你说我应该从 VS 菜单中创建一个新的 Windows 类库中的数据包类,在那里定义功能等,然后将其导出为 .dll 并从导入的数据包类中引用.dll?我从未使用过 .dll,但从我所看到的情况来看应该很容易。只是为了确保我理解,发生错误是因为我引用了两个具有完全相同名称等但来自不同项目的相同对象? @Sicarius:“发生错误是因为我引用了两个具有完全相同名称等但来自不同项目的相同对象?” - 对,就是这样。是的,您应该按照您的描述创建一个新的 DLL 项目。如果它们在同一个解决方案中,则将其作为对服务器和客户端项目的引用很容易添加,因为“添加引用...”对话框有一个“解决方案”面板,其中列出了解决方案中的项目。只需选择面板,检查新的 DLL 作为参考,就可以开始了。以上是关于套接字,接收序列化异常的主要内容,如果未能解决你的问题,请参考以下文章