473,721 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IOException thrown by BinaryFormatter .Deserialize

Hi,

When I'm doing BinaryFormatter .Deserialize() over a TCP socket. When I'm
closing the TcpListener by invoking the TcpListener.Sto p(); I get:

System.IO.IOExc eption with message "Unable to read data from the transport
connection." that InnerException of type System.Net.Sock ets.SocketExcep tion
saying "An established connection was aborted by the software in your host
machine".

The code I'm using is (it's not the complete code because the complete code
is very long and threaded):
-------------------------------------------------------------
string serverIP = "some IP address";
int port = 3001;

TcpListener m_server;
m_server = new TcpListener( System.Net.IPAd dress.Parse(ser verIP), port );

Socket m_socket = m_server.Accept Socket();

System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter =
new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();

while( true )
{
NetworkStream networkStream = new NetworkStream(m _socket);
MyObjectType obj = formatter.Deser ialize(networkS tream) as MyObjectType;
// throw the IOException when the TcpListener is closed by TcpListener.Sto p();
}
-------------------------------------------------------------

Can anybody tell me how to avoid this exception?
---------
Thanks
Sharon
Nov 17 '05 #1
19 6239
Hi,

Must probably it's cause the networkstream ends without further notice (
probably cause the client sent all the data and closed the connection ) .

Try to do this, copy the networkstream to a memorystream, later deserialize
from that memory stream.

Do you by any chance send the size first?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sharon" <Sh****@discuss ions.microsoft. com> wrote in message
news:14******** *************** ***********@mic rosoft.com...
Hi,

When I'm doing BinaryFormatter .Deserialize() over a TCP socket. When I'm
closing the TcpListener by invoking the TcpListener.Sto p(); I get:

System.IO.IOExc eption with message "Unable to read data from the
transport
connection." that InnerException of type
System.Net.Sock ets.SocketExcep tion
saying "An established connection was aborted by the software in your host
machine".

The code I'm using is (it's not the complete code because the complete
code
is very long and threaded):
-------------------------------------------------------------
string serverIP = "some IP address";
int port = 3001;

TcpListener m_server;
m_server = new TcpListener( System.Net.IPAd dress.Parse(ser verIP), port );

Socket m_socket = m_server.Accept Socket();

System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter =
new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();

while( true )
{
NetworkStream networkStream = new NetworkStream(m _socket);
MyObjectType obj = formatter.Deser ialize(networkS tream) as
MyObjectType;
// throw the IOException when the TcpListener is closed by
TcpListener.Sto p();
}
-------------------------------------------------------------

Can anybody tell me how to avoid this exception?
---------
Thanks
Sharon

Nov 17 '05 #2
Thanks Ignacio,

The client (the side that does the Serialize) is not closing the socket.
The IOException is thrown when I'm doing the stop on the server side (the
side that does the Deserialize).

The while loop is already Deserialized 30 MyObjectType objects, and this all
the objects that has been Serialized to the socket on the other side.

Can you post a sample code explaining what you want me to check?

No, I'm not sending any size first, I simply doing this:

string serverIP = "The server IP";
int port = 3001;
Socket m_socket = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
System.Net.IPEn dPoint ep = new
System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(serverIP ), port);
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter =
new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
NetworkStream networkStream = new NetworkStream(m _socket);
foreach( MyObjectType obj in myObjectTypeLis t )
{
formatter.Seria lize(networkStr eam, obj);
}
// waiting...
Any idea?

-----
Thanks
Sharon
Nov 17 '05 #3
Hi,

I had a similar situation, the problem it seems that the client send the
data and just close the connection, sometimes even without sending all the
data, ( there is a property of TcpClient.Linge rState that control this) and
the server side was reporting the same error you are seeing.

Do this test, use a memorystream to read the data , use
NetworkStream.D ataAvailable , see the example in the help, or you could just
read a byte at a time:

MemoryStream mem = new MemoryStream()
int b=-1;
while( (b=networkstrea m.ReadByte()) != -1 )
mem.WriteByte( (byte)b);
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sharon" <Sh****@discuss ions.microsoft. com> wrote in message
news:3B******** *************** ***********@mic rosoft.com...
Thanks Ignacio,

The client (the side that does the Serialize) is not closing the socket.
The IOException is thrown when I'm doing the stop on the server side (the
side that does the Deserialize).

The while loop is already Deserialized 30 MyObjectType objects, and this
all
the objects that has been Serialized to the socket on the other side.

Can you post a sample code explaining what you want me to check?

No, I'm not sending any size first, I simply doing this:

string serverIP = "The server IP";
int port = 3001;
Socket m_socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am,
ProtocolType.Tc p);
System.Net.IPEn dPoint ep = new
System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(serverIP ), port);
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter =
new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
NetworkStream networkStream = new NetworkStream(m _socket);
foreach( MyObjectType obj in myObjectTypeLis t )
{
formatter.Seria lize(networkStr eam, obj);
}
// waiting...
Any idea?

