TcpListener problem
-
Dim listener As New TcpListener(System.Net.IPAddress.Any, 7778) 'start it listener.Start() Dim bytes(1024) As Byte Dim Command As String = Nothing While True Console.WriteLine("Waiting for a connection... ") Dim client As TcpClient = listner.AcceptTcpClient() Console.WriteLine("Client connected... ") Console.WriteLine("Please Wait... ") Dim ns As NetworkStream = client.GetStream() Command = Nothing Dim i As Integer i = ns.Read(bytes, 0, bytes.Length) While (i <> 0) Command = System.Text.Encoding.ASCII.GetString(bytes, 0, i) Console.WriteLine(Command) i = ns.Read(bytes, 0, bytes.Length) 'stopper End While ns.Close() client.Close() Console.WriteLine("...") End While
Can anyone please help me why the execution stops at the bold line. Compiler don't execute anything after this line?
AliAmjad(MCP) First make it Run THEN make it Run Fast!
-
Dim listener As New TcpListener(System.Net.IPAddress.Any, 7778) 'start it listener.Start() Dim bytes(1024) As Byte Dim Command As String = Nothing While True Console.WriteLine("Waiting for a connection... ") Dim client As TcpClient = listner.AcceptTcpClient() Console.WriteLine("Client connected... ") Console.WriteLine("Please Wait... ") Dim ns As NetworkStream = client.GetStream() Command = Nothing Dim i As Integer i = ns.Read(bytes, 0, bytes.Length) While (i <> 0) Command = System.Text.Encoding.ASCII.GetString(bytes, 0, i) Console.WriteLine(Command) i = ns.Read(bytes, 0, bytes.Length) 'stopper End While ns.Close() client.Close() Console.WriteLine("...") End While
Can anyone please help me why the execution stops at the bold line. Compiler don't execute anything after this line?
AliAmjad(MCP) First make it Run THEN make it Run Fast!
When you say stops, do you mean it throws an exception? What is the exception? have you looked at the networkstream class documentation, you should be testing it canread before trying to read. look at the read method docs. [modified text] i just noticied that you have basically copied the example in the MSDN for the TCPListener, i have found in the past that these are not always reliable, and basically just show how everything hooks together, you still then need to look at each of the classes you are using to get more of the specific requirements (like the canread etc.)
Dave GoogleWave: dave.m.auld[at]googlewave.com Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo
modified on Saturday, January 30, 2010 3:34 AM
-
When you say stops, do you mean it throws an exception? What is the exception? have you looked at the networkstream class documentation, you should be testing it canread before trying to read. look at the read method docs. [modified text] i just noticied that you have basically copied the example in the MSDN for the TCPListener, i have found in the past that these are not always reliable, and basically just show how everything hooks together, you still then need to look at each of the classes you are using to get more of the specific requirements (like the canread etc.)
Dave GoogleWave: dave.m.auld[at]googlewave.com Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo
modified on Saturday, January 30, 2010 3:34 AM
Thank you for your reply daveauld. Actually it didn't throw any exception at all just stops the execution and won't continue the loop or the remaining statements after the loop which is kind of weird. Do you have any idea what's wrong with the above code???
AliAmjad(MCP) First make it Run THEN make it Run Fast!
-
Thank you for your reply daveauld. Actually it didn't throw any exception at all just stops the execution and won't continue the loop or the remaining statements after the loop which is kind of weird. Do you have any idea what's wrong with the above code???
AliAmjad(MCP) First make it Run THEN make it Run Fast!
I would change the
read
part to match the example in thenetworkstream.read
example in the docs so;Do
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length)
Loop While myNetworkStream.DataAvailableDave GoogleWave: dave.m.auld[at]googlewave.com Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo
-
Dim listener As New TcpListener(System.Net.IPAddress.Any, 7778) 'start it listener.Start() Dim bytes(1024) As Byte Dim Command As String = Nothing While True Console.WriteLine("Waiting for a connection... ") Dim client As TcpClient = listner.AcceptTcpClient() Console.WriteLine("Client connected... ") Console.WriteLine("Please Wait... ") Dim ns As NetworkStream = client.GetStream() Command = Nothing Dim i As Integer i = ns.Read(bytes, 0, bytes.Length) While (i <> 0) Command = System.Text.Encoding.ASCII.GetString(bytes, 0, i) Console.WriteLine(Command) i = ns.Read(bytes, 0, bytes.Length) 'stopper End While ns.Close() client.Close() Console.WriteLine("...") End While
Can anyone please help me why the execution stops at the bold line. Compiler don't execute anything after this line?
AliAmjad(MCP) First make it Run THEN make it Run Fast!
Hi, Stream.Read() is a blocking call. Read the documentation, it contains: "read; however, if an exception occurs, the current position within the stream remains unchanged. Implementations return the number of bytes read. The return value is zero only if the position is currently at the end of the stream. The implementation will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file)." So your program is waiting inside the Read() method for more data to become available. You may have to restructure your communication, maybe use asyncrhonous calls, another thread, a time-out, it all depends on the circumstances. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Dim listener As New TcpListener(System.Net.IPAddress.Any, 7778) 'start it listener.Start() Dim bytes(1024) As Byte Dim Command As String = Nothing While True Console.WriteLine("Waiting for a connection... ") Dim client As TcpClient = listner.AcceptTcpClient() Console.WriteLine("Client connected... ") Console.WriteLine("Please Wait... ") Dim ns As NetworkStream = client.GetStream() Command = Nothing Dim i As Integer i = ns.Read(bytes, 0, bytes.Length) While (i <> 0) Command = System.Text.Encoding.ASCII.GetString(bytes, 0, i) Console.WriteLine(Command) i = ns.Read(bytes, 0, bytes.Length) 'stopper End While ns.Close() client.Close() Console.WriteLine("...") End While
Can anyone please help me why the execution stops at the bold line. Compiler don't execute anything after this line?
AliAmjad(MCP) First make it Run THEN make it Run Fast!
It stops because .Read is a blocking call and won't return until it has actually read something. Since there's nothing more to read, the .Read call will sit there until there is something.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
It stops because .Read is a blocking call and won't return until it has actually read something. Since there's nothing more to read, the .Read call will sit there until there is something.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...