473,587 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP! - .NET sequences segmented packets before data available in NetworkStream?

Hi,

I am confused with how NetworkStream works.

My application needs to handle heavy requests sent through TCP socket
connection. I use NetworkStream.R ead method to get the stream data. The
data are sent from the client servers, black boxes for my project team. But
we have an agreement to the client vendor that we exchange only fixed-length
data, say, 200 bytes per data set. A data set is a bombination of data
fields, like 56 bytes of character, followed by 4 bytes of integer, followed
by 2 bytes of characters, and so on.

I notice that the lower layer of TCP splits some packets into smaller
packets, as expected. I assumed before stream data are loaded into
NetworkStream, the smaller packets will be combined together by their
sequence number, so programmers don't need to worry about the segmentation
things of TCP stream socket. However, it seems the smaller packets are put
into the NetworkStream as soon as they are received. This causes disaster
because the only way we retrieve data sets is by the fixed length. If an
incompleted data set is read from NetworkStream, the following data sets
cannot be understood by our application.

Is it true that .NET doesn't sequence the smaller packets before they are
loaded into NetworkStream? Or do i miss something? Losts of sample codes
available on MSND and Google search, but can't find any article discussing
about this.

What could I do to sovle this problem?

Any suggestion or hint is highly appreciated!



Ryan


Nov 20 '05 #1
6 3316
"Ryan" <ry**@cradle.co m.tw> wrote:
Is it true that .NET doesn't sequence the smaller packets before they are
loaded into NetworkStream?


Yes. It is called Network*Stream* because you receive a stream of bytes, there
is no 'record' or 'packet' as you are thinking of them.

In the Free Stuff folder on my website at www.abderaware.com you will find
several examples of socket programming. A couple of them use a class called
TcpPacketizer which takes care of sending and receiving records (packets) over a
socket's natural stream of bytes. You could adapt it to use NetworkStream, or
simply use the Socket class as I did in the sample projects.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 20 '05 #2
Hi, Zane,

Thank you for your reply.

I have downloaded the codes and read through TcpPacketizer class, it seems
the codes deal with simalar things that networkStream does, in asynchronous
way. But I can't find the lines dealing with fragmented packets. If the
codes have taken that into consideration, could you please show me which
block of the class handles the job related to combining fragmented packets?

If the codes don't mean to deal with that, do you have any suggestion for us
to tackle down the problem?

BTW, interesting site, i showed it to my colleages.

Ryan
"Zane Thomas [.NET/C# MVP]" <za**@abderawar e.com> wrote in message
news:3f******** *******@news.mi crosoft.com...
"Ryan" <ry**@cradle.co m.tw> wrote:
Is it true that .NET doesn't sequence the smaller packets before they are
loaded into NetworkStream?
Yes. It is called Network*Stream* because you receive a stream of bytes,

there is no 'record' or 'packet' as you are thinking of them.

In the Free Stuff folder on my website at www.abderaware.com you will find
several examples of socket programming. A couple of them use a class called TcpPacketizer which takes care of sending and receiving records (packets) over a socket's natural stream of bytes. You could adapt it to use NetworkStream, or simply use the Socket class as I did in the sample projects.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com

Nov 20 '05 #3
Ryan,
But I can't find the lines dealing with fragmented packets. If the
codes have taken that into consideration, could you please show me which
block of the class handles the job related to combining fragmented packets?


The code can be somewhat confusing because it's all asynchronous. Maybe I
should write the blocking methods too. :-)

Before a 'packet' is written by the sender a four-byte length is written out,
and then the data itself. On the receive side it goes like this:

1. An instance is created and initialized with a socket to use.

2. It invokes Socket.BeginRec eive passing OnReceiveLength as the method to
invoke when some data arrives.

3. OnReceiveLength reads upto four bytes (no more than four!), if there are not
yet four bytes goto 2 (basically, the code is inline in OnReceiveLength ).

4. Once four bytes have been received the packet length is known and the
BeginReceive is invoked again, this time with OnReceiveMessag e as the callback.

5. In OnReceiveMessag e as many bytes as possible, upto but not exceeded the
packet length from step 3, are read and saved.

6. If the packet is not yet complete BeginReceive is invoked with
OnReceiveMessag e as the callback, again. If the packet is complete the
TcpPacketizer user's callback is invoked and the code (inline again) returns to
step 2.
Blocking this would be:

1. Keep reading until you have four bytes. That's the length.
2. Keep reading until you have received a length's worth of bytes.

That's all.

--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 20 '05 #4
Zane,

I can't thank you enough for your patient explanation.

I start to believe what we are dealing with is a little bit different. We
are doing ok with retrieving the stream data of a single packet from
networkstream, since the data sender agrees to send a fixed-length data set.
However, if a data set (or a message package) is splited into two fragmented
packets, and if these two fragmented packets are separated by other
packet(s) in NETWORKSTREAM ( i have no idea if this could happen or not?!)
where we will retrive the stream data, the programmer will have a nightmare
to put them back together.
If this is not very clear to you, drop me a message. I will draw a picture
to explain that and send it to your email account.
Ryan


"Zane Thomas [.NET/C# MVP]" <za**@abderawar e.com> wrote in message
news:3f******** *******@news.mi crosoft.com...
Ryan,
But I can't find the lines dealing with fragmented packets. If the
codes have taken that into consideration, could you please show me which
block of the class handles the job related to combining fragmented
packets?
The code can be somewhat confusing because it's all asynchronous. Maybe I
should write the blocking methods too. :-)

