473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

NetworkStream.Read - Thread freezes

hello out there,

I use a thread data from a linuxserver. i ask for data, i read the
data (fix in size) - so far so good. this works great for the first 61
datapackages, then my thread abruptly freezes, because of the read
method. i can't locate the real reason. if I reconnect the tcpClient
after each package it works fine, but after several minutes the linux
programm quits (not my fault, not my code, not to change). has anybody
any clues about reason why this happens?

i just want to make a robust code. i thought about another thread
which works as watchdog - dataloss is no problem in this case. is
there a approved design pattern (snipplets) for this task?

thanks
rené

<code>
public byte[] getPackage(int PackageSize)
{
int SumBytes = 0;

byte[] PackageData= new byte[PackageSize];

NetworkStream myStream = getStream(...); //nonrelevant

while ( true )
{
if (myStream.DataAvailable)
{
SumBytes += myStream.Read(PackageData, SumBytes, PackageSize-
SumBytes);

if (SumBytes >= PackageSize)
{
break;
}
}
}

return PackageData;
}
</code>

Aug 14 '07 #1
10 9089
On Aug 14, 7:35 am, ohmmega <sho...@gmx.atwrote:
hello out there,

I use a thread data from a linuxserver. i ask for data, i read the
data (fix in size) - so far so good. this works great for the first 61
datapackages, then my thread abruptly freezes, because of the read
method. i can't locate the real reason. if I reconnect the tcpClient
after each package it works fine, but after several minutes the linux
programm quits (not my fault, not my code, not to change). has anybody
any clues about reason why this happens?
Have you used a packet sniffer (eg wireshark) to determine whether any
more data has actually been sent?

Jon

Aug 14 '07 #2
On 14 Aug., 09:00, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 14, 7:35 am, ohmmega <sho...@gmx.atwrote:
hello out there,
I use a thread data from a linuxserver. i ask for data, i read the
data (fix in size) - so far so good. this works great for the first 61
datapackages, then my thread abruptly freezes, because of the read
method. i can't locate the real reason. if I reconnect the tcpClient
after each package it works fine, but after several minutes the linux
programm quits (not my fault, not my code, not to change). has anybody
any clues about reason why this happens?

Have you used a packet sniffer (eg wireshark) to determine whether any
more data has actually been sent?

Jon
not yet, because i've a server log, which tells me that the server
acknowledges my request (port A) and so i start listening on port B.
the problem seems to be, that the server don't transmit sometimes.

