Solved

WINSOCK - CONNECTIONREQUEST HELP

Posted on 2004-08-16
3
832 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now