Hi Jerry,
Based on my research, the problem seems to be relative with how you parse
the buffer and show them.
Actually TCP protocol is stream-like protocol.
We should consider it all the communication between the TCPClient is a
stream.
e.g.
if ClientA call 5 five times NetStream.Write and the TCPClient is connect
to Client B.
The Client may call 4 or 6 times NetStream.Read to get all the data sent by
Client A.
So in your scenario, we can depend on the Enqueue operation to put the
whole byte array into the queue.
Here is my test to show this scenario.
1. Please change your code as below.
static int i = 0;
private void GetStreamObject(IAsyncResult ar)
{
Debug.WriteLine(++i);
byte[] buffer;
int bytes = 0;
//Extract state object from AsyncResult and get a local pointer to the
NetworkStream.
ObjectInfo oi = (ObjectInfo)ar.AsyncState;
NetworkStream ns = oi.networkStream;
lock(ns)
{
//Read the send buffer.
bytes = ns.EndRead(ar);
//Debug.WriteLine(bytes);
//Create a local buffer the size of the received data.
buffer = new byte[bytes];
//Copy the received data into the appropriate sized buffer.
Buffer.BlockCopy(oi.buffer, 0, buffer, 0, bytes);
StreamWriter sw = new StreamWriter(@"C:\buffer.txt",true);
sw.WriteLine("*");
foreach(byte b in buffer)
sw.Write(b);
sw.WriteLine("#");
sw.Close();
//Analyze what we have.
AnalyzeBytes(buffer);
//Create a new buffer for the next read.
//Probably don't really need this.
oi.buffer = new byte[BUFFER_SIZE];
//Start another async read.
AsyncCallback GetStreamCallback = new AsyncCallback(GetStreamObject);
tcpClient.GetStream().BeginRead(oi.buffer, 0, oi.buffer.Length,
GetStreamCallback, oi);
}
}
I Start the your program and use 5 in the for loop to send 5 objects in one
button click.
The result is that.
1. The Debug.WriteLine(++i); show that the method GetStreamObject is only
called 4 times including the time we click the connect button.
2. I write the buffer to a file as attachment.
We will find that *# include the data we get from one receive.
We will find that the data in one receive *# maybe include two and half
objects' data but not whole object.
Also you may try to find the pattern "01000255" in the result below, you
will find that it occur five times exclude the first connect message.
Which means that actually we have received all the data with you reproduce
sample.
So I think the key point in your scenario is to design another method to
parse the whole buffer stream during the connected time, but not break the
stream into the items in the Queue. Because the item(byte array) may not be
a whole complete object).
Anyway your idea that use a header to show following objects size is a good
idea and that is what we will use network programming.
e.g. We must define the size of the Header which show what is the size of
the object.
Header---Date----Header----Data....
We should define the Header Size e.g. 16 bytes.
Please note the above data may not be in the four receive operations.
Maybe just one time receive, we will got all the data.
What we should do is.
Read 16bytes from the buffer we received, and know the size of the object
and then read the size to get object. And then accordind our design here,
we should read another 16bytes to read the data....
*
40100010002552552552551000000012200074771011151159 71031016710897115115443286
10111411510511111061494648465052544946514956505644 32671171081161171141016111
01011171161149710844328011798108105997510112184111 10710111061110117108108510
00176797109101108849710810746771011151159710310130 00137710111511597103101781
17109981011141377101115115971031018311611410511010 31177101115115971031018412
11121010148306797109101108849710810746771011151159 71031014377101115115971031
01841211121011152000200000006300020671111101101019 91161011003211611132115101
11411810111446525225525525530679710910110884971081 07467710111511597103101437
71011151159710310184121112101115100071189710811710 19595082000000011#
*
21100#
*
01000255255255255100000001220007477101115115971031 01671089711511544328610111
41151051111106149464846505254494651495650564432671 17108116117114101611101011
17116114971084432801179810810599751011218411110710 11106111011710810851000176
79710910110884971081074677101115115971031013000137 71011151159710310178117109
98101114137710111511597103101831161141051101031177 10111511597103101841211121
01014830679710910110884971081074677101115115971031 01437710111511597103101841
21112101115200020001000630001975252255255255306797 10910110884971081074677101
11511597103101437710111511597103101841211121011151 00071189710811710195950820
00100011211000100025525525525510000000122000747710 11151159710310167108971151
15443286101114115105111110614946484650525449465149 56505644326711710811611711
41016111010111711611497108443280117981081059975101 12184111107101110611101171
08108510001767971091011088497108107467710111511597 10310130001377101115115971
03101781171099810111413771011151159710310183116114 10511010311771011151159710
31018412111210101483067971091011088497108107467710 11151159710310143771011151
15971031018412111210111520002000100063000197525225 52552553067971091011088497
10810746771011151159710310143771011151159710310184 12111210111510007118971081
17101959508200010001121100010002552552552551000000 01220007477101115115971031
01671089711511544328610111411510511111061494648465 05254494651495650564432671
17108116117114101611101011171161149710844328011798 10810599751011218411110710
11106111011710810851000176797109101108849710810746 77101115115971031013000137
71011151159710310178117109981011141377101115115971 03101831161141051101031177
10111511597103101841211121010148306797109101108849 71081074677101115115971031
01437710111511597103101841211121011152000200010006 30001975252255255255306797
10910110884971081074677101115115971031014377101115 11597103101841211121011151
00071189710811710195950820001000112110001000255255 25525510000000122000747710
11151159710310167108971151154432861011141151051111 10614946484650525449465149
56505644326711710811611711410161110101117116114971 08443280117981081059975101
12184111107101110611101171081085100017679710910110 88497108107467710111511597
10310130001377101115115971031017811710998101114137 71011151159710310183116114
10511010311771011151159710310184121112101014830679 71091011088497108107467710
1#
*
11511597103101437710111511597103101841211121011152 00020001000630001975252255
25525530679710910110884971081074677101115115971031 01437710111511597103101841
21112101115100071189710811710195950820001000112110 00100025525525525510000000
12200074771011151159710310167108971151154432861011 14115105111110614946484650
52544946514956505644326711710811611711410161110101 11711611497108443280117981
08105997510112184111107101110611101171081085100017 67971091011088497108107467
71011151159710310130001377101115115971031017811710 99810111413771011151159710
31018311611410511010311771011151159710310184121112 10101483067971091011088497
10810746771011151159710310143771011151159710310184 12111210111520002000100063
00019752522552552553067971091011088497108107467710 11151159710310143771011151
15971031018412111210111510007118971081171019595082 000100011#
Best regards,
Peter Huang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
(This posting is provided "AS IS", with no warranties, and confers no
rights.)