Hello, I have a device (a control relay board). I have it hooked into my home network and I have a different network that I would like to be able to access the device to control it. I have the mac address of my device and my ports are forwarded on my home network to do this. I'm not sure where I'm going wrong on this, but here is my code so far. Basically I need it to retrieve the ip address using the mac address dynamically so that I can access my device and send it commands. Any help would be great, plus maybe some of this code will be useful?

using System.ComponentModel;
using System.Collections;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Net;

namespace CleanedHTTPRequest
{
    class Program
    {
        static void Main(string[] args)
        {


            Console.WriteLine("Please enter the MAC address:");
            string mac=Console.ReadLine();
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://linkremovedforpost/getip.aspx?mac="+mac);

            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();
            try
            {
                string[] parts = parsed.Split(':');
                if (parts.Length != 2)
                {
                    //throw exception
                }
                string host = parts[0];
                string port = parts[1];


            TcpClient ourMagicClient = new TcpClient();

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


                Console.WriteLine("Please enter cmd part1: ");
                int part1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Please enter cmd part1: ");
                int part2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Please enter cmd part1: ");
                int part3 = Convert.ToInt32(Console.ReadLine());
                byte[] data = { Convert.ToByte(part1), Convert.ToByte(part2), Convert.ToByte(part3) };
                ourStream.Write(data, 0, data.Length);

                PostData.Close();


            Console.WriteLine(host);
        }

                //genereal exception to handle a bad mac address or invalid split
             catch (Exception sX)
            {
                Console.WriteLine("Device is taking to long to respond, please make sure the device is a valid device and connected to a valid network");
            }
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);
            Console.WriteLine(_Answer.ReadToEnd());



            Console.ReadKey();
        }

    }
}

I had this working the other day, or I thought I did. Not sure where I'm going wrong.

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.