Results 1 to 3 of 3

Thread: [RESOLVED] Background loops?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2008
    Location
    Milton Keynes, UK
    Posts
    49

    Resolved [RESOLVED] Background loops?

    Hello Everyone,

    I am trying to write a small server for a project of mine, i have the loop i want to use to check for connections but im having trouble getting it to run in the background. I have a button on my windows form that i want to enable to disable the loop but im not sure how to go about it?

    My class code is below, it works but it causes the application to stop responding but my cleint can still send and recieve the messages.

    any ideas?

    Code:
    Public Class clsServe
        Shared Sub tcpListen()
            ' Must listen on correct port- must be same as port client wants to connect on.
            Const portNumber As Integer = 8000
            Dim localAddr As IPAddress = IPAddress.Any
    
            Dim tcpListener As New TcpListener(localAddr, portNumber)
            tcpListener.Start()
            Try
                'Accept the pending client connection and return a TcpClient initialized for communication. 
                Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
                strConsoleMessage = "Communication Sarted"
                ConsoleWrite()
                ' Get the stream
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                ' Read the stream into a byte array
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Return the data received from the client to the console.
                Dim clientdata As String = Encoding.ASCII.GetString(bytes)
                strConsoleMessage = clientdata
                Dim responseString As String = "Connected to server."
    
                If clientdata.Contains("skunk02") Then
                    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("OK")
                    networkStream.Write(sendBytes, 0, sendBytes.Length)
                Else
                    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("FAIL")
                    networkStream.Write(sendBytes, 0, sendBytes.Length)
                End If
    
                strConsoleMessage = responseString
                ConsoleWrite()
    
                'Dim sendBytes2 As [Byte]() = Encoding.ASCII.GetBytes(responseString & "Hello")
    
                'networkStream.Write(sendBytes2, 0, sendBytes2.Length)
                'Any communication with the remote client using the TcpClient can go here.
                'Close TcpListener and TcpClient.
                tcpClient.Close()
                tcpListener.Stop()
                strConsoleMessage = "Transaction Complete"
                ConsoleWrite()
            Catch ex As Exception
                tcpListen()
            End Try
            tcpListen()
    
        End Sub
    End Class

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Background loops?

    Theres no reason to be closing and re-opening the TcpListener in each iteration. Keep it open and listening. The reason your application does not respond is that you're executing an infinite loop in it. And not only an infinite loop, but an infinite recursive loop. You're bound to get a StackOverflowException after running your code for some time.
    Search here on MSDN on Threading and you'll find what you need to run the code in a background thread. Also, dont use a recursive infinite loop.. this, for example, would be much better:

    VB.NET Code:
    1. While(True)
    2. ' Code goes here.
    3. End While
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2008
    Location
    Milton Keynes, UK
    Posts
    49

    Re: Background loops?

    Thanks very much, i have found some code to use as a thread with my current code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width