PDA

Click to See Complete Forum and Search --> : Listening Loop


huhhuh
Mar 23rd, 2006, 01:43 AM
mylistener = New TcpListener(MyIp.Any, Port)

mylistener.Start()

While True

If Not mylistener.Pending Then

Console.WriteLine("Awaiting Requests")

Else

listenerThread = New Threading.Thread(AddressOf listen)

Console.WriteLine("Accepting Request")

listenerThread.Start()

End If

End While





I'm having trouble with the listening loop. I've set my app up so that a proxy class is created and started the moment the form loads. With the While loop above, the code works fine and browser and console outputs are as expected. However, the form on which the proxy class is declared doesn't show. When I remove the While loop, the form shows, but apparently based on console output, the listener only functions as far as "Awaiting Requests" and doesn't do anything from that point on. Browser output also reflects this.





Public Sub listen()

Dim msg As [String]

Console.WriteLine("Handling Request")

While True

Dim mytcpclient As TcpClient = mylistener.AcceptTcpClient

Dim buffbyte(mytcpclient.ReceiveBufferSize) As [Byte]

Dim mystream As NetworkStream = mytcpclient.GetStream

If mystream.CanRead = True Then

mystream.Read(buffbyte, 0, mytcpclient.ReceiveBufferSize)

Console.WriteLine("Received stream")

End If

msg = Encoding.ASCII.GetString(buffbyte)

stopconn(mystream, mytcpclient)





Another question. The code above is part of the listen() method called to handle the request. At the end of the code, I'm calling a function stopconn(), passing the networkstream and tcpclient objects connecting me to the requesting client.





mystream.Write(ByteGet, 0, ByteGet.Length)

mystream.Flush()

mystream.Close()

mytcpclient.Close()





This is what happens at the end of the stopconn function. I assume I've terminated all connections to the client but somehow all new requests from the browser hang or supposedly get stuck in endless 'connecting' mode. I tried adding Exit Sub after calling the stopconn function but it didn't work. So how can I ensure I've cleaned up all unnecessary connections and return to the listening loop?