|
-
Nov 12th, 2008, 08:14 AM
#1
Thread Starter
Member
[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
-
Nov 12th, 2008, 08:20 AM
#2
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:
While(True)
' Code goes here.
End While
-
Nov 12th, 2008, 10:22 AM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|