Question

So i am trying to create something like a syncronized paint program by using sockets.I have a server side..and a client side and i am trying to send the inkCollection from the server to the client.This works for simple text, but i cant seem to send the inkCollection.Or it would be even cooler if you could help me send the last stroke so that the data transfers faster.Here is some code of what i've been trying:

sending strokes:

        byte[] data;
        using (MemoryStream ms = new MemoryStream())
        {
            inkcanvas.Strokes.Save(ms);
            data = ms.ToArray();
        }

        svr.SendToAll("u getting some strokes");
        svr.SendStrokes(data);

svr.SendStrokes(byte[] data):

 public void SendStrokes(byte[] data)
    {
        for (int i = 0; i < no; i++)
        {
            byte[] dt = data;
            accepted[i].Send(dt);
        }

            MessageBox.Show("dONE");
    }

and this is on the clientside:

 byte[] buffer=new byte[1024];
                MessageBox.Show("receiving strokes");
                int rec = conn.Receive(buffer, 0, buffer.Length, 0);
                if (rec <= 0)
                    throw new SocketException();
                MessageBox.Show("strokes received");
                //Array.Resize(ref buffer, rec);
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    inkcanvas.Strokes = new System.Windows.Ink.StrokeCollection(ms);
                    ms.Close();
                }
                MessageBox.Show("strokes added");

these exact same methods work perfectly for string, but when i try to do it with the strokeCollection, it fails.Nothing shows up on the client and i get the following SocketException ont the serverside: An existing connection was forcibly closed by the remote host.

But if you guys got a better way on how to do this it would be great...is it something i am missing? i mean..if it works for text transformed into a byte array...why wouldint it work for this strokecollection?

Thanks!

EDIT: You think you could help me out with some sample code? i cant really seem to implement it;

Was it helpful?

Solution

You forgot to either design or implement a protocol! You can't just send a bunch of bytes over TCP and assume the receiver will be able to make sense out of it.

You have an application message that consists of a bunch of strokes which you are trying to send over TCP. But TCP is a byte stream service, not an application message service. So you need some kind of application message protocol to package the message for transport and unpackage it on receipt. But you have not written any such code. So you're basically expecting it to work by magic.

Define a protocol. For example, it might say:

Canvas strokes shall be sent by a '1' byte indicating canvas strokes, followed by 4 bytes indicating the number of bytes in the strokes object in network byte order, followed by the stroke data. The receiver will read the first byte and identify that it's a canvas strokes object. Then the receiver will read the next four bytes to determine the length. The receiver shall accumulate that number of bytes (using multiple reads if necessary) and then process the reconstructed canvas strokes object.

Do not skip the step of creating a written protocol definition.

Then, when you have problems, follow this handy troubleshooting guide:

  1. Does the sender follow the specification? If not, stop, the sender is broken.

  2. Does the receiver follow the specification? If not, stop, the receiver is broken.

  3. Stop, the specification is broken.

If you want simple, you can convert the data into base64 and encode each message as a line of text. That will allow you to use a ReadLine function to grab exactly one message. Then you can just use a message format like an "S" (for "strokes") followed by the data in base64 format. Use a WriteLine function to send the text message followed by a newline.

OTHER TIPS

i think you forgot to set the memorystream position. you should set the memory stream position to 0 before you send out the stream, cause after strokes.save, the position of the stream is at the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top