0

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
Moynul
  • 635
  • 1
  • 8
  • 30

1 Answers1

2

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.)

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • thank you for the quick answer. It appears Windows Phone uses the JSON API which gives `DataContractJsonSerializer`. So I have used the `WriteObject(stream,ms)` . Doing so I used the `ms.ToArray()` method to store the bytes. However in the server end, which is a windows console application I am getting Additional information: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization. ` ms = (Message)formatter.Deserialize(stream);` – Moynul Feb 02 '16 at 14:42
  • @Moynul: Is the object being serialized as expected and sent over the connection? Is the stream received on the server the same length as the stream sent from the client? It sounds like some direct debugging needs to be done here, which I unfortunately can't do for you. (You are using the *same* method for de-serialization that you use for serialization, correct? That part is important.) – David Feb 02 '16 at 14:44
  • Forgive me for my naive/lack of knowledge as I am still new to socket programming. From debugging on the client, it appears that `serializedMessage` is null. So my implementation of the serialization using `DataContractJsonSerializer` failed. On the server side `serializedMessage = new byte[client.ReceiveBufferSize];` returns null on the debug output. – Moynul Feb 02 '16 at 14:52
  • 2
    @David I would advice against using `BinaryFormatter` for any data that is persisted or transmitted between machines. `BinaryFormatter` is extremely intolerant of assembly version number differences between serialization and de-serialization. Just having a different number of windows updates on the two sides can cause it to fail. I would recommend using almost any other serializer. The only thing I recommend using `BinaryFormatter` for is for unpersisted IPC on the same machine (which, for that specific use case, it works pretty good). – Scott Chamberlain Feb 02 '16 at 14:53
  • @Moynul: In that case you'll need to focus on the client-side first. If it's null then the server isn't receiving any data from it. You might also try a different JSON serializer, such as JSON.NET (by Newtonsoft), which is easily available as a NuGet package. It serializes to and from strings *very* easily, and converting between strings and byte arrays can be pretty trivial (just make sure to use the same character encoding on the client and server). – David Feb 02 '16 at 14:55
  • @ScottChamberlain: I wasn't aware of that. Good to know, thanks! – David Feb 02 '16 at 14:56
  • @David I've just installed the Json.NET, and currently looking at the documentation. I will use the same character encoding. If I come across any troubles, please do not mind if I contact you via this comment section. – Moynul Feb 02 '16 at 15:04
  • @Moynul: I hope it works out. Though keep in mind that if you have further questions which aren't duplicates of this one, asking a new question to the community may get better responses than just directly asking me :) – David Feb 02 '16 at 15:05
  • @David I may just do that, great example code by the way. I'm sure I can use it in my future windows projects. Many thanks. – Moynul Feb 02 '16 at 15:08