i am using an Array of tcpclient, i have set different devices as tcp Server and my c# Gui Acts as a Client to all of them. i know each address ip of each device in the Network so that i can set each tcp Client without Problem. Well now i want that the GUI can recognize all the Server without that i set manually the ip addresses. How can i do it? i thought at first to get the local ip address of pc so that i get the base Network(done) and then i thought to declare an Array of tcpclient which try to cennect to all the possible ip address but that Need a lot of time.
public void GetLocalIPAddress() // get the local ip of the pc
{
IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address => address.AddressFamily == AddressFamily.InterNetwork).First();
textBox2.Text = ip.ToString();
string aux = ip.ToString();
int num = aux.IndexOf(".");
byte0 = aux.Substring(0,num);
aux = aux.Substring(num + 1);
num = aux.IndexOf(".");
byte1 = aux.Substring(0, num);
aux = aux.Substring(num + 1);
num = aux.IndexOf(".");
byte2 = aux.Substring(0, num);
aux = aux.Substring(num + 1);
// if ip Address = 192.168.1.156 ==> byte0 =192 / byte1=168/ byte2=1
}
private void GetConnectedSensoren()
{
for (int k = 2; k < 254; k++ ) // intialize the tab with all the possible ip Addresses
{
myhostName[k] = byte0 + "." + byte1 + "." + byte2 + "."+k;
try
{
myclient[k] = new TcpClient(myhostName[k], portNum);
}
catch
{
MessageBox.Show("executed here");
}
}
}