Create Threads on TCPListener.Accept 
Author Message
 Create Threads on TCPListener.Accept

How can i create a thread for each connection that is accepted by a
TCPListener?
My code works if i do not create another thread, but breaks randomly after
some connects if two clients are connecting at the same time. I think it's a
problem of thread synchronisation.

As a former VB6 programmer i am not sure what impacts freethreading has to
my problem.
Below you see the code i use on my server to accept some connections.
SocketThread is my class that get the resulting socket and processes all the
work (check the bytes, send answer).

What else do i have to do to get it work?

Public Class PortListener
    Private m_tcpl As TCPListener

    Public Sub New()

        m_tcpl = New TCPListener(1100)
        m_tcpl.Start()

        While (True)
            Dim s As Socket = m_tcpl.Accept()
            Dim o As New SocketThread()    'custom object
            o.SetSocket(s)

            'This works, but blocks
            o.Start()

            'This works only if one client is connecting
            'Dim t As Thread
            't = New Thread(AddressOf o.Start)
            't.Start()

        End While
    End Sub
End Class

I also tried this for testing, but also crashes

         Dim o(1000) As SocketThread, i As Integer
        Dim t(1000) As Thread, s As socket

        While (True)

            'Accept will block until someone connects
            s = m_tcpl.Accept()

            'Dim o As New SocketThread()
            o(i) = New SocketThread()
            o(i).SetSocket(s)

            t(i) = New Thread(AddressOf o(i).Start)
            t(i).Start()

            'o.Start()
            i = i + 1

        End While

Thanks for every help
Cyrill



Mon, 14 Jul 2003 01:11:33 GMT  
 Create Threads on TCPListener.Accept
The problem was that i always startet the server from the IDE. When i start
i as exe everything work fine.

Maybe the console windows "thread 0xf46 exited....." caused the crash?

Cyrill



Mon, 14 Jul 2003 16:01:30 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. TCPListener.Accept() not blocking?

2. Creating a Web Service to accept complex datatypes

3. Creating VB EXE that accepts parameters when called

4. Creating a VB exe that accepts parameters

5. how to create active X use in web that accept param in VB

6. how to create active X use in web that accept param in VB

7. Creating jet 4.0 table columns that can accept null values

8. creating new column with ADOX to accept NULL values

9. idle cycle eating tcplisteners

10. TcpListener Problem - Help!!

11. TCPListener - SocketServer.vb (0/1)

12. TcpListener

 

 
Powered by phpBB® Forum Software