Q:

Receiving different(changed) data from TCP Client

I have made a simple TCP Server and Client and having trouble with  Object.GetType().AssemblyQualifiedName.ToString();
Here is some notes i have made on whats happening:
"System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" - first time string A, single line
"System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" - string B - much longer than string A, its multiline
"System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e08989" - second time string A, single line

You can see that the last one is different than the others, doesnt matter what string i send it will always be the same.
There is my client send void:
public void sendObjectToServer(object obj, Socket server)
        {
            string type = obj.GetType().AssemblyQualifiedName.ToString();
            bool isNull = false;
            byte[] objectInArray;
            if (obj == null)
                isNull = true;
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, obj);
                    objectInArray = ms.ToArray();
                }
                //if (isNull == false)
                {
                    server.Send(Encoding.UTF8.GetBytes(objectInArray.Length.ToString() + "|" + type));
                    server.Receive(new byte[1024]);
                    server.Send(objectInArray);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

And server's receive void:
public object receivedData(byte[] data)
        { //[0] - length [1] - type in string
            string[] dataToString = Encoding.UTF8.GetString(data).TrimEnd('\0').Split('|');
            try
            {
                object rtn = "";
                int length = Convert.ToInt32(dataToString[0]);
                byte[] mainObject = new byte[length];
                currentClient.Client.Send(Encoding.UTF8.GetBytes("1"));
                int r = currentClient.Client.Receive(mainObject);
                Type type = Type.GetType(dataToString[1]);
                object mainObj = ByteArrayToObject(mainObject);
                if (type == typeof(String))
                {
                    rtn = mainObj.ToString();
                }
                else if (type == typeof(Int32))
                {
                    return Convert.ToInt32(mainObj);
                }
                else if (type == typeof(Bitmap))
                {
                    return (Bitmap)mainObj;
                }
                else
                {
                    rtn = "none. Type name: " + type.AssemblyQualifiedName.ToString();
                }
                return rtn;
            }
            catch (Exception ex)
            {
                return "error" + ex.ToString();
            }
        }

I have fixed it by changing Type type = Type.GetType(dataToString[1]); to Type type = Type.GetType(dataToString[1].Replace("08989", "089"));
But i wanna know why it happened.
I have checked also on client side and client is sending me "System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" but I'm receiving "System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e08989"

A:

Bluntly, you fundamentally do not understand TCP. There should be a law that a person is not allowed to write any application that uses TCP until they can recite this sentence and explain what every word in it means: "TCP is a reliable byte-stream protocol that does not preserve application message boundaries."
You are expecting the client to get a whole message without writing any actual code to make this happen. TCP has no concept of application messages. If you need such a thing, you must define precisely what a message is, write code to send a message, and write code to receive a message. You have not done this at least with respect to the "message" that contains the type.
Often broken code will work by luck so long as the application messages are short or the exchanges are slow. But it will often fail as soon as it's placed under any kind of stress.
Before writing any code that uses TCP other than code that uses an established protocol, it's an extremely good idea to carefully document the protocol you're going to us. You can look at the documentation for protocols like HTTP, SMTP, IRC, and so on. If you don't thoroughly understand at least one existing protocol layered over TCP, you really aren't in any position to try to develop your own, and certainly not by "winging it" rather than specifying it.

