I have a client / server application made with C#. Its working fine when i send Strings of messages to the server. But i need to send (simple) objects to the server instead of Strings. But i'm not sure how to do this. I currently have this for my client:
The class i want to send:
[Serializable]
public class Actions
{
// Movement
public bool forward = false;
public bool left = false;
public bool backward = false;
public bool right = false;
}
Then i send the object using the default Send() command. But before that happends i convert the Actions class to a byte array first like so:
public void SendObject ( object obj )
{
this.socket.Send ( this.ObjectToByteArray ( obj ) );
}
// Convert an object to a byte array
private byte[] ObjectToByteArray(object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
On the server side i receive the data like this (and thats were i'm stuck at...):
public void OnDataReceived(IAsyncResult asyn)
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
try
{
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
int iRx = socketData.m_currentSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
// CONVERT THE BYTE ARRAY BACK TO AN OBJECT
//object Cls = this.ByteArrayToObject ( socketData.dataBuffer );
}
catch ( ... ) { }
}
What do i have to do so i can access the object on the server?? Obviously when i do: Cls.forward;
Then it gives me an error because it doesn't know what properties the object has. Anyone any idea how to do this??