Original Post
Hello, I've started working on serialization and deserialization coupled with networking and am having trouble. In order to accomplish what I'm trying to do, I was told I would have to share an assembly file among my two projects in order to cast one 'player' object to another when it arrives at the server/client. Client Server It compiles just fine, but when the server attempts to cast the remote player object to a local player object, I receive the error <Unable to cast object of type 'Player' to type 'Player'. So far I've: 1) Created a strong name 2) Added the .exe from the client directory to the GAC 3) Profit? No. I'm probably forgetting something or doing something wrong (probably two) Any ideas?
class Program
{
static void Main(string[] args)
{
Process();
}
static void Process()
{
Stream stream;
TcpClient tcpClient = new TcpClient();
MemoryStream memoryStream;
BinaryFormatter binaryFormatter;
const string IP = @"127.0.0.1";
const int PORT = 5055;
IPAddress ipAddress = IPAddress.Parse(IP);
Player player = new Player();
try {
tcpClient.Connect(ipAddress, PORT);
while (true) {
using (stream = tcpClient.GetStream()) {
using (memoryStream = new MemoryStream()) {
Console.Write("Send to server? (y/n) > ");
string input = Console.ReadLine().ToUpper();
if (input[0] == 'Y') {
binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, player);
byte[] buffer = memoryStream.GetBuffer();
stream.Write(buffer, 0, buffer.Length);
}
memoryStream.Close();
}
stream.Close();
}
}
} catch (Exception e) {
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
[Serializable()]
class Player : ISerializable
{
private string name;
private int level;
public Player()
{
this.name = "Default";
this.level = 1;
}
public Player(SerializationInfo info, StreamingContext context)
{
this.name = (string)info.GetValue("Name", typeof(string));
this.level = (int)info.GetValue("Level", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", name);
info.AddValue("Level", level);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Runtime.CompilerServices;
class Program
{
static void Main(string[] args)
{
Serve();
}
public static void Serve()
{
Player player = new Player();
Socket socket;
TcpListener tcpListener;
Stream stream;
MemoryStream memoryStream;
BinaryFormatter binaryFormatter;
const int PORT = 5055;
tcpListener = new TcpListener(IPAddress.Any, PORT);
tcpListener.Start();
Console.WriteLine("Server started on port {0}", PORT);
Console.WriteLine("Waiting for connections");
while (true) {
socket = tcpListener.AcceptSocket();
Console.WriteLine("[{0}] has connected", socket.RemoteEndPoint);
try {
using (stream = new NetworkStream(socket)) {
using (memoryStream = new MemoryStream()) {
binaryFormatter = new BinaryFormatter();
byte[] buffer = new byte[512];
int bytesReceived;
while ((bytesReceived = stream.Read(buffer, 0, 512)) != 0) {
memoryStream.Write(buffer, 0, 512);
}
memoryStream.Seek(0, 0);
player = (Player)binaryFormatter.Deserialize(memoryStream);
memoryStream.Close();
}
stream.Close();
}
} catch (Exception e) {
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
[Serializable()]
class Player : ISerializable
{
private string name;
private int level;
public Player()
{
this.name = "Default";
this.level = 1;
}
public Player(SerializationInfo info, StreamingContext context)
{
this.name = (string)info.GetValue("Name", typeof(string));
this.level = (int)info.GetValue("Level", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", name);
info.AddValue("Level", level);
}
}