473,405 Members | 2,349 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,405 software developers and data experts.

TcpClient Socket Blocking Problems

I'm having a problem with the TcpClient that I can only conclude is
either a feature, or a complete misunderstanding of the docs on my part.

In a nutshell, I'm simply performing the following sequence with a server:

connect
write(50 bytes)
read(2002 bytes)
write(50 bytes)
read(2002 bytes)
write(50 bytes)
read(2002 bytes)
disconnect

At random times, one of the read sequences will read only 1460 bytes
instead of 2002 bytes. The reaminder, 542 bytes, just happens to fall on
a packet boundry. More on that later.

Now, for the sake of this argument, the number of times the write/read
sequence could happen per connection is unknown. Could be once; could be
20 times.

Now for some somewhat real code...

TcpClient client = new TcpClient();
client.NoDelay = true;
client.Client.Blocking = true;
client.Connect("remote_server", 3456);

for (int i = 1; i++; i <= 3) {
Byte[] outputbuffer;
NetworkStream stream;

outputbuffer = Encoding.ASCII.GetBytes("50 bytes of data...");
stream = this.GetStream();
stream.Write(outputbuffer, 0, outputbuffer.Length);

Byte[] inputbuffer;
inputbuffer = new Byte[2002];
Debug.WriteLine(stream.Read(inputbuffer, 0,inputbuffer.Length));
}
client.Close();
Now, sometimes when I run this, I get:
2002
2002
1460
Or this:
1460
2002
2002
OR this:
2002
1460
2002

Clearly, Socket.Blocking is not doing what I expect.
Sockst docs say that BLocking is on by default, so shouldn't it be doing
so, or is this a matter of the NetworkStream not doing something right?

Should I be using the Socket.Send/Receive methods instead?

At this point, I'm quite confused about the interaction of Socket vs.
NetworkStream within the TcpClient.

Thanks,
-=Chris
Jul 21 '05 #1
4 4300
Christopher H. Laco wrote:
[snip]
Now, sometimes when I run this, I get:
2002
2002
1460
Or this:
1460
2002
2002
OR this:
2002
1460
2002


I forgot the mention that in the cases where NetworkSteam.Read returns
1460 instead of the requested 2002, the full 2002 bytes have indeed been
sent by the server and seen by the network card as witnessed by a good
dose of Ethereal packet sniffing.

In those cases, the 1460 is the first of two packets, and the second
packet contains the remaining 542 bytes of data.

-=Chris
Jul 21 '05 #2
Hi,

stream.Read does not obey that all data will be received. You have to
loop Read untill it returns 0.

Try something like this:

byte[] buffer = new byte[2002];
int bytesread = 0;

while (stream.Read(buffer, bytesread, buffer.Length - bytesread) > 0);

Note, I haven't tested this exact code, but it should work, or at least
it shows you the idea.

Sunny

In article <4f****************@newssvr28.news.prodigy.com>,
me********@gmail.com says...
Christopher H. Laco wrote:
[snip]
Now, sometimes when I run this, I get:
2002
2002
1460
Or this:
1460
2002
2002
OR this:
2002
1460
2002


I forgot the mention that in the cases where NetworkSteam.Read returns
1460 instead of the requested 2002, the full 2002 bytes have indeed been
sent by the server and seen by the network card as witnessed by a good
dose of Ethereal packet sniffing.

In those cases, the 1460 is the first of two packets, and the second
packet contains the remaining 542 bytes of data.

-=Chris

Jul 21 '05 #3
Sunny wrote:
Hi,

stream.Read does not obey that all data will be received. You have to
loop Read untill it returns 0.

Try something like this:

byte[] buffer = new byte[2002];
int bytesread = 0;

while (stream.Read(buffer, bytesread, buffer.Length - bytesread) > 0);

Note, I haven't tested this exact code, but it should work, or at least
it shows you the idea.

Sunny


Yeah, I knew about the loop trick. There seems to be some dissention
about whether to loop on DataAvailable or on the int count returned from
Read. Although, I'd think the latter is safer.

I guess I'm more curious as to WHY the NetworkStream doesn't really
honor socket blocking in the sence of waiting until I have all the bytes
requested.

-=Chris
Jul 21 '05 #4
Hi,

In article <41********@summitproxy.summit.network>, me********@gmail.com
says...
I guess I'm more curious as to WHY the NetworkStream doesn't really
honor socket blocking in the sence of waiting until I have all the bytes
requested.

-=Chris


Because the network streem does not support Length property. So there is
no way to query it about the content. You just read until it ends.

in very basic terms:
The socket is the communication part only. The stream is the content.
The socket only provides access to the stream, it does not deal with its
content. So it blocks only until there is some stream, and after that if
passes that stream to you for reading.

Sunny
Jul 21 '05 #5

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

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
3
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
3
by: Adie | last post by:
I'm using TcpClient in blocking mode with NetworkStreams and would like to be able to give notification that data was sent - I presume this must be possible? But anyone know how?
3
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
1
by: Claire | last post by:
Hi Im writing an application using the above controls in blocking mode. Ive not used them before and I'm more used to asynchronous socket programming utilizing socket events. As there are no...
1
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
1
by: Anders Berg | last post by:
Hi! I'm developing a very simple chat application in VB.NET and I've stumbled into a problem. For simplicity I'm using the TcpListener and TcpClient classes. However, at one point I discovered...
4
by: Christopher H. Laco | last post by:
I'm having a problem with the TcpClient that I can only conclude is either a feature, or a complete misunderstanding of the docs on my part. In a nutshell, I'm simply performing the following...
1
by: crashed | last post by:
Hi, I am using C# in .NET 2.0 and im trying to read a stream from a socket. The code works on the first attempt but fails on subsequent attempts. It is in a multithreaded application. It seems...
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...
0
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...
0
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,...
0
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...

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.