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:
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
Re: Not listening. You must call the Start() method before calling this method.
Moved from VB.net CodeBank
Re: Not listening. You must call the Start() method before calling this method.
Why are you using a timer to accept incoming messages?
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.
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.
Re: Not listening. You must call the Start() method before calling this method.
Quote:
Originally Posted by
ident
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.
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.
Re: Not listening. You must call the Start() method before calling this method.
Quote:
Originally Posted by
ident
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.