albertleng
8/6/2010 3:48:00 PM
Great!!! By having a Winsock control which solely listens for
connection request and pass the request to another Winsock control
works!
The next thing i'll try to implement is to allow multiple clients
connections.
Thanks a lot! :)
On Aug 5, 10:53 pm, "Nobody" <nob...@nobody.com> wrote:
> There is no socket listening for incoming connections all the time in your
> software, so if something goes wrong with the current connection, there is
> nothing waiting for the client to reconnect to. You have to implement
> support for more than one client. At least, you have to add tcpListener
> control, then keep that in Listen mode all the time. Example:
>
> Private Sub Form_Load()
> tcpServer.LocalPort = 500
> tcpListener.LocalPort = 500
> tcpListener.Listen
> End Sub
>
> Private Sub tcpListener_ConnectionRequest(ByVal requestID As Long)
>
> If tcpServer.State <> sckClosed Then
> tcpServer.Close
> End If
> ' Accept the request with the requestID
> ' parameter.
> tcpServer.Accept requestID
>
> End Sub
>
> Note how I moved the code from tcpServer_ConnectionRequest() to
> tcpListener_ConnectionRequest(). "tcpListener" will remain in listening mode
> forever. There is no need to call "Listen" anywhere again, including in
> Close event.
>
> The code above will allow one client to connect, even after crashing, but if
> there is a second client trying to connect, the first client is
> disconnected. To really support more than one client, you have to turn
> tcpServer into a control array, like the MSDN article shows. The MSDN
> article has a little problem, it doesn't reuse existing closed sockets, so
> after 32767 connections, your software would show an error message and stops
> accepting new connections. To fix this, you need to loop through the control
> array, and reuse sockets. Example:
>
> Found = False
> For i = tcpServer.LBound To tcpServer.UBound
> If tcpServer(i).State = sckClosed Then
> ' Reuse this one
> Found = True
> Exit For
> End If
> Next
> If Found Then
> ' Accept the request with the requestID
> ' parameter.
> tcpServer(i).Accept requestID
> Else
> i = tcpServer.UBound+1
> Load tcpServer(i)
> tcpServer(i).Accept requestID
> End If