Results 1 to 10 of 10

Thread: Storing A Username and IP address in an array

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    56

    Exclamation Storing A Username and IP address in an array

    I am working on a chat program, and have the UDP server set up, but I need a way (unlike mine) that can store, retrieve, and delete usernames and IP addresses from an array. The array is named

    IPList

    and has the dimensions
    (1 to 2, 1 to 100000)

    1 is the Username, 2 is the IP address.

    My current way can store one username, but has a problem with the second one. It seems to erase both of them. If you could give me a new algorithm, I would really appreciate it.

    And then, with BSChat working, I shall take over the world...er...not.

    -brian728s
    [email protected]

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Can you post the code ur using to write to the Array.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    56
    Public IPList(1 To 2, 1 To 100000) As String

    Public Function FindIndex(Search As String) As Double
    Dim i As Double
    FindIndex = -1
    For i = 1 To 100000
    If IPList(1, i) = Search Then
    FindIndex = i
    Exit Function
    End If
    Next i
    End Function

    Public Function NewUser(Name As String, IP As String) As Boolean
    Dim a As Double
    Dim Name2 As String
    Name2 = Replace(Name, Chr(1), "")
    a = FindBlankIndex()
    If FindIndex(Name2) = -1 Then
    a = FindBlankIndex
    IPList(1, a) = Name
    IPList(2, a) = IP
    NewUser = True
    Else
    NewUser = False
    End If
    End Function

    Public Sub LogOut(Name As String)
    Dim a As Double
    a = FindIndex(Name)
    If a <> -1 Then
    IPList(1, a) = ""
    IPList(2, a) = ""
    End If
    End Sub

    Public Function FindBlankIndex() As Double
    FindBlankIndex = -1
    Dim i As Double
    For i = 1 To 100000
    If IPList(1, i) = "" Then
    FindBlankIndex = i
    Exit Function
    End If
    Next i
    End Function

    FindBlankIndex Works and returns the index of the first blank record in IP list. Findindex works as far as I know. NewUser and logout are the ones that are driving me insane. They apparantly can store and retrieve the first one, but don't work with any ones over that.

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Next time, paste your code between vbcode tags. It's easier to read. I'll paste his code again:
    VB Code:
    1. Public Function FindIndex(Search As String) As Double
    2.     Dim i As Double
    3.     FindIndex = -1
    4.     For i = 1 To 100000
    5.         If IPList(1, i) = Search Then
    6.             FindIndex = i
    7.             Exit Function
    8.         End If
    9.     Next i
    10. End Function
    11.  
    12. Public Function NewUser(Name As String, IP As String) As Boolean
    13.     Dim a As Double
    14.     Dim Name2 As String
    15.     Name2 = Replace(Name, Chr(1), "")
    16.     a = FindBlankIndex()
    17.     If FindIndex(Name2) = -1 Then
    18.         a = FindBlankIndex
    19.         IPList(1, a) = Name
    20.         IPList(2, a) = IP
    21.         NewUser = True
    22.     Else
    23.         NewUser = False
    24.     End If
    25. End Function
    26.  
    27. Public Sub LogOut(Name As String)
    28.     Dim a As Double
    29.     a = FindIndex(Name)
    30.     If a <> -1 Then
    31.         IPList(1, a) = ""
    32.         IPList(2, a) = ""
    33.     End If
    34. End Sub
    35.  
    36. Public Function FindBlankIndex() As Double
    37.     FindBlankIndex = -1
    38.     Dim i As Double
    39.     For i = 1 To 100000
    40.         If IPList(1, i) = "" Then
    41.             FindBlankIndex = i
    42.             Exit Function
    43.         End If
    44.     Next i
    45. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I would redim the array un run-time as needed, if you like to improve it.

    Try this code, I remove the FindBlankIndex function... and didn't test it so I don't know if it works.
    VB Code:
    1. Public Function FindIndex(Search As String) As Double
    2.     Dim i As Double
    3.     FindIndex = -1
    4.     For i = 1 To 100000
    5.         If IPList(1, i) = Search Or IPList(1, i) = "" Then
    6.             FindIndex = i
    7.             Exit Function
    8.         End If
    9.     Next i
    10. End Function
    11.  
    12. Public Function NewUser(Name As String, IP As String) As Boolean
    13.     Dim a As Double
    14.     Dim Name2 As String
    15.     Name2 = Replace(Name, Chr(1), "")
    16.  
    17.     a = FindIndex(Name2)
    18.  
    19.     If IPList(1, a) = "" Then        
    20.         IPList(1, a) = Name
    21.         IPList(2, a) = IP
    22.         NewUser = True
    23.     Else
    24.         NewUser = False
    25.     End If
    26. End Function
    27.  
    28. Public Sub LogOut(Name As String)
    29.     Dim a As Double
    30.     a = FindIndex(Name)
    31.     If IPList(1, a) <> "" Then
    32.         IPList(1, a) = ""
    33.         IPList(2, a) = ""
    34.     End If
    35. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    FYI, if these are username/ip pairs, then IMHO this would be MUCH easier than a matrix:
    VB Code:
    1. Public Type UsernameIPPair
    2.     Username As String
    3.     IP As String
    4. End Type
    5.  
    6. Dim Users(0 To SomeBigNumber) As UsernameIPPair
    7. Msgbox Users(0).Username
    8. Msgbox Users(x).IP
    9. .
    10. .
    11. .

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I know that... but maybe that means to change a lot of code. I didn't mean to change it that much.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8
    True, I'm just saying for future reference...one of my programs uses a matrix and it makes it really hard to code

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    56
    Thank you! I used type once before, but couldn't remember it for this one. I will post a link to my website with a free download once I get it working.

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Beware!!

    This is a correction to the code
    VB Code:
    1. Public Function NewUser(Name As String, IP As String) As Boolean
    2.     Dim a As Double
    3.     Dim Name2 As String
    4.     Name2 = Replace(Name, Chr(1), "")
    5.  
    6.     a = FindIndex(Name2)
    7.  
    8.     [b]If a > 0 Then[/b]
    9.         If IPList(1, a) = "" Then        
    10.             IPList(1, a) = Name
    11.             IPList(2, a) = IP
    12.             NewUser = True
    13.         Else
    14.             NewUser = False
    15.         End If
    16.     [b]Else
    17.         MsgBox "Sorry, the list is full"
    18.     End If[/b]
    19. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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