0

I have a sensor ,i connect to this sensor using network cable.I should send a command to this sensor to get the value .

The sensor ip is :192.168.2.44
my computer ip:192.168.2.111

I used a program called hercules as you can see here to connect to the sensor : enter image description here

In the TCP server tab i define the port to 3000 when i clicked on listen button the program shows this (as you can see in the picture) client connected

enter image description here

After connecting i can send a command to the sensor to get the value as you can see in the picture: enter image description here

I found this code but it does't work and i can't send a command by this to get the value ,the main problem is my code can't connect to the port could you give me some help . i am so new in socket programming .

Code:

try
{
    string hostname = "192.168.2.44";
    int portno = 3000;
    IPAddress ipa = Dns.GetHostAddresses(hostname)[0];
    try
    {
        System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
        sock.Connect(ipa, portno);
        if (sock.Connected == true)  // Port is in use and connection is successful
            Console.WriteLine("Port is Closed");
        sock.Close();
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        if (ex.ErrorCode == 10061)  // Port is unused and could not establish connection 
            Console.WriteLine("Port is open");
        else
            Console.WriteLine(ex.ErrorCode);
    }
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
    Console.ReadLine();
}

Exception:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.2.44:3000

I should implement something like the hercules using c#

cramopy
  • 3,459
  • 6
  • 28
  • 42
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • "_I found this code but it does't work_" What do you mean by that? Does it throw an exception? – cbr Jun 18 '15 at 10:43
  • yes exactly ,the timeout exception – Ehsan Akbar Jun 18 '15 at 10:44
  • @cubrr A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.2.44:3000 – Ehsan Akbar Jun 18 '15 at 10:46
  • 3
    Wait a minute. In your screenshots, PC is a master device (it opens a listening server socket), and the sensor is a slave. While your code assumes, that PC tries to connect to a sensor as a *client*. Do you feel the difference? – Dennis Jun 18 '15 at 10:47
  • @Dennis ok dear friend – Ehsan Akbar Jun 18 '15 at 10:51
  • @Dennis so do you have any ideas? – Ehsan Akbar Jun 18 '15 at 10:54
  • 1
    @EhsanAkbar : You need to make a server (listening for new connections on port 3000, from your screenshots), not a client. I suggest you make a server using `TcpListener` rather than attempting to use the lower level `Socket` class directly. The example that I wrote [here](http://stackoverflow.com/questions/12630827/using-net-4-5-async-feature-for-socket-programming/12631467#12631467) might serve as a handy starting point... – spender Jun 18 '15 at 10:55
  • @spender i try to implement your code ,but i have a little error,can i have a chat with u ? – Ehsan Akbar Jun 18 '15 at 11:07
  • @spender i get this exception :Only one usage of each socket address (protocol/network address/port) is normally permitted – Ehsan Akbar Jun 18 '15 at 11:11
  • 1
    @EhsanAkbar, did you switch off your "Hercules" server? Two processes can't share the same port when listening. – spender Jun 18 '15 at 11:15
  • No i didn't ,thank you let me check it – Ehsan Akbar Jun 18 '15 at 11:17

1 Answers1

2

In your screenshots, PC is a master device (it opens a listening server socket), and the sensor is a slave. While your code assumes, that PC tries to connect to a sensor as a client.

The minimal code snippet is this:

        var listener = new TcpListener(IPAddress.Any, 3000);
        listener.Start();

        using (var client = listener.AcceptTcpClient())
        using (var stream = client.GetStream())
        {
            // build a request to send to sensor
            var request = new byte[] { /*...*/ };
            stream.Write(request, 0, request.Length);
            // read a response from sensor;
            // note, that respose colud be broken into several parts;
            // you should determine, when reading is complete, according to the protocol for the sensor
            while (!/* response is complete */)
            {                    
                // stream.Read calls here
            }
        }

Also note, that if the protocol is textual, then you could build request and parse response using StreamWriter/StreamReader.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Thank you i implement your code but i got this error :Only one usage of each socket address (protocol/network address/port) is normally permitted – Ehsan Akbar Jun 18 '15 at 11:16
  • 1
    @EhsanAkbar: this is because another application uses 3000 port (maybe, "Hercules" you referred). You must close that application or make it to stop listening on 3000 port. – Dennis Jun 18 '15 at 11:19
  • thank you how can i send my request ,my request is like this 000100020005030190000212BD in hex format – Ehsan Akbar Jun 18 '15 at 11:19
  • 1
    @EhsanAkbar: I don't know anything about sensor's protocol. Ultimately, you must convert the request into array of bytes. The rules for conversion is defined by protocol. If it is binary, you can just convert string you've posted into byte constants in code: 0x00, 0x01, ... , 0xBD. – Dennis Jun 18 '15 at 11:24