Question

Long story short, my application has been sending raw text and graphics between a server/client using binary serilization. Wanting to push both items on the same port I tried changing to using the Soap formatter to send an object with a type flag and contained object to be cast later. I try wrapping a string in the abject and use SoapFormatter to serialize and Deserialize the object. When I try this the client throw an exception while deserializing the object: "Invalid character in the given encoding. Line 1, position 6." Is there any idea of what I am doing wrong?

The object being send across the network:

[Serializable]
public class NetContainer
{

    //this object is sent and can contain either Text or an Image file
    #region Variables

    private Flag flag; //type of object being sent
    private object media;

    #endregion Variables

    #region Constructor

    public NetContainer(Flag type, object containedObject)
    {
        flag = type;
        media = containedObject;
    }

    #endregion Constructor

    #region Properties

    public Flag Flag
    {
        get { return flag; }
        set { flag = value; }
    }

    public object Media
    {
        get { return media; }
        set { media = value; }
    }

    #endregion Properties

}
    public enum Flag
    {
        Imain,
        IProfile,
        TMain,
        ABack
    }

The Sending code:

public void SendText(string text)
    {

        //assemble package
        NetContainer package = new NetContainer(Flag.TMain, text);

        foreach (TcpClient clientInstance in ClientList)
        {
            if (clientInstance.Connected) //if connection is active
            {
                BinaryFormatter formatter = new BinaryFormatter();
                //serialize package and send
                formatter.Serialize(clientInstance.GetStream(), package);
            }
        }
    }

The receiving code that throws the exception:

private void StartReading()
    {
        while (connection.Connected == true)
        //while (true)
        {
            using (NetworkStream stream = connection.GetStream()) //use the client's connection stream
            {
                //BinaryFormatter formatter = new BinaryFormatter();
                SoapFormatter formatter = new SoapFormatter();
                while (true)
                {
                    //deserialize the package from the network stream
                    NetContainer package = (NetContainer)formatter.Deserialize(stream);

                    switch (package.Flag)
                    {
                        case Flag.TMain:
                            rtbMain.Text += (string)package.Media;
                            break;
                        case Flag.Imain:
                            pbxMain.Image = (Image)package.Media;
                            break;
                        default:
                            Console.WriteLine("Unkown Flag error: " + package.Flag);
                            break;

                    }
                }
            }
        }
    }
Was it helpful?

Solution

Issue resolved. I somehow left the line:

BinaryFormatter formatter = new BinaryFormatter();

which needed to be:

SoapFormatter formatter = new SoapFormatter();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top