Solved

Transmitting Remote IP through Winsock Control

Posted on 2004-10-13
3
269 Views
Last Modified: 2013-12-26
Hi,

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
    tcpClient.Listen
   
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?

Thanks,

B.

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.
0
Comment
Question by:jbcase01
  • 2
3 Comments
 
LVL 85

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)
    Else
        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
    Winsock1(Index).Close
   
    ' attempt to release unneeded Winsock Controls from end of array
    Do While Winsock1(Winsock1.UBound).State = sckClosed
        Unload Winsock1(Winsock1.UBound)
    Loop
    ReDim Preserve inputBuffer(Winsock1.UBound)
End Sub
0
 

Author Comment

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

Thanks,

B.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 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.

~IM
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now