Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. TcpListener problem

TcpListener problem

Scheduled Pinned Locked Moved Visual Basic
helpcsharpmcpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliAmjad
    wrote on last edited by
    #1
            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!

    D L D 3 Replies Last reply
    0
    • A AliAmjad
              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!

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • D DaveAuld

        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

        A Offline
        A Offline
        AliAmjad
        wrote on last edited by
        #3

        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!

        D 1 Reply Last reply
        0
        • A AliAmjad

          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!

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #4

          I would change the read part to match the example in the networkstream.read example in the docs so;

          Do
          numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length)
          Loop While myNetworkStream.DataAvailable

          Dave GoogleWave: dave.m.auld[at]googlewave.com Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

          1 Reply Last reply
          0
          • A AliAmjad
                    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!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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]


            1 Reply Last reply
            0
            • A AliAmjad
                      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!

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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...

              A 1 Reply Last reply
              0
              • D Dave Kreskowiak

                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...

                A Offline
                A Offline
                AliAmjad
                wrote on last edited by
                #7

                Thanks for your answers guys now i got the reason for this kind of behavior.

                AliAmjad(MCP) First make it Run THEN make it Run Fast!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups