c# - how to create tcpclient to unknown ip address -
i using array of tcpclient, have set different devices tcp server , c# gui acts client of them. know each address ip of each device in network can set each tcp client without problem. want gui can recognize server without set manually ip addresses. how can it? thought @ first local ip address of pc base network(done) , thought declare array of tcpclient try cennect possible ip address need lot of time.
public void getlocalipaddress() // local ip of 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() { (int k = 2; k < 254; k++ ) // intialize tab possible ip addresses { myhostname[k] = byte0 + "." + byte1 + "." + byte2 + "."+k; try { myclient[k] = new tcpclient(myhostname[k], portnum); } catch { messagebox.show("executed here"); } } }
how create tcpclient unknown ip address?
i think can't that.
well want gui can recognize server without set manually ip addresses
also, don't think possible. of course can generate addresses - if want know every client awaiting connection on specific port in network... have try-connect every single 1 of them (on port of course).
if want check if address active (not specific tcp port) can use ping , parse output. still need try connecting every computer.
the method calculate possible ipv4 addresses suggested works if network mask 255.255.255.0 (/24), if have other mask, fail. there several ways of calculating hosts in subnet. please try 1 of mentioned here, here or here. hope helps :)
edit: here: how ip of hosts in lan? have nice solution cutting down ping response time can test connection 255 computer in a second :)
Comments
Post a Comment