Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a web application that can communicate with my device over internet.The device use Ethernet port for communication.

Previously i have designed a web application that can communicate to weighing scale device over a LAN network.i wanted to know whether the same code can be used to devices over internet or not.We use IP address and port no to communicate to the device .

I have used the given below code for communication.

C#
public string GetScaleWeight(string val1, string val2)//GetWeight contains the code to fetch the weight from the scale//
        {
            try
            {
                Socket sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//here we are using socket programming//
                IPAddress ip = IPAddress.Parse(val1);                                                    // to connect to the scale device//
                IPEndPoint dp = new IPEndPoint(ip, int.Parse(val2));
                sc.Connect(dp);
                Byte[] myArray = new Byte[100];
                int value = Convert.ToInt32("5", 16);
                string stringValue = Char.ConvertFromUtf32(value);
                sc.Send(Encoding.ASCII.GetBytes((stringValue)));//passing the hex command to the machine//
                int no = sc.Receive(myArray);//getting the length of the result array//
                string str = System.Text.Encoding.ASCII.GetString(myArray);//getting the result and storing it in a string//
                sc.Close();
                str = str.Substring(0, no);//checking and removing any unwanted garbage value if any //
               return str;
            }
            catch
            {
                return "";
            }
        }  


Can this code be used to communicate to the devices over internet with some modification.
If not Please help on how can we do this.
Posted
Updated 27-Jul-15 19:23pm
v2

1 solution

Sockets would work if there are no intermediate proxies or firewall, but, since that is unlikely in case of internet, you should use HTTP/HTTPS for communication.
 
Share this answer
 
Comments
pranav8265 28-Jul-15 4:11am    
thanks for replying.
Can you provide a example ??
himanshu agarwal 28-Jul-15 4:15am    
This should help you. It has an example as well.

https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx
pranav8265 28-Jul-15 6:14am    
how can we send command to device using HTTP communication.My device accepts command in byte format to which it produces a byte reply.I am not been able to understand how i can make use of HTTP communication.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900