473,626 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NetworkStream.R ead blocking question

Hello Friends,

When I make a call to NetworkStream.R ead(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 12781
NS.Read ends up doing a:
int read = socket.Receive( buffer, offset, size, SocketFlags.Non e);

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*******@yaho o.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
| Hello Friends,
|
| When I make a call to NetworkStream.R ead(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*******@yaho o.comwrote:
When I make a call to NetworkStream.R ead(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.co m>
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*******@yaho o.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Hello Friends,

When I make a call to NetworkStream.R ead(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.R ead 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
1678
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 an issue with SP1. Anyway I have the code below as a work around (using bool keepalives as it must run on the CF): while(keepAlive)
1
2639
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 "unable to write data to transport connection" error. I have tried multiple ways to transfer this file(including remoting, sockets without network stream) and downloaded different examples of client/server byte transfer, all with the same result....
0
1527
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
3317
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 data. The
3
4586
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 and return it as a string. So, I've taken my NetworkStream and wrapped an XmlTextReader around it. At first I tried using ReadOuterXml() to grab the full stanza. This
4
7557
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 data. I expected that shutting down the TcpListener would unblock the Read() and cause an exception, or return 0 bytes. But it does not work. Relevant parts of the code are shown below. If a client connects, the stream blocks in its Read()...
2
4095
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 = tcpListenerServer.AcceptTcpClient();
5
4214
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 release... Is there anyone out there that considers themselves a threading / sockets / serialization guru? Not sure if it's appropriate to post an entire project here and the issues discussion is probably a bit too complex for newsgroup back and...
10
9130
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 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...
0
8272
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
8713
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
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...
0
8514
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...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.