0
Answer

Exception During "Deserialize"

M W

M W

17y
1.8k
1
I'm trying to Serialize a class object that contains an Image and send it over the network to a server. I'm able to get Serialize AND Deserialize to work just fine in the same procedure, as a test. But, I cant get it to work over Sockets. I think it has to do with the fact that the Clients MemoryStream Size is bigger than the what the Server's byte array can handle. Not sure how to fix this! ANy ideas?? Here's the exception I get: Error..... at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) Here's my code: *********** Client **************************** IFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); formatter.Serialize(ms, imageToSend); ms.Seek(0, 0); byte[] data = ms.ToArray(); //Add Networking stuff Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), SendingPort); clientSocket.Connect(remoteEndPoint); BinaryFormatter format = new BinaryFormatter(); MemoryStream str = new MemoryStream(data); str.Position = 0; str.Close(); clientSocket.Send(data); ms.Close(); clientSocket.Close(); *************** Server ***************** IPAddress ipAd = IPAddress.Parse("127.0.0.1"); tcpListener = new TcpListener(ipAd, ListenPort); tcpListener.Start(); Socket fromClient = tcpListener.AcceptSocket(); byte[] data = new byte[fromClient.ReceiveBufferSize]; while (true) { int k = fromClient.Receive(data); //Deserialize BinaryFormatter format = new BinaryFormatter(); MemoryStream str = new MemoryStream(data); str.Position = 0; //CRASHES HERE!!! EictImageTransmissionData in_obj = (EictImageTransmissionData)format.Deserialize(str); in_obj.getImage().Save("C://TEMP//RECEIVER//" + in_obj.getImageData().getFileName(), ImageFormat.Jpeg); str.Close(); }