c# - Serialisation error "The input stream is not a valid binary format" -


i'm writing program needs send data client server on tcp socket.

i have following code serialises , desirialises object.

public static byte[] serializeobjects(object obj)     {         using (memorystream ms = new memorystream()) {             iformatter bf = new binaryformatter();             bf.serialize(ms, obj);             byte[] bytes = ms.toarray();             return bytes;         }          }   public static object deserialiseobject(byte[] bytes)     {         using (memorystream ms = new memorystream()) {             iformatter bf = new binaryformatter();             ms.write(bytes, 0, bytes.length);             ms.seek(0, seekorigin.begin);             var obj = bf.deserialize(ms);             return obj;         }     } 

the code sends the serialised data server client.

while ((userinput = console.readline()) != null)             {                 byte[] serialisedobject;                 serialisedobject = serializeobjects(userinput);                 byte[] userdatabytes = bitconverter.getbytes((int32)serialisedobject.length);                 _writer.write(userdatabytes);                 _writer.write(serialisedobject);                 _writer.flush(); 

and code receives serialised data stream.

networkstream stream = new networkstream(socket, true);             streamwriter writer = new streamwriter(stream, encoding.utf8);             byte[] readmsgleng = new byte[4];             stream.read(readmsgleng, 0, 4);              int datalength = bitconverter.toint32(readmsgleng, 0);             byte[] messagedata = new byte[datalength];             stream.read(messagedata, 0, datalength);             returnedobject = deserialiseobject(messagedata); 

the error occuring follows "the input stream not valid binary format. starting contents (in bytes) are:......"

not sure is causing error.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -