I'm having a problem with the TcpClient that I can only conclude is
either a feature, or a complete misunderstandin g 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.B locking = 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(ou tputbuffer, 0, outputbuffer.Le ngth);
Byte[] inputbuffer;
inputbuffer = new Byte[2002];
Debug.WriteLine (stream.Read(in putbuffer, 0,inputbuffer.L ength));
}
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 4 4335
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.Re ad 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
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(bu ffer, 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.co m>, me********@gmai l.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.Re ad 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
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(bu ffer, 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
Hi,
In article <41********@sum mitproxy.summit .network>, me********@gmai l.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the client machine to close per my network app the computer fills up w/
thousands of these
:0 TCP foobox:8888 localhost:2188 TIME_WAIT
:0 TCP foobox:8888 localhost:2189 TIME_WAIT
:0 TCP foobox:8888 localhost:2190 TIME_WAIT
|
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 and arrives at the
server but from after that i only a couple of KB from the start of the file
which cases the picture to display only the very top (of the screen).
All the pictures are saved at the client side before sent to the server and
they...
|
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?
|
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 slowly,
about 4 seconds between send and receive. In our production environment with
hundreds of transactions it was truly costly.
a while ago i changed de TcpClient object. Now i am using a Socket
(System.Net.Sockets.Socket) object and it...
|
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 events associated to the controls, I'm wondering how i
monitor connection state.
On my server side, i loop through a thread that tests the DataAvailable
flag.
On client side, i opened the connection then closed it again after some time
| |
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 closes the connection so I can exit my
listener code. Here is what I have tried.
First I asked MSDN and they said..
|
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 that I want to use the protected Active and Client
properties of the TcpClient class. To do this I made my own derived class:
Public Class MyTcpClient
Inherits TcpClient
|
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 sequence with a server:
connect
write(50 bytes)
read(2002 bytes)
write(50 bytes)
read(2002 bytes)
|
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 the PlayerStream.Read() sets the
PlayerSocket.Connected=false???
I am getting the following error:
System.IO.IOException: Unable to read data from the transport connection: A
connection attempt failed because the connected party did not properly
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |