.NET Framework - TCP Send with Response
Asked By Scott
23-Jan-10 10:26 AM

I have to send a TCP stream to a device. The device returns a response and
I cannot figure out how to catch the response. I know the device response
properly because if I connect to it using HyperTerminal I can send the
command and immediately the response is displayed back to me in
HyperTerminal. Now I need to do this programmatically. The send portion of
the code below works because the device response properly but for some
reason I am unable to catch the response. I have looked through MANY examples
online and they all seem to look like this code block.
Please help, and thanks in advance.
Scott
(kc7dji@gmail.com)
System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient();
tcpClient.Connect(LED_IP, LED_Port);
System.Net.Sockets.NetworkStream networkStream =
tcpClient.GetStream();
// Does a simple write.
Byte[] sendBytes = Encoding.ASCII.GetBytes(LED_Stream);
Byte[] RecvBytes = new Byte[4096];
networkStream.Write(sendBytes, 0, sendBytes.Length);
String responseData = String.Empty;
Int32 bytes = networkStream.Read(RecvBytes,
RecvBytes.Length, 0);
responseData =
System.Text.Encoding.ASCII.GetString(RecvBytes, 0, bytes);
this.txtResult.Text = bytes.ToString() + ": " +
responseData;
networkStream.Flush();
tcpClient.NoDelay = true;
tcpClient.Close();
tcpClient = null;
networkStream = null;
Encoding.ASCII.GetBytes
(1)
System.Net.Sockets.NetworkStream
(1)
System.Net.Sockets.TcpClient
(1)
RecvBytes.Length
(1)
NetworkStream
(1)
TcpClient
(1)
String.Empty
(1)
Thread.Sleep
(1)
Scott replied to Scott
Ok I have already found a portion of my problem. I had 2 parameters switched
on the Read command.
Wrong:
Int32 bytes = networkStream.Read(RecvBytes, RecvBytes.Length, 0);
Correct:
Int32 bytes = networkStream.Read(RecvBytes, 0, RecvBytes.Length);
Now my last problem is that I am getting a few bytes of data back but not all
of it. Like it is happening too fast. I am getting 3-5 bytes when I should
be getting about 20.
Any Ideas?
Scott replied to Scott
wow... I have fought this for 3 days.... today must be a good day because I
have it working now.
I just put the thread to sleep for 100 ms and it all works as expected.
Peter Duniho replied to Scott
It may work, but you did not fix it correctly. And as a general rule,
using Thread.Sleep() or similar is never the correct way to fix network
i/o problems.
The most obvious problem in your code is that you are only calling
Read() once. TCP provides no guarantees at all about how many bytes a
given call to Read() will return. In only guarantees that bytes you
receive will be received in the same order in which they were sent.
You must call Read() repeatedly until you have received as much data as is
required.
Pete
ozbea replied to Peter Duniho
It provides a couple more guarantees than that.
It also guarantees that the data will not be duplicated, nor lost
without an error. This is supplementary to, but not the same as
receiving data in the same order as sent.
Also, when Read returns and there was no error and
the sender has not closed the stream, the read count must be >= 1.
Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mike Schilling replied to Peter Duniho
Or you have seen the terminating character, or no data has been sent for
N milliseconds, or whatever, for your particular application, means
TCP, I could issue a "read record" request, and the receiver would
read exactly as much data as the sender sent. Nowadays, the concept
of record has pretty much gone away, everything is just a byte stream,
and every networking application gets to create its own communications
protocol. it is so much simpler. Not.)
Peter Duniho replied to Mike Schilling
IMHO, those ALL fall under the description "as much data as is
required". :)
(In the old days, using DecNET instead of
Only the prevalence of TCP/IP makes it seem like it is that way. There
are lots of other protocols to choose from, if you do not need to go over
the Internet, and many of them support connection-oriented, reliable,
message-based communications.
Pete
Arne_Vajhøj replied to Mike Schilling
RMS over DECNet is record oriented.
And even $QIO(W) with DECnet pass length.
Arne
Mike Schilling replied to Arne_Vajhøj
Sure, my point is that I have not used DECnet for decades; it is all TCP
these days.
Keep getting SocketExceptions .NET Framework I am using the System.Net.Sockets.NetworkStream to read data from a network connection I keep getting exceptions The code: Array.Clear hit by the browser. Am I missing something simple? I have tried various numbers for NetworkStream.ReadTimeout and NetworkStream.WriteTimeout, but all to no avail. Perhaps I just do not know enough to set the right numbers. .NET Microframework Discussions System.Net.Sockets.NetworkStream (1) NetworkStream.WriteTimeout (1) NetworkStream.ReadTimeout (1) IOException (1) Silverlight (1) NetworkStream (1
Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte 192.168.100.44:9100 bei System.Net.Sockets.TcpClient. . ctor(String hostname, Int32 port) bei WindowsApplication1.clsTCP.TCP_Daten(String IP_Adresse, Int32 Port, String Message TCP-Client \ clsTCP.vb:Zeile 18. H = E4ngt es vielleicht an dem TcpClient und dem NetworkStream den ich nicht schlie = DFe? Wie kann ich in den Exception Client und Stream schlie ASCII and store it as a Byte array. Dim data As [Byte]() = 3D System.Text.Encoding.ASCII.GetBytes(Message) ' Get a client stream for reading and writing. Dim stream As NetworkStream = 3D client
einen Hinweis f??r mich? Vielen Dank und viele Gr????e Christian C# - German Discussions Encoding.ASCII.GetBytes (1) ASCIIEncoding.GetBytes (1) Encoding.UTF8.GetBytes (1) ASCIIEncoding (1) UnicodeEncoding (1) Encoding.Default (1 pr??fst Du das nur ASCII ankommt? Bzgl. der Methode: Warum nimmst Du nicht einfach Encoding.ASCII.GetBytes(p)? Deine Extra-Methode ist eigentlich unn??tig. MfG, Ulf Hallo Christian, Ulf hat es warum Nicht-ASCII in Deinem Fall zu Problemen f??hren string p = "??hnlich"; byte[] buffer = Encoding.ASCII.GetBytes(p); string s = Encoding.ASCII.GetString(buffer); MessageBox.Show(s); / / ergibt: "?hnlich" / / da '??' kein
bei System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) - -- Ende der internen Ausnahmestapelüberwachung - -- bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) bei MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32
specification be disabled? Response.ContentType = "multipart / x-mixed-replace; boundary = myboundary"; / / Thanks ASP.NET Discussions Encoding.ASCII.GetBytes (1) System.Threading.Thread.Sleep (1) Response.ContentType (1) ASP.NET (1) OResponse.OutputStream.Write myboundary \ r \ nContent-Type: image / jpeg \ r \ nContent-Length: " + images[ ix ].FrameSize.ToString() + sSeparator; boundary = Encoding.ASCII.GetBytes( sBundary ); oResponse.OutputStream.Write( boundary , 0 , boundary.Length ); oResponse.OutputStream.Write( images[ ix ].Frame , 0