Solved

WINSOCK - CONNECTIONREQUEST HELP

Posted on 2004-08-16
3
842 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month6 days, 17 hours left to enroll

622 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