Solved

WINSOCK - CONNECTIONREQUEST HELP

Posted on 2004-08-16
3
834 Views
Last Modified: 2013-12-25
Hi everyone,

I'm having some problems with the winsock _ConnectionRequest event and would appreciate some one help.  What I'm trying to do is, when the event is fired, to have a msgbox to accept or not the requested connection.  Here is what I've done so far:

'**** part of the code extracted from www.Planet-Source-Code.com ***
' a new client connected
Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
   
    Dim strRemoteHost As String
    Dim aFreeSocket As Integer
   
    ' Request the number of an unused socket
    aFreeSocket = GetFreeSocket
   
    If aFreeSocket = 0 Then      
        ' Tell the new client that the server is full and close the connection
        DebugText "Number of maximum clients reached ! A connection had to be refused!"
        sckAccept(0).Accept requestID
        DoEvents
        sckAccept(0).SendData "Sorry, server is full!"
        DoEvents
        sckAccept(0).Close
    Else
        If MsgBox("The following client " & sckAccept(aFreeSocket).RemoteHostIP & " is asking for a connection. Allow?", vbYesNo, "Connection Attempt) = vbYes Then
            ' accept the connection on a free socket. set status of this socket to true(used)
            bSocketStatus(aFreeSocket) = True
            'sckAccept(aFreeSocket).GetData
            sckAccept(aFreeSocket).Accept requestID
            DoEvents
            DebugText "A new Client with ID " & aFreeSocket & " and IP " & sckAccept(aFreeSocket).RemoteHostIP & " connected!"
            ' Send a welcome message to the new client
            sckAccept(aFreeSocket).SendData "Connection Accepted..."
            ' Refresh the combobox -> add our new client
            'RefreshComboBox
            AddToCombo aFreeSocket
        Else 'optional
            ' Tell the new client that the requested connection has been refused
            sckAccept(0).Accept requestID
            DoEvents
            sckAccept(0).SendData "Sorry, connection refused by the server!"
            DoEvents
            sckAccept(0).Close        
        End If    
    End If
'***** code ends here!

The problem I have found is that until I the connection is not accepted there is no way to retrieve the RemoteHost.  So when the msgbox is fired, since the connection has not been established yet the sckAccept(aFreeSocket).RemoteHostIP is an empty string...

e.g.
"The following client    is asking for a connection. Allow?"  instead of
"The following client xxx.xxx.xxx is asking for a connection. Allow?"

Hope I explain myself.  All help will be appreciated..

Thanks in advance



0
Comment
Question by:chisco
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 11809796
You can get the RemoteHost from the socket that is listening, which in your case is sckListen:

    sckListen.RemoteHostIP ' <--- This will give you IP of machine trying to connect

So you can change your MsgBox line to this:

    If MsgBox("The following client " & sckListen.RemoteHostIP & " is asking for a connection. Allow?", vbYesNo, "Connection Attempt) = vbYes Then

Regards,

Idle_Mind
0
 

Author Comment

by:chisco
ID: 11810121
Hi Idle Mind,

thanks for the response.  the sckListen is inside an array, allowing up to 20 connections.. so what you suggest will not work since I have to pass the index to the socket


'*****
'e.g. - assuming connection - aFreeSocket=1

sckListen(aFreeSocket).RemoteHostIP

'*****

Here is where I get the empty string, since it is not returning the RemoteHostIP until the connection has been accepted...

'before the connection has been accepted
Debug.Print "The following connection " & sckListen(aFreeSocket).RemoteHostIP & " is attempting to connect." 'returns empty string

'after the connection has been accepted
sckListen(aFreeSocket).accept RequestID
Debug.Print "The following connection " & sckListen(aFreeSocket).RemoteHostIP & " has been established." 'returns the IP successfully

So what I'm trying to figure out is who is wanting to establish the connection before allowing/denying them.

thanks,
chisco



0
 

Author Comment

by:chisco
ID: 11810314
Hi again,

da... I read so fast your answer that did not realize to use sckListen instead of sckAccept..

guess is monday today.. :)

thanks Idle Mind.. it works...
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question