473,394 Members | 1,774 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,394 software developers and data experts.

NetworkStream.Read blocking question

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. Is this the correct
behavior? My MSDN documentation led me to believe that this function
should block until at least 1 byte of data was available, the
connection was closed, or size bytes of data had been read.

Any comments or clarifications would be greatly appreciated.

Thanks,

Mike

Jul 19 '06 #1
4 12751
NS.Read ends up doing a:
int read = socket.Receive(buffer, offset, size, SocketFlags.None);

So its blocking behavior should be the same as that of calling
socket.Receive yourself. This will block until you get at least 1 byte. So
your not getting 1 byte after some time or maybe you have another stream
ontop of NS that is blocking for X bytes. Are you sure your getting some
data and it is still blocking?

--
William Stacey [MVP]

"Cyron" <md*******@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
| 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. Is this the correct
| behavior? My MSDN documentation led me to believe that this function
| should block until at least 1 byte of data was available, the
| connection was closed, or size bytes of data had been read.
|
| Any comments or clarifications would be greatly appreciated.
|
| Thanks,
|
| Mike
|
Jul 19 '06 #2
Cyron <md*******@yahoo.comwrote:
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.
That's certainly not *always* the case. I've seen NetworkStream return
having read fewer than "size" bytes, even when there's more data
available later. You shouldn't make assumptions as to how much data
will be returned, beyond that it will be between 1 byte and "size"
bytes, if the stream is still open and data is or becomes available.

--
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
Jul 19 '06 #3
Hi,
"Cyron" <md*******@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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. Is this the correct
behavior? My MSDN documentation led me to believe that this function
should block until at least 1 byte of data was available, the
connection was closed, or size bytes of data had been read.
You better prepare your code for not a consistent behavior. It does depend
of the lower layers of how many bytes you get. The only thing you know for
sure is that you will not get more than thesize of the buffer.

My guess is that it does post the same amount of data it got from the
network. You can see how this work by playing around with the array size &
the TCP buffer's size.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 19 '06 #4
You better prepare your code for not a consistent behavior. It does depend
of the lower layers of how many bytes you get. The only thing you know for
sure is that you will not get more than thesize of the buffer.

My guess is that it does post the same amount of data it got from the
network. You can see how this work by playing around with the array size &
the TCP buffer's size.
Thanks for the comments and suggestions everyone. I'm sorry I was a
little unclear in my initial post. I do most of my network programming
with unix sockets and was expecting NetworkStream.Read to have have
similar blocking properties -- ie. the call returns when there is at
least 1 byte of data available. It turns my C# code was correct,
however an associate had previously used this machine to develop a
driver which modified the lower level network stack. I tested the code
on 5 other vanilla Windows boxes and it works as expected.

Thanks again for your time and suggestions,

Mike

Jul 19 '06 #5

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

Similar topics

0
by: Stelrad Doulton | last post by:
Hi all, I am writting a Jabber-esque client-server application and have run into an issue with the XmlTextReader constructor when passing a NetworkStream - It hangs forever, apparently this is...
1
by: kmacintyre | last post by:
I am trying to us a simple NetworkStream to transfer a file over tcp. This works most of the time, but one specific file never downloads(.mdb file). It seems to close the socket and I get an...
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( )); }
6
by: Ryan | last post by:
Hi, I am confused with how NetworkStream works. My application needs to handle heavy requests sent through TCP socket connection. I use NetworkStream.Read method to get the stream...
3
by: g66g08d14 | last post by:
Hi. We have a Jabber-esque client server package that uses XMPP for communication over network sockets. Using .NET 2.0, I need to read a full stanza (i.e. balanced xml) as soon as available...
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...
2
by: PiotrKolodziej | last post by:
Hi I have a simple question. Here is the code related to my question: while (true) { if (tcpListenerServer.Pending() && !this.Disposing) { TcpClient tcpClient =...
5
by: | last post by:
I've got a basic "chat" infrastructure that can send objects across the wire, but I'm running into a few issues with trying to send multiple messages and things behaving differently in debug and...
10
by: ohmmega | last post by:
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.