Results 1 to 8 of 8

Thread: Not listening. You must call the Start() method before calling this method.

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Not listening. You must call the Start() method before calling this method.

    I was using this nice little snippet of code on a Windows XP Pro machine before lunch and it worked perfectly. I then went out for a bite and came back and started testing it again. But now all of a sudden I keep getting an Exception:

    Not listening. You must call the Start() method before calling this method.
    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.IO
    
    Public Class Form1
    
        Dim Listener As New TcpListener(81)
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
            ListThread.Start()
        End Sub
    
        Private Sub Listening()
            Listener.Start()
        End Sub
    
        Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
                Message = ""
                Client = Listener.AcceptTcpClient()
    
                Dim Reader As New StreamReader(Client.GetStream())
                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString
                End While
                MsgBox(Message, MsgBoxStyle.OkOnly)
            End If
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Listener.Stop()
        End Sub
    
    End Class
    What appears to be happening is when the machine I'm using to talk to this listening code shuts down before the listen code shuts down this problem starts. If I restart the editor it still happens until some time that it starts working again. I must be missing something here. This is the code I use to send the P2P stream over to the code above.

    Code:
    Imports System.Net.Sockets
    Imports System.IO
    
    Public Class Form1
    
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Client = New TcpClient("192.168.20.145", 81) ' Change the IP here to the server's address
            Dim Writer As New StreamWriter(Client.GetStream())
            Writer.Write(TextBox1.Text)
            Writer.Flush()
        End Sub
    
    End Class

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Not listening. You must call the Start() method before calling this method.

    Moved from VB.net CodeBank
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Not listening. You must call the Start() method before calling this method.

    Why are you using a timer to accept incoming messages?

  4. #4

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Not listening. You must call the Start() method before calling this method.

    ident, thanks for your question on that. The honest answer is because that's the way the code I found sometime ago showed it to work. At least this method. And it does work, I'm just trying to understand it more and take more control with it. What I am seeing is that on starting the server code, it comes up and runs some of the time, but other times it throws that above exception. Now, to also be honest, I'm on a LAN at a client's office which is notorious for latency problems. But as I see the server code, it should just come up and begin listening. It should not depend on the client already running. But some times that's what happens here at least. The server throws the exception if the client is not running first. But still other times it runs fine. Right now it's throwing the exception at me.

    and koolsid, yes, I posted this a while back in the codebank as a down and dirty P2P application. I'm testing lots of new ideas around here.

    Thanks for the replies so far.

  5. #5

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Not listening. You must call the Start() method before calling this method.

    Still kind of new to this but I changed the interval setting on the timer from 10 to 100 and it seems to have settled things down on this LAN.

  6. #6

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Not listening. You must call the Start() method before calling this method.

    Quote Originally Posted by ident View Post
    Why are you using a timer to accept incoming messages?
    So I could omit the timer. I'm all for expediency.

    And there is something wrong with the timer. It seems to be the source of the problem with this, at least on this LAN. I'm going to research another method.
    Last edited by Vladamir; Apr 8th, 2013 at 03:14 PM.

  7. #7

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Not listening. You must call the Start() method before calling this method.

    Ok, a total rewrite was needed for this and I eliminated the timer. I found this code on M$ site. It works well but I'm having to fine tune it for my needs. Thanks again to all who offered advice. I'm making progress.

  8. #8

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Not listening. You must call the Start() method before calling this method.

    Quote Originally Posted by ident View Post
    Why are you using a timer to accept incoming messages?
    Hey ident, I've been doing lots of testing with this and tried another method which uses a While loop. Had some success with it but came back to this timer method. I'm curious if there is a better method of doing this. This project takes data, processes through Excel and send the results back to the client. The trouble I'm having with the While method is that no matter how hard I try, it leaves an instance of Excel in the task manager. I can't say why its doing that or if it's the While that is causing it. But very similar code I've been using for months now does not have this problem. It's a long story and one I hope to have sorted out here soon. Any advice you have on doing a better P2P process would be appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width