Instead of only having a certain amount of clients specified to connect to the winsock, how do I make it so it uses as much connections as possible? So it uses as many as the computer can hold instead of giving a specific amount?
Printable View
Instead of only having a certain amount of clients specified to connect to the winsock, how do I make it so it uses as much connections as possible? So it uses as many as the computer can hold instead of giving a specific amount?
Like this.
It assumes that there's a Winsock control array on your form with the name "sckServer".
So, just add a Winsock control, name it "sckServer" and set its Index property to 0.
I didn't test it, but it should work.Code:Option Explicit
Private Function Server_FindSocket() As Integer
Dim i As Integer, intRet As Integer
If sckServer.UBound = 0 Then
Load sckServer(1)
Server_FindSocket = 1
Else
For i = 1 To sckServer.UBound
If sckServer(i).State = sckClosed Then
intRet = i
Exit For
End If
Next i
If intRet > 0 Then
Server_FindSocket = intRet
Else
On Error Resume Next
'Could raise error if server is completely full
intRet = sckServer.UBound + 1
If Err.Number = 0 Then
Load sckServer(intRet)
Server_FindSocket = intRet
Else
Debug.Print "Connection rejected: server full!"
End If
On Error GoTo 0
End If
End If
End Function
Private Sub Form_Load()
sckServer(0).LocalPort = 1234
sckServer(0).Listen
End Sub
Private Sub sckServer_Close(Index As Integer)
sckServer(Index).Close
End Sub
Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim intSocket As Integer
intSocket = Server_FindSocket
If intSocket > 0 Then
sckServer(intSocket).Accept requestID
Debug.Print "New connection on socket #" & intSocket & " from " & sckServer(intSocket).RemoteHostIP
End If
End Sub