By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

networkStream.Write waits until networkstream.close

P: n/a
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
*-----------------------*
Nov 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
cmjman wrote:
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()

--
Helge
Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.