Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

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

$
0
0
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:

Quote:

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


Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>