开发者

How to handle multiple clients on a socket server made with winsock on VB6?

开发者 https://www.devze.com 2023-02-05 18:42 出处:网络
i have this very simple chat app made in VB6 using winsock, but as you can see it only accept only one connexion, how can i handle multiple users? Thanks!

i have this very simple chat app made in VB6 using winsock, but as you can see it only accept only one connexion, how can i handle multiple users? Thanks!

Private Sub Winsock1_Close()
    ' Finaliza la conexión
    Winsock1.Close

   开发者_C百科 txtLog = txtLog & "*** Desconectado" & vbCrLf

End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

    If Winsock1.State <> sckClosed Then
        Winsock1.Close ' close
    End If

    Winsock1.Accept requestID

    txtLog = "Cliente conectado. IP : " & _
              Winsock1.RemoteHostIP & vbCrLf

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim dat As String

    Winsock1.GetData dat, vbString
    txtLog = txtLog & "Cliente : " & dat & vbCrLf

End Sub


The solution is to have an array of Winsock objects, and create a new one at runtime. The new object you have created accepts the connection request.

So, in your connection request sub, you would have a new socket:

Dim ConnectionCount as long

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    ConnectionCount=ConnectionCount+1

    Load Winsocks(ConnectionCount)
    Winsocks(ConnectionCount).Accept(requestID)

     txtLog = "Cliente conectado. IP : " & _
          Winsocks(ConnectionCount).RemoteHostIP & vbCrLf

End Sub

Edit: Here is a tutorial that may help you better than my code: http://www.devx.com/tips/Tip/5488

It follows the same idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消