The requsted address is invalid in this context when i am trying to listing for specific IP

pravin Gavande 1 Reputation point
2021-05-07T12:40:36.173+00:00

I am trying to connect the medical equipment with LAN cable. and i am using C# based windows application to receive the data which machine is sending when any blood sample runs in machine

As per my requirement i need to use TCP listner to recevie the data which machine is sending.. but when i connected medical equipment(ip address - 198.168.1.10) with my laptop(ip address- 198.168.1.11) throw lan then i am getting error message like "The requested address is invalid in this context".

(I already checked both sender ip is able to ping from laptop as well).

Then i read may articale and find that need to use IPADDRESS.ANY. But this will not help me.

Means sender machine says ///when cursor comes to start the listener then it is throwing the error msg. please check network connection...

pasting my code here.. please check and help me with this

//Code start

//Posting my complete code

          string response = String.Empty;
            IPAddress ipAddress = IPAddress.Parse("198.168.1.10");
            Socket client;
            TcpListener listener = new TcpListener(ipAddress,5100);
            listener.Start();
            byte[] data;

            while (true)
            {

              client = listener.AcceptSocket();
              var childSocketThread = new Thread(() =>
                {
                    data = new byte[4096];
                    int size = client.Receive(data);
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    string returndata = Encoding.UTF8.GetString(data);
                    // Get response
                    string responce = responcemsg();
                    client.Send(Encoding.UTF8.GetBytes(responce));
                    //Custom save funtion
                    callsave(returndata);
                  client.Close();
                });

                childSocketThread.Start();
            }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,038 questions
.NET Internet of things
.NET Internet of things
.NET: Microsoft Technologies based on the .NET software framework.Internet of things: A concept that aims to extend the benefits of the regular internet, including constant connectivity, remote control ability, and data sharing, to goods in the physical world.
30 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Poblacion 1,561 Reputation points
    2021-05-08T11:09:37.947+00:00

    Wrong address. If your laptop is 198.168.1.11 you cannot listen on 198.168.1.10. The address that you pass to the Listener is your local address (your machine could potentially have several addresses, and you tell it n which one of them it should be listening for any other machine to connect). It is not the address of the machine from which you want to receive a connection.

    Edit: If you want your computer to listen on all of its addresses, so that you don't have to bother to tell the TcpListener which one to use, you can pass the option IPAddress.Any.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.