Hey guys I’m making a chat program and I have a parent “MessagePacket” class with a bunch of child classes for different types of packets to be sent, either containing a message for the main chat window, list of people currently chatting, private chat message, etc.
In the client the first thing I do is create a “AddClientPacket” and serialize/send it to the server, the server then deserializes it as its parent “MessagePacket” class which contains a type property that is used to determine what to cast/do with the generic MessagePacket it got.
When the server trys to deserialize it it causes a crash, im not sure why. Heres some code:
–SERVER–
private void HandleClientComm(Object client)
{
Client addClient = new Client(clientIds++, ((TcpClient)client).GetStream());
IFormatter formatter = new BinaryFormatter();
while (true)
{
MessagePacket packet = new MessagePacket();
try
{
packet = (MessagePacket)formatter.Deserialize(addClient.ClientStream);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
switch (packet.Type)
{
case MessageType.ALLCHAT:
{
messages.Enqueue(((AllChatPacket)packet).Message);
}
break;
case MessageType.ADDCLIENT:
{
addClient.ClientName = ((AddClientPacket)packet).ClientName;
clientNames.Add(addClient.ClientName);
clientList.Add(addClient);
}
break;
case MessageType.REMOVECLIENT:
{
clientNames.Remove(addClient.ClientName);
clientList.Remove(addClient);
}
break;
case MessageType.PRIVATECHAT:
{
}
break;
}
}
}
–CLIENT–
public void ListenToServer()
{
AddClientPacket addClient = new AddClientPacket();
addClient.Type = MessageType.ADDCLIENT;
name = addClient.ClientName = Interaction.InputBox("Whats your name", "Name?", "", 100, 100);
try
{
formatter.Serialize(clientStream, addClient);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
this.Close();
return;
}
while (true)
{
if (quit)
{
break;
}
MessagePacket packet = new MessagePacket();
try
{
packet = (MessagePacket)formatter.Deserialize(clientStream);
}
catch (Exception)
{
return;
}
switch (packet.Type)
{
case MessageType.ALLCHAT:
{
textBox1.Text = textBox1.Text + Environment.NewLine + ((AllChatPacket)packet).Message;
textBox1.Refresh();
}
break;
case MessageType.NAMELIST:
{
clientNameList.DataSource = ((NameListPacket)packet).ClientNames;
}
break;
}
}
}
That would be the issue then. Even though they ‘look’ the same to you, have the same properties, .NET sees them as completely different types if they are defined in two different assemblies and therefore will not let you serialize TypeA and deserialize it into TypeB. You are going to have to create a single .dll with that class definition that both share.