Before a 'packet' is written by the sender a four-byte length is written out, and then the data itself. On the receive side it goes like this:

1. An instance is created and initialized with a socket to use.

2. It invokes Socket.BeginRec eive passing OnReceiveLength as the method to invoke when some data arrives.

3. OnReceiveLength reads upto four bytes (no more than four!), if there are not yet four bytes goto 2 (basically, the code is inline in OnReceiveLength ).

4. Once four bytes have been received the packet length is known and the
BeginReceive is invoked again, this time with OnReceiveMessag e as the callback.
5. In OnReceiveMessag e as many bytes as possible, upto but not exceeded the packet length from step 3, are read and saved.

6. If the packet is not yet complete BeginReceive is invoked with
OnReceiveMessag e as the callback, again. If the packet is complete the
TcpPacketizer user's callback is invoked and the code (inline again) returns to step 2.
Blocking this would be:

1. Keep reading until you have four bytes. That's the length.
2. Keep reading until you have received a length's worth of bytes.

That's all.

--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com

Nov 20 '05 #5
Ryan,
I start to believe what we are dealing with is a little bit different. We
are doing ok with retrieving the stream data of a single packet from
networkstrea m, since the data sender agrees to send a fixed-length data set.
However, if a data set (or a message package) is splited into two fragmented
packets, and if these two fragmented packets are separated by other
packet(s) in NETWORKSTREAM ( i have no idea if this could happen or not?!)
where we will retrive the stream data, the programmer will have a nightmare
to put them back together.


Ok, I think I understand the situation. The first thing you should know is that
if the sender sends you two records A and B, it is true that both A and B may
arrive either in one Receive or in multiple Receives ... but no data for B will
be Received until all the data for A has been Received. TCP takes care of that
for you.

What TCP _stream_ sockets do not do is guarantee that either A or B will be
received with one invocation of Receive. You might get all of A, and then B in
three parts, or A in two parts and B in one part. There is no predicting, it
depends upon network and socket stack conditions.

So ... if you're sending Records then you and the sender must agree on what a
record is. In the common internet protocols (smtp for instance) a record is a
string of characters followed by "crlf". But that's not a big help if you're
sending binary data. If you're sending arbitrary binary data that has no
recognizable record-start or record-end delimeters then you must precede the
binary data with a byte count. TcpPacketizer assumes the worst case and sends a
byte count before each record.

You and the data sender must cooperate in order to correctly send and receive
packets.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 20 '05 #6
Zane,

Now I am totally there.

Thank you so much. I appreciate your assistance.

Ryan
"Zane Thomas [.NET/C# MVP]" <za**@abderawar e.com> wrote in message
news:3f******** *******@news.mi crosoft.com...
Ryan,
I start to believe what we are dealing with is a little bit different. Weare doing ok with retrieving the stream data of a single packet from
networkstrea m, since the data sender agrees to send a fixed-length data set.However, if a data set (or a message package) is splited into two fragmentedpackets, and if these two fragmented packets are separated by other
packet(s) in NETWORKSTREAM ( i have no idea if this could happen or not?!)where we will retrive the stream data, the programmer will have a nightmareto put them back together.
Ok, I think I understand the situation. The first thing you should know

is that if the sender sends you two records A and B, it is true that both A and B may arrive either in one Receive or in multiple Receives ... but no data for B will be Received until all the data for A has been Received. TCP takes care of that for you.

What TCP _stream_ sockets do not do is guarantee that either A or B will be received with one invocation of Receive. You might get all of A, and then B in three parts, or A in two parts and B in one part. There is no predicting, it depends upon network and socket stack conditions.

So ... if you're sending Records then you and the sender must agree on what a record is. In the common internet protocols (smtp for instance) a record is a string of characters followed by "crlf". But that's not a big help if you're sending binary data. If you're sending arbitrary binary data that has no
recognizable record-start or record-end delimeters then you must precede the binary data with a byte count. TcpPacketizer assumes the worst case and sends a byte count before each record.

You and the data sender must cooperate in order to correctly send and receive packets.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com

Nov 20 '05 #7

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

Similar topics

9
4392
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with the microsoft HTML workshop utility, lets call it c:\path\help.chm. My question is how do you launch it from the GUI? What logic do I put behind...
4
3340
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to cmd.Cmd but I would also like to get the man-page-like help for classes and functions. Does anyone know how to do that? Thanks. Sarir
2
6442
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
4322
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program)...
6
3003
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect an html authoring tool to help you accomplish? 2. What do you expect from online help for a html authoring tool? 3. What audience do you think...
0
568
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if link doesn't work, basically here is the article: An Exploration Into Launching Context-Sensitive HTML Help with Topic IDs in VB.NET I spent this...
8
3214
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both including search the internet for help, but the help is worthless. Any ideas?
10
3347
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably the worst I ever seen. I almost cannot find anything I need, including things I
1
6120
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve default property of object Label. Click for more:...
0
2869
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application calls MS Excel, so the scenario is that I am supposed to see the Excel Menu bar, FILE EDIT VIEW INSERT ... HELP. I am able to see the menu bar, but in...
0
7918
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...
0
8340
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...
1
7967
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...
0
6621
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...
1
5713
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...
0
5392
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...
0
3840
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...
1
2353
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
0
1185
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...

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.