Currently I use the following code to retrieve the IP address of the local workstation...
strIPAddress = System.Net.Dns.GetHostEntry(strComputerName).AddressList(0).ToString()
This is fine for the Windows XP workstations. However, in Vista and Windows 7, this returns the IPv6 address which is not used at all. Is there a method of setting this to work so it always returns the IPv4 address regardless of platform?
I know I can increment the AddressList value to 1 and get the correct IP in Windows 7. The bad part is that this requires going through the motions of identifying the OS and choosing one or the other.
The must be some way of specifying IPv4 only. Perhaps getting a result from DNS on the network rather than the workstation itself?
You just need to loop through the AddressList looking at the AddressFamily seeing which is set to InterNetwork
Dim IP4 = New List(Of IPAddress)(Dns.GetHostEntry(strComputer).AddressList).Find(Function(f) f.AddressFamily = Sockets.AddressFamily.InterNetwork)
Or the longer way:
Dim IP4 As IPAddress
Dim AL = Dns.GetHostEntry(strComputer).AddressList
For Each A In AL
If A.AddressFamily = Sockets.AddressFamily.InterNetwork Then
IP4 = A
Exit For
End If
Next
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.
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!
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