473,782 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NetworkStream .Write() method not completing

I'm trying to create a simple, synchronous TCP client program to
receive requests and return data. My code very closely resembles the
example code provided in the Help files, but I find that the .Write
method - when run at full speed - only seems to successfully write
data the first time it is called.

For testing purposes, I am using Hyperterminal on another computer to
send 4 characters. The program always successfully reads all 4
characters. It is then supposed to reverse them and send the reversed
byte array back. Here is the code:

Private tcpLstn As New TcpListener(511 12)

Public Sub subMainLoop()
Dim tcpClnt As TcpClient
Dim ns As NetworkStream
Dim buffr(4) As Byte
Dim buffr2(6) As Byte
Dim iRead As Integer

bScan = True

tcpLstn.Start()

While bScan
'--- Code execution pauses here until TCP connection
request
'--- is received.
tcpClnt = tcpLstn.AcceptT cpClient()

ns = tcpClnt.GetStre am()

'--- Wait until 4 characters are successfully read
iRead = 0
While iRead < 4
iRead = iRead + ns.Read(buffr, iRead, 4 - iRead)
End While

'--- Create array of characters to send back
buffr2(0) = 13
buffr2(1) = 10
buffr2(2) = buffr(3)
buffr2(3) = buffr(2)
buffr2(4) = buffr(1)
buffr2(5) = buffr(0)

Try
'--- Send requested data over TCP port
ns.Write(buffr2 , 0, 6)
ns.Close()
tcpClnt.Close()
Catch e As Exception
Console.WriteLi ne(e.ToString() )
End Try

'--- Process events
Application.DoE vents()
End While

tcpLstn.Stop()

End Sub
When stepping through in debug mode, the ns.Write(buffr2 , 0, 6)
command works about 95% of the time, and the returned characters are
displayed in HyperTerminal. When running the program with no breaks
and no MsgBox calls in the main loop, the program only successfully
writes back the 4 characters (plus {LF}{CR}) the first time. From
HyperTerminal (and other testing methods) I can tell that the 4
characters I send ARE, in fact, being read by the program EVERY time.
The response simply isn't coming through.

Ultimately, the program is only ever going to be communicating with
one other computer. So there isn't any need for multi-threading or
asynchronous reading/writing.

Am I missing something, though?
Nov 20 '05 #1
2 4657
I can't believe that in this day in age there are still inherent timing issues/bugs in released development classes. This is RETARDED!!

The problem, it turns out, is that the NetworkStream .Write() method returns IMMEDIATELY. It does NOT wait until the data has actually been sent. Microsoft claims that it returns after making sure the data was added to the STREAM. This may be the case. However, there is no guarantee that the stream has actually been sent before closing the connection

My code reads like this
...
ns.Write(buffr2 , 0, 6
ns.Close(
tcpClnt.Close(
...

Problem was: ns.Close() and tcpClnt.Close() were getting called before the data stream was actually sent - i.e. the underlying socket got closed before completing pending data transmissions. The code is/will be running on a P4 1.8GHz+ processor. The fix was to put in a manual loop delay (which any decent program should NEVER have to resort to) right after the ns.Write() command. This is a sucky solution, but the only one that works

...
ns.Write(buffr2 , 0, 6
For i = 1 to 100000
i =
Nex
ns.Close(
tcpClnt.Close(
...

I tried enabling tcpClnt.LingerS tate, setting tcpClnt.NoDelay = True, raising the tcpClnt.LingerT ime, making sure the .SendBuffer was set low since I am only sending 6 bytes at a time - but none of those things made any difference. THE ONLY WAY to avoid the timing limitation on sending data is to create a manual delay or have other code executing before calling the .Close() methods

Someone needs to put this in a book/article somewhere

Nov 20 '05 #2
I can't believe that in this day in age there are still inherent timing issues/bugs in released development classes. This is RETARDED!!

The problem, it turns out, is that the NetworkStream .Write() method returns IMMEDIATELY. It does NOT wait until the data has actually been sent. Microsoft claims that it returns after making sure the data was added to the STREAM. This may be the case. However, there is no guarantee that the stream has actually been sent before closing the connection

My code reads like this
...
ns.Write(buffr2 , 0, 6
ns.Close(
tcpClnt.Close(
...

Problem was: ns.Close() and tcpClnt.Close() were getting called before the data stream was actually sent - i.e. the underlying socket got closed before completing pending data transmissions. The code is/will be running on a P4 1.8GHz+ processor. The fix was to put in a manual loop delay (which any decent program should NEVER have to resort to) right after the ns.Write() command. This is a sucky solution, but the only one that works

...
ns.Write(buffr2 , 0, 6
For i = 1 to 100000
i =
Nex
ns.Close(
tcpClnt.Close(
...

I tried enabling tcpClnt.LingerS tate, setting tcpClnt.NoDelay = True, raising the tcpClnt.LingerT ime, making sure the .SendBuffer was set low since I am only sending 6 bytes at a time - but none of those things made any difference. THE ONLY WAY to avoid the timing limitation on sending data is to create a manual delay or have other code executing before calling the .Close() methods

Someone needs to put this in a book/article somewhere

Nov 20 '05 #3

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

Similar topics

1
556
by: cmjman | last post by:
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...
4
7208
by: 0to60 | last post by:
I have a class that wraps a TcpClient object and manages all the async reading of the socket. It works really nice, and I use it all over the place. But there's this ONE INSTANCE where I create one of these things and it WON'T read data. If I set a breakpoint in my EndRead callback, it never goes off. NOTHING is different from anywhere else I use this class, its just this one place. Now, if I create a second constructor for my class...
1
4785
by: Casey Watson | last post by:
Hi :) I'm having some major trouble with an XML Client/Server application that I am writing. I am using NetworkStream with CryptoStream to Read and Write XML between computers. Now, whenever I write the XML, the Server section which uses the XMLTextReader Class will not accept EndElements period, and neither the ReadStartElement or ReadEndElement will work properly. However, if I only send to WriteStartElements over the network, the...
0
266
by: JeremyH | last post by:
I'm trying to create a simple, synchronous TCP client program to receive requests and return data. My code very closely resembles the example code provided in the Help files, but I find that the .Write method - when run at full speed - only seems to successfully write data the first time it is called. For testing purposes, I am using Hyperterminal on another computer to send 4 characters. The program always successfully reads all 4...
7
5188
by: david.topham | last post by:
Hi The code below demostrates an issue I'm having with with NetworkStream: using System; using System.Net.Sockets; namespace TCPCTest { class Class1
2
4101
by: PiotrKolodziej | last post by:
Hi I have a simple question. Here is the code related to my question: while (true) { if (tcpListenerServer.Pending() && !this.Disposing) { TcpClient tcpClient = tcpListenerServer.AcceptTcpClient();
5
4220
by: | last post by:
I've got a basic "chat" infrastructure that can send objects across the wire, but I'm running into a few issues with trying to send multiple messages and things behaving differently in debug and release... Is there anyone out there that considers themselves a threading / sockets / serialization guru? Not sure if it's appropriate to post an entire project here and the issues discussion is probably a bit too complex for newsgroup back and...
7
4276
by: littleIO | last post by:
Hi, I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated. I have a server which listens, receives calls, processes them and sends back the results to clients. The code below makes the client application not to respond. Client can send data but is stuck in the process of waiting information back from the server. Any ideas?
6
7281
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I remember there are 2 streams in Java, one for read, one for write. If I just use synchronous Read() and Write() method:
0
9639
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, 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...
0
9479
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,...
0
10311
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, 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...
0
10146
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 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...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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 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.