networkStream.Write waits until networkstream.close

C

cmjman

I have an issue where networkStream.Write doesn't perform its write
downstream from my client program until the network stream is closed.
I then see the data that was sent appear on the other side.

I am sending a small amound of data and read where data wasn't sent
until the buffer reached a larger size. However, there is a
TcpClient property call NoDelay that is suppose to eliminate this
delay. Here is a snippet of my code below. Can anyone think of a
reason why the networkStream.Write isn't performed until the
networkstream is closed?

Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("i set ip here", "and port here")
//connection occurs as expected
tcpClient.NoDelay() = True

Dim networkStream As NetworkStream = tcpClient.GetStream()

Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("test")
If NetworkStream.CanWrite And NetworkStream.CanRead Then
' Do a simple write.
networkStream.Write(sendBytes, 0, sendBytes.Length)
//this .write is returned but the data is not sent until a
networkstream.close is performed

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
H

Helge Jensen

cmjman said:
I have an issue where networkStream.Write doesn't perform its write
downstream from my client program until the network stream is closed.

That's because streams are buffered, and need to be Flush()'ed to force
sending data.

Buffering allows the runtime/OS to do much more efficient transmits.
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("i set ip here", "and port here")
//connection occurs as expected
tcpClient.NoDelay() = True

Assigning to the output of functions rarely work, since the functions
usually doesn't return references. I don't know the specifics of the
NoDelay function, but I would be very suprised if the above line
actually changed the NoDelay for tcpClient.
Dim networkStream As NetworkStream = tcpClient.GetStream()

Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("test")
If NetworkStream.CanWrite And NetworkStream.CanRead Then

Why do you even check this? wouldn't you rather have an error if you
couldn't send?
' Do a simple write.
networkStream.Write(sendBytes, 0, sendBytes.Length)

networkstream.Flush()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

TCP Send with Response 8
TCP Transfer issues 3
TCP/IP 3
NNTP Client 4
How to debug this? 10
sending xml over tcp/ip 7
Testing client server application from single computer 3
a simple client example 3

Top