Send object over a TCP connection

5.6k Views Asked by At

I've started windows mobile programming today and I have successfully connected to my server.

The application I am making on Visual Studio is not a universal application, but a Windows Mobile Application.

The API DataWriter is used to write data to an output stream, in the applications scenario the output stream is the socket. I.E:

            DataWriter dw = new DataWriter(clientSocket.OutputStream);

One of the methods I have been looking at is WriteBytes and WriteBuffer (Documentation can be found her for API documentation for DataWriter .

Which method do I use, and why? How can I convert this class and sent it to my server using the methods mentioned above.

    public class Message
    {
       public string pas { get; internal set; }
       public int type { get; internal set; }
       public string us { get; internal set; }#
    }

    //the below code goes in a seperate function 
    DataWriter dw = new DataWriter(clientSocket.OutputStream);
    Message ms = new Message();
    ms.type = 1;
    ms.us = usernameTextBox.Text;
    ms.pas = usernameTextBox.Text;
    //TODO: send ms to the server
1

There are 1 best solutions below

9
On BEST ANSWER

Between the two methods, WriteBytes() seems like the simpler approach. WriteBuffer() offers you more control over the output buffer/stream, which you can certainly use if and when you need to. But, for all intents and purposes, if you just need to simply open a connection and send it a byte stream, WriteBytes() does the job.

How can I convert this class and sent it to my server

That's entirely up to you, really. What you have to do is define how you're going to "serialize" your class to transmit over the connection (and thereby have to "deserialize" it when the other code receives the data).

There are a few ways to do that, among many others. A straightforward approach (taken from the top answer on that linked question), would be to use the BinaryFormatter class. Something like this:

var ms = new Message();
ms.type = 1;
ms.us = usernameTextBox.Text;
ms.pas = usernameTextBox.Text;

byte[] serializedMessage;
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
    formatter.Serialize(stream, ms);
    serializedMessage = ms.ToArray();
}

// now serializedMessage is a byte array to be sent

Then on the other end you'd need to deserialize it back to an object instance. Which might look something like this:

// assuming you have a variable called serializedMessage as the byte array received

Message ms;
using (var stream = new MemoryStream())
{
    var formatter = new BinaryFormatter();
    stream.Write(serializedMessage, 0, serializedMessage.Length);
    stream.Seek(0, SeekOrigin.Begin);
    ms = (Message)formatter.Deserialize(stream);
}

You can of course abstract these behind a simpler interface. Or if you're looking for any kind of human readability in the serialization you might try something like a JSON serializer and directly convert the string to a byte array, etc.


Edit: Note that this is really just an example of one of many ways to "serialize" an object. And, as pointed out by a user in comments below, there could be drawbacks to using this binary serializer.

You can use any serializer, really. You can even make your own. Technically overriding .ToString() to print all the properties and then calling that is a form of serialization. The concept is always the same... Convert the in-memory object to a transmittable piece of data, from which an identical in-memory object can later be built. (Technically, saving to a database is a form of serialization.)