Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I did this book assignment for class, however I like to go in and do things outside the course structure afterwards. So my idea is to add in a feature where the user inputs an IP of his/her choice. I already know how I'm going to add in the module, However I don't Know what data type I would assign to the variable for the IP Address.

What I have tried:

VB
Module Module1
    Sub Main()
        Dim pingCounter As Integer = 0
        pingCounter = generateRandom(pingCounter)
        Console.WriteLine("The server was pinged " & pingCounter & " time(s).")
        Console.WriteLine("Press Any key to exit...")
        Console.ReadKey()
    End Sub
End Module
Module RNG
    Function generateRandom(ByRef pingCounter As Integer) As Integer
        Dim RN As Integer = 0
        Dim Count As Integer = 0
        For Count = 1 To 200
            RN = Int(Rnd() * 20 + 1)
            If RN <> 5 Then
                Console.WriteLine("Random number was " & RN & " this was not the target number.")
                Console.WriteLine("Re-rolling in 1000 ms...")
            ElseIf RN = 5 Then
                pingMe()
                pingCounter = pingCounter + 1
            End If
            System.Threading.Thread.Sleep(1000)
        Next
        Return pingCounter
    End Function
End Module
Module Ping
    Sub pingMe()
        Shell("Ping.exe 127.0.0.1", , True)
        Console.Out.WriteLine(" ")
    End Sub
End Module
Posted
Updated 2-Mar-16 8:21am
Comments
ZurdoDev 2-Mar-16 14:14pm    
String is fine, isn't it?
cigwork 2-Mar-16 14:31pm    
Depends how "clever" you want to be and what want to do with the address.

You could use System.Net.IPaddress if you're working with .Net

Or if there isn't already an IPAddress class in the libraries you have access to you could create your own IPAddress class with all sorts of "useful" features like construction from string, validation of entered string, splitting the string into octets, various formatted string representation (hex or octal or ...) or whatever else you think might be fun.

Or you could just use a string as previously suggested and trust the user to enter a sane address. It all depends how robust you want your solution to be.
Member 12312671 2-Mar-16 15:20pm    
Thanks guys :)

1 solution

Use string - it copes with IPV4 and IPV6, which are very different in format.
IP address - Wikipedia, the free encyclopedia[^]
Alternatively, there is the IPAddress class[^] which will hold either. However, if you want to store them anywhere, you will have to convert them to strings anyway!
 
Share this answer
 
Comments
Member 12312671 4-Mar-16 13:41pm    
Thanks griff i notice you answer my questions a lot :)
OriginalGriff 4-Mar-16 14:48pm    
:laugh:
I hadn't noticed!
It's probably because you have member number rather than a name, so it just isn't memorable.
Have you considered changing it? It's pretty easy to do, if you can find a free name of nickname. Just go to your profile page and it should be pretty obvious.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900