4

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??

w00
  • 26,172
  • 30
  • 101
  • 147
  • 1.Do you want to socket only ? There are other solution for this like remoting or WCF. Also when multiple client commnuniate than its problem. 2. Another way is pass this way First send SizeofByarray Than send object array. – dotnetstep Dec 17 '11 at 13:28
  • I know it's been a long time. Do you have a better way of doing this now? – answerSeeker Nov 03 '20 at 10:01

2 Answers2

2

Cast it from an object into a Actions:

object Cls = this.ByteArrayToObject ( socketData.dataBuffer );
Actions action = (Actions)Cls;

That's the simplest way to do it. You should also add exception handling, type checking, etc.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Generally, both the client and server need to know about the types of objects that could be serialized across the connection.

Secondly, you need a way of indicating what kind of object you are passing, so the other end knows how to deserialize it. This could be by convention (the same call always sends the same kind of message) or with additional data in the message stream. Or with a different serializer.

Have you looked at WCF at all? It provides ways of abstracting all of this rather nicely through data contracts.

Joe
  • 41,484
  • 20
  • 104
  • 125
  • I can't use WCF because i'm using a game engine. – w00 Dec 17 '11 at 14:06
  • I don't see how those two things conflict with each other, unless the engine includes the actual client/server communications. – Joe Dec 17 '11 at 14:25