Transmitting Remote IP through Winsock Control


I have a Master/Slave application that I am writing.  Currently the Master application needs the user to manually enter the IP addresses of Slave PC's.  I would like to automate this process, so that the Slave sends it's IP to the Master.  Following this, the Client will wait for the Master to respond.

Here is what I have so far:

Private Sub Form_Load()
    'Determine the IP address of the Client Machine
    'and send this information to the Master Machine / Application
    Dim ThisIP As String
    ThisIP = Me.tcpClient.LocalIP
    tcpClient.SendData (ThisIP)
    'Set the LocalPort proplerty to an integer
    'Then invoke the Listen method.
    tcpClient.LocalPort = 1001
End Sub

Where I am having problems... tcpClient.SendData (ThisIP)

My question:

1.  Why am I getting errors (wrong type of declaration)?
2.  How can I accomplish this task?



P.S.  Here is the error that I am getting:

Run time error 40006
Wrong protocol or connection state for the requested transaction or request.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You are getting an error becuase the winsock control isn't connected to anything yet.  Also, you said you wanted the slaves to send to the server but the code you have shown is putting the winsock control into a Listening state which suggests its the server.

An easier method is to simply query the RemoteHostIP property on the server side after you accept a connection.  It will tell you the IP address of the machine that just connected.  Here is what I typically use in my winsock server ConnectionRequest() and Close() events:

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim acceptIndex As Integer
    Dim curIndex As Integer
    ' See if there is a winsock already loaded
    acceptIndex = -1
    If Index = 0 Then
        For curIndex = 1 To Winsock1.UBound
            If Winsock1(curIndex).State = sckClosed Then
                acceptIndex = curIndex
                Exit For
            End If
        Next curIndex
    End If
    ' Load a winsock  if none available
    If acceptIndex = -1 Then
        acceptIndex = Winsock1.Count
        Load Winsock1(acceptIndex)
        ReDim Preserve inputBuffer(acceptIndex)
        inputBuffer(acceptIndex) = ""
    End If
    ' Accept connection on index decided upon
    Winsock1(acceptIndex).LocalPort = 0
    Winsock1(acceptIndex).Accept requestID
    Dim RemoteIP As String
    RemoteIP = Winsock1(acceptIndex).RemoteHostIP                ' <-------------------
    ' Increase number of Users Connected
    users = users + 1
    Me.Caption = "Server : " & users & " User(s)"
End Sub

Private Sub Winsock1_Close(Index As Integer)
    Dim curIndex As Integer
    ' Decrease Total number of users
    users = users - 1
    Me.Caption = "Server : " & users & " User(s)"
    ' index has disconnected
    ' attempt to release unneeded Winsock Controls from end of array
    Do While Winsock1(Winsock1.UBound).State = sckClosed
        Unload Winsock1(Winsock1.UBound)
    ReDim Preserve inputBuffer(Winsock1.UBound)
End Sub
jbcase01Author Commented:
Idle Mind,

Thanks for the info.  Yes you are right... the client suggests that it is acting as the Master.  In fact, that is exactly how it is working.

Here is why:

Once the 'Client' app. is installed on the remote machine, a registry key is set to make it execute on start up.  After start up, the Slave will wait for the Master to dish out work, but the Master has to know it's IP address.  The Master App. will not be running all of the time.  Basically, what I am trying to do is develop a 'work pool' of IP addresses, such that when the Master app is run, it will know where to send the work (a few seconds after program start).

I guess an easier way to say this would be, How can I get the client to broadcast it's IP to the Master?  I am sure that this is not the smartest or safest way to do things, but it will get the job done.  Security comes later.

I will try to modify my code and use what you have suggested.

As usual, any additional comments or suggestions are appreciated.


Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Ok.  I see what you are tyring to accomplish here.  First, a bit on communication models.

The Master app wants to use a "Push Model", where as a producer, it delivers some kind of content to a consumer (usually on a predefined interval) without the consumer needing to manually request that content each time.  This is how a magazine subscription works.

The "Pull Model" on the other hand, only delivers content when the consumer actually requests it, such as when you order a pizza. (yummy...)

You have stated that both the Master and Client will be not be on all the time, so neither model will work very well.  In this case, you can use a hybrid of the two.

It sounds like you will know how many clients there are and just won't know there IP address so you can send their workload.  So what the Master can do is split the workload between the clients and have it either staged for download in a specific folder such as in an FTP app, or simply assign the jobs to the different clients in a database so as each client connects it can query the db and download its assignments.  (I don't know exactly what you are doing so I"m just speculating here.)

In my opinion, it is better for the client to pull the data from master since it will most likely be in a more controlled environment where you can say with more certainty, "The master will be up between 6 and 8 am central time for clients to connect and receive workloads."  The clients will all turn on their computers at different times.

I used a similar model in a previous job where employees across the nation connected to an FTP server with their own logons to download their workloads for the day.  The master app in this case, was always on and received work from customers, who were also around the nation.  As it received work, it would assign it to the pool of employees based on the type of work and place it in their FTP folder.  The employees would connect each morning and download their work.  When they were done it would be FTP'ed back up to the server where the master could then process it and send it back to the original customer.

So in summary, I think your Master should not push at all.  It should assign the work and then simply wait for the clients to connect and download it.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.