I'm working at a server chat application and I need to get the ip of the computer that's running the server app.
Because you didn't specify if you want an IPv4 or an IPv6 address:
Imports System.Net.NetworkInformation
Module Module1
Sub Main()
Dim networkInterfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each networkInterface As NetworkInterface In networkInterfaces
For Each unicastAddress As UnicastIPAddressInformation In networkInterface.GetIPProperties().UnicastAddresses
Console.WriteLine(unicastAddress.Address)
Next
Next
Console.ReadLine()
End Sub
End Module
As you iterate through the available network interfaces on the computer, there should be something you would know that would identify the one that you want.
Open cmd and type ipconfig. Now look for IPv4
Related
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.
I just adquire a cash drawer, connected with a rj11 port to my computer. When i ask about to how to open in my application, they said me to just send a sequence characters at less than 1200 bps to port com5 , beacuse the rj11 is configured to respond to that. How can i send it? I already try this but nothing happens
Using COM As System.IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort("COM5")
COM.WriteLine("Enviodeprueba")
End Using
If you know those are the correct baud rate and port number, set those for the serial port first:
Dim COM As System.IO.Ports.SerialPort = New System.IO.Ports.SerialPort
With COM
.PortName = "COM5"
.BaudRate = "1100" 'as you stated it has to be less than 1200
.DataBits = 8
'and whatever other properties you want to set for the port here
End With
Then you have to open the port:
COM.Open()
THEN you should be able to send info to it:
COM.WriteLine("Enviodeprueba")
And be sure to close the port when you are done with it:
COM.Close()
This is the MSDN article about WriteLine for a port.
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.
Using VB.NET how do I connect to an available wireless network. I have been able to list all the available networks.
Assuming you are wanting to control the Windows biult-in wifi stack, you should be able to do it with the WlanConnect Function. A signature is availeble at pinvoke.net.
MSDN has a list of the articles pertaining to wifi here.
The MSDN page does not say whether this is the case, but an application might need elevated permissions to use this API...
If you have the WLAN profile saved in your PC, this approach is simple.
Sub connectTo(ByVal name As String)
Dim p = "netsh.exe"
Dim sInfo As New ProcessStartInfo(p, "wlan connect " & name)
sInfo.CreateNoWindow = True
sInfo.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(sInfo)
End Sub
'use the sub to connect to your AP. connectTo("myAP")
Otherwise, it is easier to use ManagedWifi or SimpleWifi dll libraries. Here is my code where I used SimpleWifi.dll to connect to a network with a passkey.
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.