Results 1 to 13 of 13

Thread: [RESOLVED] Winsock Private IP problem

  1. #1

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Resolved [RESOLVED] Winsock Private IP problem

    Hey Guys,
    I've run into a problem that I don't know how to fix.

    I have a program that grabs the private IP address for the computer using the Winsock control.

    I have it check to see if it has changed and compare to what it was, etc..

    Well, if the ip address changes via DHCP renewal, the program sees it just fine everytime it changes. But if I statically assign an IP address, the program only recognizes the change one time. Even though its the same code.

    Here is the simplified version of what I'm doing

    Code:
    Private Sub Refresh_All()
    
    If WS1.State <> sckClosed Then 
       WS1.Close
    End If
    
    strNewLocalIP = WS1.LocalIP
    
    If strNewLocalIP <> strOldLocalIP Then
    ' do a few updates to database and send email (this works perfectly)
    End If
    
    WS1.Close
    
    End Sub
    Yes, thats the very simplified version and more or less pseudocode for what I'm doing.

    This and the rest of the code are all stored in the Sub. I call the sub when the program loads, and then on a specified timer when it gets to 0. The user can set whether they want it to check on startup. But regardless whether they do or wait for the automatic refresh, it calls the same Sub.

    But, if I statically assign the IP address, it only notices the change the first time it runs the sub. Every subsequent attempt doesn't do anything. It's like it doesn't recognize that it has changed.

    So, if they have it set to check for changes on startup, when the program loads, it sees the change right away. But anytime it checks after that while its still running, it doesn't notice.
    But, if they have it wait until the first refresh to check, it notices during the refresh, but not any other time after that.


    I hope that makes enough sense to get the help I need

    Thanks in advance.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  2. #2
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Winsock Private IP problem

    I Edited the Code and the zip
    Last edited by 5ms?; Dec 1st, 2009 at 05:23 PM.

  3. #3

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Winsock Private IP problem

    Thanks for your help, but it is doing the same thing with your code that it does with mine.

    If I have your program running, then I go in and manually assign a static IP address, then click the command button again, it doesn't recognize that it has changed. But if I re-run the program, it sees it again.

    To me, it seems that Winsock only recognizes the change 1 time. After that, it requires a restart of the whole app to see it again. Which, doesn't make any sense since it is reinitialized every time it is refreshed.

    Anybody else have any ideas?

    Thanks
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  4. #4
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Winsock Private IP problem

    Here a
    has my IP changed zip
    Here is the Code, add Winsock1, and your good to go.
    Set Winsock1 Index to 0, then run this
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
     Me.Caption = IPchanged
    End Sub
    
    Public Function IPchanged() As Boolean
     Static OldIP
      Load Winsock1(1)
     IPchanged = OldIP <> Winsock1(1).LocalIP
     OldIP = Winsock1(1).LocalIP
     Unload Winsock1(1)
    End Function
    
    Private Sub Form_Load()
     Dim InitializeIPcheck
     'InitializeIPcheck = IPchanged
    End Sub
    Attached Files Attached Files
    Last edited by 5ms?; Dec 1st, 2009 at 05:27 PM.

  5. #5

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Winsock Private IP problem

    On initial testing, it seems to be working or at least it is recognizing when I change it.

    Would you mind explaining why this is working? I want to make sure I fully understand what is going on in your code before I try to implement it.

    Thank You
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  6. #6
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Winsock Private IP problem

    It's loading a new Winsock1, when Winsock Loads it gets your IP "I think".

    Run this
    Code:
    Public Function IPchanged() As Boolean
     Static OldIP
      Load Winsock1(1)
     IPchanged = OldIP <> Winsock1(1).LocalIP
     OldIP = Winsock1(1).LocalIP
     Debug.Print OldIP ; " ; ";  Winsock1(0).LocalIP
     Unload Winsock1(1)
    End Function
    Last edited by 5ms?; Dec 1st, 2009 at 12:10 PM.

  7. #7

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Winsock Private IP problem

    I got that much, but what does changing the index to 0 at design time and then using a 1 as in index at run time do?

    Also, something to keep in mind, this program is designed to run for long periods of time, so it will most likely see multiple occurrences when the IP address changes.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  8. #8
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Winsock Private IP problem

    By changing the index to 0; thus the control is part of a control array.
    i.e Winsock1(0).

    In the Function IPchanged(), you are a loading a new Winsock1(),
    i.e Load Winsock1(1)

    and then Unloading it
    i.e Unload Winsock1(1)

  9. #9
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Winsock Private IP problem

    Did you run code in post#6

    Do note:
    By changing the index from (no index) to a index, i.e 0
    VB will add An Index if there is no code.

    i.e
    Code:
    Private Sub Winsock1_DataArrival( ByVal bytesTotal As Long)
    
    End Sub
    
    Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    
    End Sub
    Code:
    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    
    End Sub
    
    Private Sub Winsock1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    
    End Sub
    Last edited by 5ms?; Dec 1st, 2009 at 12:00 PM.

  10. #10

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Winsock Private IP problem

    I see.

    Ok, I've implemented the changes throughout my program and am doing testing on it now. So far, it seems to be working like it's supposed to.

    I will update this thread if I find any problems.

    Thank you very much for your help
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  11. #11
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Winsock Private IP problem

    Please Mark your Thread "Resolved", if the query is solved...

  12. #12

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Winsock Private IP problem

    Quote Originally Posted by 5ms? View Post
    Please Mark your Thread "Resolved", if the query is solved...

    I know, I wanted to make sure it was Resolved before I did.

    I was able to get it to work just as it should, so Thank You for your help.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  13. #13
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] Winsock Private IP problem

    You're welcome

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width