Get certain text from a webpage [VB.NET] - vb.net

What I'm looking to acquire is ip addresses from a webpage which contains the information.
Example page: http://www.game-monitor.com
I'm basically looking how to make vb.net visit that webpage and save the IP Addresses it gets from that webpage.
Thanks!

Check the System.Net.NetworkInformation for the Ping method. You can ping the hostname and return the IP to a variable.
Dim ping as Ping = New Ping()
Dim pingReply as PingReply = ping.send("www.game-monitor.com")
Dim ip as String = PingReply.Address.ToString()
Edit, you might want to add a try catch in case the ping doesn't get a reply.

Related

How to use IP addresses to populate a webbrowser control

I have a listbox containing IP addresses - I want to select one from that list and load the corresponding web page. For some reason it doesn't treat the IP as a valid parameter.
' LBPrinters contains a list of IP addresses i.e. 192.168.101.45
Dim url As String = Trim(LBPrinters.SelectedItem.ToString)
Try
WebBrowser1.Navigate(url)
Catch
MessageBox.Show("Exception: Did not work")
End Try

Get the IP and Computer Name

I have this code:
localIp = Request.UserHostName
hostName = DetermineCompName(localIp)
Session.Add("localIp", localIp)
Session.Add("hostName", hostName)
As you can see, I put the 2 variables on a session so that I can use it when I want. Testing the app on 10 computers, I saw that on some of the computers it gets the Client IP and the Computer name, but on others it goes empty.
As in some computers it works, I don't understand what's wrong. Does anyone have the right method to do this?
To get the computer name you can simply do:
Dim hostName As String = Environment.MachineName
or:
Dim hostName As String = My.Computer.Name
For the IP it's a little bit trickier, I assume you want the ipV4, so you can try this:
Dim localIp As String
For Each address As System.Net.IPAddress In System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList
If address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
localIp = address.ToString()
Exit For
End If
Next
Please note that if you do just:
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()
then this will return the ipV6.

TCP/IP Messaging Application VB.net - IP Address / Port-Forwarding Questions

