Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSWINSCK - How to reject a new connection 1

Status
Not open for further replies.

StuH

Programmer
Mar 25, 2001
53
0
0
AU
Hi all,

I have a VB6 app using mswinsck.ocx in order to do a client/server data transfer process.

Basically, my server app sets a (user specified) port to listen to and accepts connections and processes small bits of data that comes through from various clients using my client app via the same port.

My problem is, I have a need to ONLY allow 40 simultaneous connections at any one time. What do I have to do to reject the 41st connection whenever it tries to connect to the same IP and port?

Thanks - and Happy New Year!
 
I assume you have created a control array for your winsock control?

If so, why not declare a (public) variable to use as a counter? Increment the counter each time you accept a connection, and decrement it each time a connection is closed.

Alternatively, if you dynamically load the control when accepting a connection, you could check the UBound of your winsock control array to see if it's >= 40.

Hopefully this gives you some ideas.

Nick
 
In your ConnectionRequest handler try simply exiting without doing an Accept when you want to refuse a connection.
 
Thanks nickd87. Yes, I do have the winsock control as an array and yes, I have a counter that displays on the form which goes up and down. But it only goes down if a client disconnects themselves.

@dilettante - why are the simplest things never in your mind when you need them? I always seem to go looking down the complicated route. Here's that procedure:

Code:
'*****************
Private Sub wskServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim lngWsk As Long
    Dim blnFoundOne As Boolean
    
    'Find an available Winsock control.
    For lngWsk = 1 To wskServer.UBound
        'Socket must be "not in use" and remote end closed.
        blnFoundOne = (Not blnInUse(lngWsk)) _
                  And (wskServer(lngWsk).State = sckClosed)
        If blnFoundOne Then
            Exit For
        End If
    Next
    
    'Create a new one if none are available.
    If Not blnFoundOne Then
        lngWsk = wskServer.UBound + 1
        Load wskServer(lngWsk)
        ReDim Preserve blnInUse(lngWsk)
        ReDim Preserve strBuffer(lngWsk)
        
        'Update our statistics.
        lblMaxClients.Caption = CStr(lngWsk)
    End If
    
    'Accept the connection.
    wskServer(lngWsk).Accept requestID
    blnInUse(lngWsk) = True
    
    'Update our statistics.
    lngActiveClients = lngActiveClients + 1
    lblActiveClients.Caption = CStr(lngActiveClients)
    Log wskServer(lngWsk).RemoteHostIP & " connected as client " & CStr(lngWsk)
End Sub
'*************

So you're saying all I need to do is add a line at the top like:

If lngActiveClients>=40 then Exit Sub

Sorry, my code is getting too long and I didn't even look at this procedure. I'd been looking for syntax for a Reject option - eg.

wskServer(lngWsk).Reject requestID

Thanks very much for simplifying my craziness. :)
 
Yes, it does exactly what I want. I only have two PCs here at home I can test with, so I set the line as:

If lngActiveClients>=1 then Exit Sub

I connected successfully with 1 PC, but the second got kicked off as soon as I tried to connect with it. Change the setting back to 40 and the second PC joined just fine.

Love those 1-line solutions.... :)
 
Excellent!

I was pretty sure that was what you needed but I didn't know if a "connection refused" result was precisely what was desired. I have some servers that will keep an extra Winsock control array around, and temporarily use it to return a "service too busy" status response. I seldom have to "grow" this beyond the initial entry (index 0) but if people hammer on me I can keep up. I've had it grow to 3 or 4 in the past.

The one line approach doesn't require this extra effort. If you have control of the client you can code it to simply look for the proper error result on connection. In my case I need to deal with 3rd party clients and there was a requirement to help users distinguish between "service down" and "service too busy.
 
Hi dilettante,

Actually, now that you mention it, that does sound like a good option. Based on the code above, what would I need to do to respond with "maximum connections exceeded"? I already have a service down/incorrect IP client response (triggered by timeout).

I have control of the client software, so what then would I need to add to receive that response and display it?

Thanks.
 
A lot depends on the nature of the client. For something like a Telnet client you still have to allow minimal session option negaotiation to takes place, and then you can just blast out the message and close the connection.

It's really a question of whether your client waits for a connection acknowledgement message upon connect or not, or whether it can handle unsolicited messages and know what to do with them.

Think about how a lot of the early Internet protocols work (SMTP, FTP, etc.). Server responses always start with a line that contains a status code and description text:

[tt]HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354

<html>
<body>
<h1>Happy New Millennium!</h1>
(more file contents)
.
.
.
</body>
</html>
[/tt]
 
Here is the Connect code:

Code:
Private Sub WSConnect()
MousePointer = vbHourglass
    TrackError = True
    wskClient.RemoteHost = txtServer.Text
    wskClient.RemotePort = lngPort
    DoEvents
    wskClient.Connect
    Do While wskClient.State <> sckConnected
    DoEvents
    Loop
    TrackError = False
MousePointer = vbDefault
End Sub

Here is the connect error code:

Code:
Private Sub wskClient_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    L = MsgBox("Server at " + txtServer.Text + " disconnected or is invalid", vbCritical, "Server Error")
    Log "Error " & CStr(Number) & ": " & Description
    Log "Closing connection"
    wskClient.Close
    DisconnectCleanup
    cmdStop.Visible = False
    cmdStop.Enabled = False
    cmdConnect.Visible = False
    cmdConnect.Enabled = False
    
End Sub

As you can see it's a generic error saying the server isn't there. But what codes should I be looking for if the server simply has ignored the request from the client - as opposed to the server not existing? Any help you can offer to update the code above is greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top