-----
Thanks
Sharon

Nov 17 '05 #4
Hi,

An extra question, you said that it already had 30 objects deserialized ,
how many are you sending? 31?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sharon" <Sh****@discuss ions.microsoft. com> wrote in message
news:3B******** *************** ***********@mic rosoft.com...
Thanks Ignacio,

The client (the side that does the Serialize) is not closing the socket.
The IOException is thrown when I'm doing the stop on the server side (the
side that does the Deserialize).

The while loop is already Deserialized 30 MyObjectType objects, and this
all
the objects that has been Serialized to the socket on the other side.

Can you post a sample code explaining what you want me to check?

No, I'm not sending any size first, I simply doing this:

string serverIP = "The server IP";
int port = 3001;
Socket m_socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am,
ProtocolType.Tc p);
System.Net.IPEn dPoint ep = new
System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(serverIP ), port);
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter =
new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
NetworkStream networkStream = new NetworkStream(m _socket);
foreach( MyObjectType obj in myObjectTypeLis t )
{
formatter.Seria lize(networkStr eam, obj);
}
// waiting...
Any idea?

-----
Thanks
Sharon

Nov 17 '05 #5
Hi Ignacio,

That's exactly the point, I'm Serialize 30 objects and getting them all at
the deserializing side.
So all the data was sent successfully.
But still I get this exception when closing the TcpListener.

What do you think.

--------
Thanks
Sharon
Nov 17 '05 #6
Hi Sharon,

I have a similar problem

DO this, Check the InnerException to see what it contains, if so what is
the ErrorCode you are getting?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sharon" <Sh****@discuss ions.microsoft. com> wrote in message
news:0D******** *************** ***********@mic rosoft.com...
Hi Ignacio,

That's exactly the point, I'm Serialize 30 objects and getting them all at
the deserializing side.
So all the data was sent successfully.
But still I get this exception when closing the TcpListener.

What do you think.

--------
Thanks
Sharon

Nov 17 '05 #7
Hi Ignacio,