I am developing a piece of scientific software in VB.net that I intend to distribute. Part of the application will involve communication with a server (and other clients). For all intents and purposes the communication will be like a chat application.
I have developed the software using sockets, and it works fine when testing at home on my local network. Below is basically how I send messages:
Dim serverIPString as String = "192.168.1.5"
Dim serverPort as long = 65500
dim messageString as String = "Hello"
Dim Client As New System.Net.Sockets.TcpClient(serverIPString, serverPort)
Dim Writer As System.IO.StreamWriter(Client.GetStream())
Writer.Write(messageString)
Writer.Flush()
Writer.Close()
And this is how I listen:
Public Class TCPIPListener
Dim initalised As Boolean = False
Dim port As Long
Dim IP As System.Net.IPAddress
Dim ourListener As TcpListener
Dim ourClient As TcpClient
Dim ourMessage As String
'''''''''''
'' ctors ''
'''''''''''
Public Sub New()
initalised = False
End Sub
'Takes IP and port and starts listeing
Public Sub New(inIP As String, inPort As Long, inExchange As messageExchange)
'Try to set the IP
Try
IP = System.Net.IPAddress.Parse(inIP)
Catch ex As Exception
Exit Sub
End Try
'Set the port
port = inPort
initalised = startListener()
End Sub
'''''''''''''''
'' Listening ''
'''''''''''''''
Private Sub Listening()
ourListener.Start()
End Sub
'starts listener
Public Function startListener() As Boolean
ourListener = New TcpListener(IP, port)
ourClient = New TcpClient()
Dim ListenerThread As New Thread(New ThreadStart(AddressOf Listening))
ListenerThread.Start()
initalised = True
Return True
End Function
'Called from a timer on the form
Public Function tick() As Boolean
If Not initalised Then
Return False
End If
If ourListener.Pending = True Then
ourMessage = ""
ourClient = ourListener.AcceptTcpClient()
Dim Reader As New System.IO.StreamReader(ourClient.GetStream())
While Reader.Peek > -1
ourMessage = ourMessage + Convert.ToChar(Reader.Read()).ToString
End While
messagebox(ourMessage)
End If
Return True
End Function
End Class
Using this approach, every client will be listening for messages sent from the server, and any messages sent will go to the server and be directed to the relevant client - it's a little inefficient but necessary for the way my software works.
The problem is that I am having real trouble getting this to work across the internet. If I send a message to a local IP address it works fine, even from the debugger. If I use a external IP address I get the following:
No connection could be made because the target machine actively refused it XXX.XXX.XXX.XXX:65500"
I have turned on port forwarding with my router and set up the correct port (65500 here) to forward to my PC running the lister (192.168.1.5) for both TCP and UDP. This website it tells me this port is open, and those either side are not.
If I right click on the exe and run as administrator, my antivirus pops up and says it is doing something suspicious when I start the listener (which I guess if anything is encouraging). If I then add my application to the list of exceptions and run it again, then check to see if the port is listening (using this) I find my application listening on the correct port (if I stop the application it is gone). However the client application still gives the same error above. If I use a random IP address rather than the one of the listener I get a different error:
{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond XXX.XXX.XXX.XXX:65500"}
I suspect I am not taking the correct approach to this. I am fine with most aspects of VB, but have not really tackled this sort of problem before. When it comes to distributing software I really don't want my customers to have to mess around with deep settings to get this application to work. Any help will be immensely useful to me!
Another problem I guess I will face in the future is if multiple clients have the same external IP but different internal IPs (I intend to distribute to universities). Under these circumstances I guess port forwarding will not work.
Thanks very much in advance (and apologies for the long post)!
If you set your server to listen for connections on 192.168.1.5 then it will only allow connections from that specific IP address. The address you give a TcpListener is the IP address which you will accept connections from.
Since the 192.168.1.5 address is internal no one outside your network will be able to connect to your server. Make your server listen to 0.0.0.0 (or the equivalent IPAddress.Any) instead to make it allow connections from anyone.

Getting an active IP address from a list of IP address in VB.net

I am stuck while getting ip4 address from my computer with 2 network card and 1 wifi card.
The code i am using to get IP address is this :
'Get Network Information
hostName = System.Net.Dns.GetHostName()
Dim hostEntry = Dns.GetHostEntry(hostName)
Dim addressList As New List(Of IPAddress)
For Each address In hostEntry.AddressList
If address.AddressFamily = Sockets.AddressFamily.InterNetwork Then
addressList.Add(address)
End If
Next
The problem is, if i used this code to get the ip address into a text box, it didn't always show the ip address my computer is currently active :
teIP4.Text = addressList(0).ToString
Edit : I am trying to connect to SQL Server via LAN but still in a and inserting my ip address to the database for further use, so i need to get the active ip address.
Is there anyway to specify which IP Address is currently active from code? Thanks!

Get IP Address of Remote Client with No DNS Entry in VB.Net

I have a Windows forms application running on a terminal server. I need to determine the IP addresses of each client machine.
I found a way to retreive the IP address for computers with DNS entries (example below), but several of my thin clients were set up with static IPs and have no DNS name. Is there a way to determine the IP address of a remote client without having a DNS name?
Dim clientName As String = My.Computer.Network.ClientName
Dim IPHost As Net.IPHostEntry = Net.Dns.Resolve(clientName & "domain.com")
Dim addresses As Net.IPAddress() = IPHost.AddressList
fullIP = addresses(0).ToString()
To get the primary IP Address, you can use:
System.Net.Dns.GetHostEntry("").AddressList(0).ToString
This may return an IP6 address, in which case you can try to find the IP4 using:
Dim ipentry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")
For i As Integer = 0 To ipentry.AddressList.Count - 1
MsgBox(System.Net.Dns.GetHostEntry("").AddressList(i).ToString)
Next