Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


Transmitting Remote IP through Winsock Control

Posted on 2004-10-13
Medium Priority
Last Modified: 2013-12-26

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.
Question by:jbcase01
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
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12305266
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

Author Comment

ID: 12307317
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.


LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 12308621
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.


Featured Post

Industry Leaders: 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 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…
Suggested Courses

647 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