Results 1 to 11 of 11

Thread: TCP server/client

  1. #1
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    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.
    FOX Designs

  2. #2
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,866

    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.

  3. #3
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    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?
    FOX Designs

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 00
    Location
    Good Old Europe
    Posts
    3,562

    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!

  5. #5
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    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...
    FOX Designs

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 00
    Location
    Good Old Europe
    Posts
    3,562

    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!

  7. #7
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,866

    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.

  8. #8
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    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
    FOX Designs

  9. #9
    Frenzied Member Evil_Giraffe's Avatar
    Join Date
    Aug 02
    Location
    Suffolk, UK
    Posts
    1,880

    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.

  10. #10
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    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.
    FOX Designs

  11. #11
    New Member
    Join Date
    Jul 12
    Location
    india
    Posts
    4

    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
  •