|
-
Jul 31st, 2012, 08:35 PM
#1
Thread Starter
Addicted Member
TCP server/client
Hello i have been following a tutorial and after finishing the server part wont run. can someone please help me with this error.
Error:
Code:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
At this line:
Code:
Dim server As New clsServer
Code:
Code:
Imports System.Net
Imports System.Net.Sockets
Public Class clsServer
Dim server As New clsServer
Private Listener As TcpListener
Private t As System.Threading.Thread
Private reader As System.IO.StreamReader
Public Clients As New Collection
Public Sub StartListening()
Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 6580)
Listener.Start()
t = New System.Threading.Thread(AddressOf ListenContinuously)
t.Start()
End Sub
Public Sub StopListening()
t.Abort()
End Sub
Private Sub ListenContinuously()
While True = True
If Listener.Pending = True Then
AcceptClient()
End If
'PollForMessages() <This is for a later tutorial. That's why this line is commented out>
End While
End Sub
Private Sub AcceptClient()
Dim pendingClient As TcpClient
Dim nameOfClient As String
pendingClient = Listener.AcceptTcpClient()
reader = New System.IO.StreamReader(pendingClient.GetStream)
nameOfClient = reader.ReadLine()
Clients.Add(pendingClient, nameOfClient)
MessageBox.Show(nameOfClient & " has just signed in.")
End Sub
Private Sub btnStartServer_Click(sender As System.Object, e As System.EventArgs) Handles btnStartServer.Click
server.StartListening()
btnStartServer.Enabled = False
btnStopServer.Enabled = True
End Sub
Private Sub btnStopServer_Click(sender As System.Object, e As System.EventArgs) Handles btnStopServer.Click
server.StopListening()
btnStopServer.Enabled = False
btnStartServer.Enabled = True
End Sub
Private Sub clsServer_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
server.StopListening()
End Sub
End Class
I will need help with the client part as well.
-
Jul 31st, 2012, 11:37 PM
#2
Re: TCP server/client
See, what you're doing is, inside the clsServer, you're re-initializing the server again:
Code:
Dim server As New clsServer
So every new instance of clsServer will continuously create new instances of the server.
-
Jul 31st, 2012, 11:51 PM
#3
Thread Starter
Addicted Member
Re: TCP server/client
thank you for replying... but i really dont know how to fix that problem, even though you told me what is happening..
so how do i fix this?
-
Aug 1st, 2012, 12:04 AM
#4
Re: TCP server/client
You should not create (or initialize) an instance of your class "clsserver" inside the code of that class, that would constitute an infinite loop (each new instance will create a new instance) resulting in a stack-overflow.
Instead you should initalize in the Main or MainForm-code. After that you can call the class-methods like "startListening and "StopListening".
However the Subs handling From-events should NOT be part of the clsserver-code!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Aug 1st, 2012, 12:21 AM
#5
Thread Starter
Addicted Member
Re: TCP server/client
Not sure i understand fully or at all what you mean but i did try this based on what i think you meant.
Code:
Private Sub btnStartServer_Click(sender As System.Object, e As System.EventArgs) Handles btnStartServer.Click
Dim server As New clsServer
server.StartListening()
btnStartServer.Enabled = False
btnStopServer.Enabled = True
End Sub
Private Sub btnStopServer_Click(sender As System.Object, e As System.EventArgs) Handles btnStopServer.Click
Dim server As New clsServer
server.StopListening()
btnStopServer.Enabled = False
btnStartServer.Enabled = True
End Sub
Private Sub clsServer_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim server As New clsServer
server.StopListening()
End Sub
i moved it from where it was and placed it in each sub where its needed and it works just fine with the client...
-
Aug 1st, 2012, 12:42 PM
#6
Re: TCP server/client
I don't think that it wil work this way. You need to have the server declared only once just above all Subs in the MainForm-class.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Aug 1st, 2012, 03:01 PM
#7
Re: TCP server/client
Code:
Public Class clsServer
Dim server As New clsServer
Private Listener As TcpListener
Private t As System.Threading.Thread
Private reader As System.IO.StreamReader
-snip-
End Class
The "Dim server As New clsServer" is causing your overflow.
-
Aug 1st, 2012, 09:50 PM
#8
Thread Starter
Addicted Member
Re: TCP server/client
I know that is causing it but fixing it is the problem.. since the next tutorial is not up yet i have no idea what to do.
LINK to tut:
http://www.vbprogramming.8k.com/tutorials/LocalTCP.htm
-
Aug 2nd, 2012, 03:47 AM
#9
Re: TCP server/client
The issue is that you've created a Form called clsServer. If you read through the tutorial carefully, you'll see that there are two code files involved. The first is a class called clsServer. This is not a Form! Later on, he creates a Form. This form uses a clsServer instance, and it is this form that includes the Dim server As New clsServer line. Since that is instantiating the class, not the form, this doesn't cause the infinite recursion.
You can either start again from scratch or try and figure out what goes in which file. If you're not too familiar with the concept of classes, instances and objects, I'd suggest starting from the beginning again.
-
Aug 2nd, 2012, 09:22 AM
#10
Thread Starter
Addicted Member
Re: TCP server/client
thank you Evil_Giraffe for pointing this out to me.. i guess i should have put the LINK in my first post.
-
Aug 2nd, 2012, 12:21 PM
#11
New Member
Re: TCP server/client
Hey upload ur full source code (zip)...ill help you solve ur problem...hopefully
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
|