0

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");
        }

    }
}
Simon Karlsson
  • 4,090
  • 22
  • 39

2 Answers2

0

How to create tcpclient to unknown ip address?

I think you can't do that.

Well now i want that the GUI can recognize all the Server without that i set manually the ip addresses

Also, I don't think it is possible. Of course you can generate this addresses - if you want to know every client awaiting connection on specific port in your network... you have to try-connect to every single one of them (on this port of course).

If you only want to check if the address is active (not specific TCP port) you can use ping and parse the output. But you will still need to try connecting to every computer.

The method to calculate all possible IPv4 addresses you suggested works only if network mask is 255.255.255.0 (/24), but if you have other mask, it will fail. There are several ways of calculating hosts in subnet. Please try one of mentioned here, here or here. Hope it helps :)

EDIT: Here: How to get IP of all hosts in LAN? You have a very nice solution with cutting down ping response time so you can test connection to 255 computer in a a second :)

Community
  • 1
  • 1
badsamaritan
  • 407
  • 3
  • 9
0

As @badsamaritan said, there is no way to send data to a destination with unknown address. You also cannot get your mail delivered if the mail doesn't have your address written in it.

However you could use some tricks to achieve your goal:

  1. trial and error. This means you try to connect to every possible IP address, and that's much more than your subnet. If you want to be able to use this program in every network, you must consider that the network may use routers, that connect different subnets. In your own home LAN, the situation is likely easier. In both cases it's basically like an IP scan, time consuming and prone to errors.

  2. send an UDP broadcast from your client. The data contains your client's IP address. The servers reply to your client via UDP with their own IP. Then you can establish the TCP connections to the servers as usual. However, there are also difficulties:

    • servers will not respond if they are down. If you don't know the server count, you will simply miss them.
    • some routers don't forward broadcasts.
  3. use a completely different communication protocol than TCP/IP or UDP/IP. If that protocol doesn't have an IP address, then you don't need to know it. There are other protocols, but TCP/IP is the de-facto standard. Don't forget: your OS needs to support it.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45