Solved

WINSOCK - CONNECTIONREQUEST HELP

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 70
vb6 connector to SQL Server 2 42
Excel Macro update each line based on another worksheet 2 58
SQL Server 2012 to SQL Server 2016 24 56
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 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