|
-
Apr 8th, 2013, 12:18 PM
#1
Thread Starter
Hyperactive Member
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
-
Apr 8th, 2013, 12:57 PM
#2
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
-
Apr 8th, 2013, 01:02 PM
#3
Re: Not listening. You must call the Start() method before calling this method.
Why are you using a timer to accept incoming messages?
-
Apr 8th, 2013, 02:06 PM
#4
Thread Starter
Hyperactive Member
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.
-
Apr 8th, 2013, 02:36 PM
#5
Thread Starter
Hyperactive Member
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.
-
Apr 8th, 2013, 02:38 PM
#6
Thread Starter
Hyperactive Member
Re: Not listening. You must call the Start() method before calling this method.
 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.
Last edited by Vladamir; Apr 8th, 2013 at 03:14 PM.
-
Apr 9th, 2013, 06:17 AM
#7
Thread Starter
Hyperactive Member
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.
-
Apr 10th, 2013, 04:36 PM
#8
Thread Starter
Hyperactive Member
Re: Not listening. You must call the Start() method before calling this method.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|