Using the Systems IP Address

John-Boy1

Member
Joined
Mar 6, 2006
Messages
14
Programming Experience
Beginner
At present im using System.Net.IPAddress to display the IP address and port number in seperate labels on the form. Instead of manually coding the IP address I want the system to dynamically take it off the computer.

So this means if the programme is loaded on another machine instead of having to change the code this will automatically be done.

VB.NET:
[SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] portNumber [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 8080
Label13.Text = portNumber
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] localAdd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.IPAddress = System.Net.IPAddress.Parse("###.###.#.#")
Label12.Text = localAdd.ToString
[/SIZE]

Hope this make sense and appreciate any suggestions
 
To retrieve the current machines IP Address...

VB.NET:
Imports System.Net
 
// Get the hostname 
Dim MyHost as string = System.Net.Dns.GethostName()
 
// Get the IP from the host name
 
Dim MyIp as string = System.Net.Dns.GetHostByName(myHost).AddressList(0).ToString()
 
// Show the IP 
 
MessageBox.Show(myIP)
 
Thankyou for the reply.

Unfortunatley I am having a problem incorporating the code you have given me with the TCP Listner.

The code used before:
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net.Sockets[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net.IPAddress[/COLOR]
[/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] localAdd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.IPAddress = System.Net.IPAddress.Parse("###.###.#.#")
Label12.Text = localAdd.ToString
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tcpListener [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] TcpListener(localAdd, portNumber)
tcpListener.Start()
[/SIZE]

Unfortunately when I incorporate your code
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net.Sockets[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net.IPAddress[/COLOR]
[/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyHost [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = System.Net.Dns.GetHostName()
[/SIZE][SIZE=2][COLOR=#008000]'Get the IP from the host name
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyIp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = System.Net.Dns.GetHostByName(MyHost).AddressList(0).ToString()
Label12.Text = MyIp
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tcpListener [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] TcpListener(MyIp, portNumber)
tcpListener.Start()
[/SIZE]

The MyIp is underlined in the TCP Listner and says "value of type 'String' can not be converted to 'System.Net.IPAddress'

Is there anyway I can get around this?

Many Thanks
 
Items in AddressList is already System.Net.IPAddress class instances
VB.NET:
Dim MyIp As IPAddress = System.Net.Dns.GetHostByName(MyHost).AddressList(0)
Dim tcpListener As New TcpListener(MyIp, portNumber)


 
Back
Top