Ok,
The top exception is System.IO.IOExc eption {"Unable to read data from the
transport connection."}, and it contains an inner exception
System.Net.Sock ets.SocketExcep tion {"An established connection was aborted by
the software in your host machine"}

NativeErrorCode = 10053

Stack trace:

System.Net.Sock ets.NetworkStre am.Read(Byte[] buffer, Int32 offset, Int32 size)
System.IO.Strea m.ReadByte()
at System.IO.Binar yReader.FillBuf fer(Int32 numBytes)
System.IO.Binar yReader.ReadByt e()
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadByte(
System.Runtime. Serialization.F ormatters.Binar y.Serialization HeaderRecord.Re ad(__BinaryPars er input
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadSerializa tionHeaderRecor d()
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.Run()
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. Deserialize(Hea derHandler
handler, __BinaryParser serParser, Boolean fCheck, IMethodCallMess age
methodCallMessa ge)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream
serializationSt ream, HeaderHandler handler, Boolean fCheck,
IMethodCallMess age methodCallMessa ge
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream serializationSt ream, HeaderHandler handler
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream serializationSt ream)
I hope it gives you what you need.
---------------
Thanks again
Sharon
Nov 17 '05 #8

Hi Sharon,

Seems there are something else cause the formatter to read further data from
the NetworkStream after you've close the Tcp connection. Have you tried using
NetworkStream.c lose() method to close both the stream and the underlying Tcp
connection? Also, I think Ignacio's suggestion on using a memory stream to
temporarly hold the binary data is also a considerable means for testing.

Please feel free to post here if you got any further finding.

Thanks,

Steven Cheng

"Sharon" wrote:
Hi Ignacio,

Ok,
The top exception is System.IO.IOExc eption {"Unable to read data from the
transport connection."}, and it contains an inner exception
System.Net.Sock ets.SocketExcep tion {"An established connection was aborted by
the software in your host machine"}

NativeErrorCode = 10053

Stack trace:

System.Net.Sock ets.NetworkStre am.Read(Byte[] buffer, Int32 offset, Int32 size)
System.IO.Strea m.ReadByte()
at System.IO.Binar yReader.FillBuf fer(Int32 numBytes)
System.IO.Binar yReader.ReadByt e()
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadByte()
System.Runtime. Serialization.F ormatters.Binar y.Serialization HeaderRecord.Re ad(__BinaryPars er input)
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadSerializa tionHeaderRecor d()
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.Run()
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. Deserialize(Hea derHandler
handler, __BinaryParser serParser, Boolean fCheck, IMethodCallMess age
methodCallMessa ge)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream
serializationSt ream, HeaderHandler handler, Boolean fCheck,
IMethodCallMess age methodCallMessa ge)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream serializationSt ream, HeaderHandler handler)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream serializationSt ream)
I hope it gives you what you need.
---------------
Thanks again
Sharon

Nov 17 '05 #9

Hi Sharon,

Seems there are something else cause the formatter to read further data from
the NetworkStream after you've close the Tcp connection. Have you tried using
NetworkStream.c lose() method to close both the stream and the underlying Tcp
connection? Also, I think Ignacio's suggestion on using a memory stream to
temporarly hold the binary data is also a considerable means for testing.

Please feel free to post here if you got any further finding.

Thanks,

Steven Cheng

"Sharon" wrote:
Hi Ignacio,

Ok,
The top exception is System.IO.IOExc eption {"Unable to read data from the
transport connection."}, and it contains an inner exception
System.Net.Sock ets.SocketExcep tion {"An established connection was aborted by
the software in your host machine"}

NativeErrorCode = 10053

Stack trace:

System.Net.Sock ets.NetworkStre am.Read(Byte[] buffer, Int32 offset, Int32 size)
System.IO.Strea m.ReadByte()
at System.IO.Binar yReader.FillBuf fer(Int32 numBytes)
System.IO.Binar yReader.ReadByt e()
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadByte()
System.Runtime. Serialization.F ormatters.Binar y.Serialization HeaderRecord.Re ad(__BinaryPars er input)
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadSerializa tionHeaderRecor d()
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.Run()
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. Deserialize(Hea derHandler
handler, __BinaryParser serParser, Boolean fCheck, IMethodCallMess age
methodCallMessa ge)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream
serializationSt ream, HeaderHandler handler, Boolean fCheck,
IMethodCallMess age methodCallMessa ge)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream serializationSt ream, HeaderHandler handler)
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( Stream serializationSt ream)
I hope it gives you what you need.
---------------
Thanks again
Sharon

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3807
by: Dominic | last post by:
Hi everybody, In my application, I'm planning to use BinaryFormatter to serialize a potentially huge object to file (and, of course, deserialize back to memory later). My question is if there is any hard limit on the size of this object? Is it only limited by the amount of memory or hard-disk space in the server?
0
1055
by: aladdinm1 | last post by:
Hi All, Reference to the problem I posted with subject "BinaryFormatter.Deserialize fails when used with .net ActiveX". I could successfully solve the problem by creating a class inherited from SerializationBinder and setting the Binder property of the BinaryFormatter to an instance of that class. The code used was just one line to return the type used to serialize data. I got this information by help of the following Url: ...
3
5479
by: Joshua Moore | last post by:
I have a webservice that serializes a ton of variables and other good stuff to a txt file using SoapFormatter (IFormatter), and when I try to deserialize it using the binary formatter, i get the following error: BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1986938170.1886350437. I've read a lot about this error from several sources on the net, but can't
0
1768
by: Fred Heida | last post by:
Hi Al, i have a funny problem.. i you can call it funny.. what i have is 2 assemblies, the first one does nothing other then Application.Run(new MyForm())
11
3953
by: Igor | last post by:
Hi. While executing BinaryFormatter.Deserialize() I get: System.InvalidCastException: Specified cast is not valid. I implemented ISerializable interface. What may be a problem? Thanks.
2
2730
by: Henrik Skak Pedersen | last post by:
Hello, I have a class which is beeing serialized/deserialized using the BinaryFormatter class. That has been working with no problems until I signed all my assemblies. Now I get a SerializationException when I try to Deserialize the class which has been serialized before the signing. So what is best practice in my situation? Do I have to convert all my saved classes or do I write a SerializationBinder to handle it runtime?
0
2075
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the application implementation in another .dll (let's call that app.dll). app.dll makes a function call into another dll (let's call that one dep.dll). dep.dll has some code in it where it deserializes a memorystream using a binaryformatter.
2
5818
by: Doug Lind | last post by:
Hi all, I have seen a number of posts re: the BinaryFormatter version incompatibility but nothing on how to recover from it. In my case, I want the exception to trigger an alternate behaviour and continue process the stream. My app reads and writes configuration settings to a binary file upon starting and shutdown. The settings are stored in various objects that are serialized and deserialized using a BinaryFormatter. When reading...
11
3121
by: JZ | last post by:
Hi, I'm using a class and binary formatter to store data in files. For example.. Dim FPs As New StuctureDataFile() Dim FileStream As Stream = File.Open(pfile, FileMode.Open) Dim FileFormatter As New BinaryFormatter() FPs = DirectCast(FileFormatter.Deserialize(FileStream), StuctureDataFile)
0
8858
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9148
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5994
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.