Solved

WINSOCK - CONNECTIONREQUEST HELP

Posted on 2004-08-16
3
838 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 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…

735 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