furthermore i thought that the use of networkStream.dataAvailable
should prevent from such deadlocks (the thread don't loop forever -
i've implemented a timeout, which isn't listet in the snipplet above.
it's definitly hangs up the read method).

Aug 14 '07 #3

On Aug 14, 9:49 am, ohmmega <sho...@gmx.atwrote:
furthermore i thought that the use of networkStream.dataAvailable
should prevent from such deadlocks [...]
Technically, it's a blocking call, not a deadlock, but you're right;
NetworkStream.Read() should return immediately if DataAvailable (which
is why it doesn't necessarily read as many bytes as you wanted). Are
you sure the parameters to Read() are all valid, and that nobody else
is using the stream?

Aug 14 '07 #4
On 14 Aug., 10:00, UL-Tomten <tom...@gmail.comwrote:
On Aug 14, 9:49 am, ohmmega <sho...@gmx.atwrote:
furthermore i thought that the use of networkStream.dataAvailable
should prevent from such deadlocks [...]

Technically, it's a blocking call, not a deadlock, but you're right;
NetworkStream.Read() should return immediately if DataAvailable (which
is why it doesn't necessarily read as many bytes as you wanted). Are
you sure the parameters to Read() are all valid, and that nobody else
is using the stream?
concerning the parameters: i've just tried out the code from the msdn.
they are using a StringBuilder instead of an byte array - ok.
since i use a byte array, i can see no error in my code (anybody
else?).

refering to the streamaccess: i'm definitly the only one, who's using
this stream on that port - but: it's running over the companynetwork
with serveral switches in beetween. i think the problem occoured only
after accessing over network - direct connection worked that far. but
i will try it again to make this sure.

can you tell me why my code blocks, even after checking for available
data?

Aug 14 '07 #5

On Aug 14, 2:42 pm, ohmmega <sho...@gmx.atwrote:
concerning the parameters: i've just tried out the code from the msdn.
they are using a StringBuilder instead of an byte array - ok.
Well, myReadBuffer in the MSDN example is a byte[1024]; the
StringBuffer is not relevant to the NetworkStream.Read() call.

Things to try:

1. Try the code reading from a local port on your own machine.
2. To make sure no other part of your code is somehow tampering with
your Socket/NetworkStream, make a new test (perhaps using the Snippet
Compiler) using only what's needed to actually test the Read() against
the server.
3. Try outside the debugger.
4. Run the code on a different machine.

Aug 14 '07 #6
On 14 Aug., 15:20, UL-Tomten <tom...@gmail.comwrote:
On Aug 14, 2:42 pm, ohmmega <sho...@gmx.atwrote:
concerning the parameters: i've just tried out the code from the msdn.
they are using a StringBuilder instead of an byte array - ok.

Well, myReadBuffer in the MSDN example is a byte[1024]; the
StringBuffer is not relevant to the NetworkStream.Read() call.

Things to try:

1. Try the code reading from a local port on your own machine.
2. To make sure no other part of your code is somehow tampering with
your Socket/NetworkStream, make a new test (perhaps using the Snippet
Compiler) using only what's needed to actually test the Read() against
the server.
3. Try outside the debugger.
4. Run the code on a different machine.
i've just tested the direct communication and to my frustration the
system hangs up.
concerning your comments:
1. you mean writing my own server in c#, arn't you?
2. this overrates myself: what do you mean with snippet compiler? i've
only access to visual studio 2003. if there is such a compiler or
compile option let me know.
3. release? also game over.
4. same problem.

i also tried wireshark, but it swampped me a little, so i couldn't get
any usefull clues.

any other usefull hints?
thanks so long.

Aug 14 '07 #7

On Aug 14, 3:52 pm, ohmmega <sho...@gmx.atwrote:
i've just tested the direct communication [...]
What's "direct communication"?
1. you mean writing my own server in c#, arn't you?
Yeah, anything that behaves like a simple server, so you can test the
Read(). What the server does is not important. Or just Read() from an
existing server, since you don't care about the results: connect to
the IIS, Send() a HTTP request (or something that will make IIS
respond with an error message) and Read() some bytes. If that blocks
too, then the problem is not the server.
2. this overrates myself: what do you mean with snippet compiler?
Snippet Compiler compiles snippets. It's a mini-environment for
testing code. http://www.sliver.com/dotnet/SnippetCompiler/

Aug 14 '07 #8
On 14 Aug., 16:35, UL-Tomten <tom...@gmail.comwrote:
On Aug 14, 3:52 pm, ohmmega <sho...@gmx.atwrote:
i've just tested the direct communication [...]

What's "direct communication"?
1. you mean writing my own server in c#, arn't you?

Yeah, anything that behaves like a simple server, so you can test the
Read(). What the server does is not important. Or just Read() from an
existing server, since you don't care about the results: connect to
the IIS, Send() a HTTP request (or something that will make IIS
respond with an error message) and Read() some bytes. If that blocks
too, then the problem is not the server.
2. this overrates myself: what do you mean with snippet compiler?

Snippet Compiler compiles snippets. It's a mini-environment for
testing code.http://www.sliver.com/dotnet/SnippetCompiler/
with direct connection i mean: pc - cable - device (no switches, no
routers)

maybe i write a server tomorrow, today i'm just ***** ò¿ó

snippet: ah ja.

my boss says that the server sometimes makes problems if there is no
one on there to catch requested packages. that's strange, because i'm
always waiting for the whole package. damn.

Aug 14 '07 #9
ohmmega wrote:
[...]
can you tell me why my code blocks, even after checking for available
data?
I don't know the specifics of how the DataAvailable property in .NET is
implemented. However, assuming it uses the most obvious mechanism --
receiving with the MSG_PEEK flag -- it is not guaranteed to avoid a
blocking receive.

All that calling recv(..., MSG_PEEK) does is give you an instantaneous
observation of the state of the socket. That state can in fact change
the moment you return from checking the state, and if for whatever
reason the buffered data has to be discarded before you actually try to
receive the data, the attempt to receive will block.

Now, whether that is affecting what's going on here or not is hard to
say. Generally, as long as the algorithm does not actually rely on the
receive not blocking, the worst that will happen is that you'll just
block until the data can be resent, and then it will go ahead and be
received.

But regardless, it's very important to understand the limitations of
MSG_PEEK, and the likelihood that these limitations apply also to the
DataAvailable property.

Pete
Aug 14 '07 #10
UL-Tomten <to****@gmail.comwrote:

<snip>
NetworkStream doesn't even behave like other streams in the framework.
Could you elaborate on this? It's always nice to know "gotchas" from
other people...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 14 '07 #11

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

Similar topics

0
by: Hai Ly Hoang \(MT00KSTN\) | last post by:
Hi, FileStream.Read(buf, offset, siz) will return when reaching the end of file or a block (size siz) can be read from the file. In the case of NetworkStream, it's more confusing. Now, consider...
0
by: Abubakar | last post by:
Hi, try { int x = ns.Read(readbuffer, 0, readbuffer.Length); } catch (System.IO.IOException ioexception) { UINotifications.ServerMessageDisplay(ioexception.ToString( )); }
4
by: Kai Thorsrud | last post by:
Hi, Thanks a lot for the short path solution to the app i'm working on by including a Perl script ( App i'm converting from perl to .Net) for the part i can't do yet. I'm communicating with a...
1
by: Steve | last post by:
I'm working on a program that talks to the Yahoo chat servers. But I have run into a small problem and I'm not sure what to do next. After I successfully log into yahoo and receive my cookies I...
4
by: DaveR | last post by:
I have a background thread to accept and process TCP/IP connections. When the application shuts down I want to gracefully terminate it. The thread will block in NetworkStream.Read() waiting for...
4
by: Cyron | last post by:
Hello Friends, When I make a call to NetworkStream.Read(byte buffer, int offset, int size) I have found that this method will block until size bytes have been read or the connection is closed. ...
1
by: Charles | last post by:
Hi, Is there a way to find out how many bytes NetworkStream Read/Write actually read/wrote when there is an exception (i.e. socket read/write timeout). Or, can I assume that if there is an...
1
by: Terry Olsen | last post by:
Need a little help with reading from a network stream. I'm downloading articles from an NNTP server. The end of an article is signified by <CRLF>.<CRLF> For all the talking back & forth, I used...
6
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.