Hello, I have multiple networks with a changing amount of devices on each. What I'm trying to do is have those devices report their ip address to a website where I can enter in their mac address and determine which ip address it's coming from so that I can control said device with my program. Basically I'm trying to figure out if I can do this from C#(that is, broadcast each device to the website). From there I would like to be able to use code to connect and control devices similar to:

           HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://website/ip.html");

            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";


            //stream for postvars
            Stream PostData = WebReq.GetRequestStream();
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            string parsed = _Answer.ReadToEnd();
            //used to get ip and port dynamically
            string[] parts = parsed.Split(':');
            if (parts.Length != 2)
            {
                //throw exception
            }
            string host = parts[0];
            string port = parts[1];
            string html;
            // obtain some arbitrary html....
            using (var client = new WebClient())
            {
                html = client.DownloadString("ip.html");
            }
            // use the html agility pack: http://www.codeplex.com/htmlagilitypack
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            StringBuilder sb = new StringBuilder();
            foreach (HtmlTextNode node in doc.DocumentNode.SelectNodes("//text()"))
            {
                sb.AppendLine(node.Text);
            }
            string final = sb.ToString();

            TcpClient ourMagicClient = new TcpClient();

            //entering the mac address will direct it to the device
            Console.WriteLine(sb.ToString());
            Console.ReadKey();

            ourMagicClient.Connect(host,Convert.ToInt32(port));
            NetworkStream ourStream = ourMagicClient.GetStream();

Recommended Answers

All 9 Replies

Use wmi to get the relevant info (mac address, ip address, etc) and send that info wherever you want to send it.

The above assumes that your program is running on each client pc.

ok, so will the fact that the devices i want to control are control relay boards have any effect on what you're suggesting?

Wmi is for computers running Windows. You need to provide more info about your devices and environment.

I'm using control relay boards with a wifi interface that will be networked into a router. From there I'd like to be able to setup a page that can control them from a remote network anywhere in the world on any OS. Currently I'm able to control them with wifi and a page through HTTPRequest that isn't mine - but I don't have access to that code. I'm sending byte arrays to control the devices. What I'm worried about is that the control relay boards might have software on them that's doing the actual broadcasting to the website(I'm not too sure about that). The wifi interface is from Roving Networks. I'm using Visual Studio 2012 to develop this. I've considered using Dynamic DNS, but I'm thinking that going this route would send commands to ALL the devices on the network and wouldn't differentiate between devices. This is about as much information as I have that I think might be relevant. I'm not sure what kind of lower programming is on the boards. I'm also able to use an HTTPRequest to send commands to a particular IP with a port forwarded, but it would be the same with the Dynamic DNS - I think it would just end up sending that same command to all the devices. Basically I'm looking at trying to force an external ip for each device behind a router or differentiate using MAC addressing. Sorry if I sound like a broken record or this wasn't enough information. That's where I'm at with it all(I'm lacking the information to do what I want it to do).

There's a networking component here as well as your software/programming question.

It's not clear, at least to me on how you control these devices today. Where is the existing web page you refer to?

Is your intention to run a web page on an internal network that you will access from the internet?

Do you know how this existing web page you speak of works? The devices currentLy report info to this page? There would have to be a web service of some sort then to allow the devices to communicate with the web server via this interface.

I don't know how this page works currently, but I do know that many devices work on this site from many different networks. I suppose my intention is to create the ability to do what the code below does, but be able to assign the byte array from a remote network to which ever device I want that happens to be on the same network(say 5 devices on the same network), retrieve the ip by using a MAC address, and ensure the commands are sent only to the specified device on the network(by MAC address). It's probably a lot more simple than what I'm making it. I've just put my head in circles with this. The webpage I currently can use for ip identification using MAC addressing is: http://link.signalswitch.com/

to control the devices online through a webpage(an account is needed) - http://www.signalswitch.com/UserCustomizedPage.aspx

I control these devices today by sending byte arrays through a com(usb) or an ip address with an open port. I can currently control these devices using something like:

 HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://link.signalswitch.com/getip.aspx?mac=macaddress");

            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";


            //stream for postvars
            Stream PostData = WebReq.GetRequestStream();
            //Write and close

            TcpClient ourMagicClient = new TcpClient();
            ourMagicClient.Connect(ip, port);
            NetworkStream ourStream = ourMagicClient.GetStream();
            byte[] data = { 254, 108, 1 };
            ourStream.Write(data, 0, data.Length);



            PostData.Close();



            //Read response.
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);
            Console.WriteLine(_Answer.ReadToEnd());
            Console.ReadKey();

I'm assuming that these devices must be reporting information to this website, but I guess I'm trying to figure out how I can do that with C# to my page(that hasn't been created) or to a program running on a server to handle this.

I can figure out how to send the MAC addresses to a remote location, but I still haven't been able to figure out how to control them individually without sending mass commands to all devices on a network.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.