networkStream.Write waits until networkstream.close

  • Thread starter Thread starter cmjman
  • Start date Start date
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
*-----------------------*
 
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()
 
Back
Top