question

pravinGavande-9647 avatar image
0 Votes"
pravinGavande-9647 asked pravinGavande-9647 commented

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

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();
             }
dotnet-csharpdotnet-iot
· 16
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @pravinGavande-9647 ,


I'm not sure what you port you are listening to: it seems 5100 but 80 is mentioned too...

The example has some unused code, could it be optimized to:

 IPAddress ipAddress = IPAddress.Parse("198.168.1.10");
 TcpListener listener = new TcpListener(ipAddress, 5100);
 listener.Start();

Do you have some other documentation?

crosspost from stackoverflow


0 Votes 0 ·

Thanks

My code is like below..

IPAddress ipAddress = IPAddress.Parse("198.168.1.10");
TcpListener listener = new TcpListener(ipAddress, 5100);
System.Net.IPEndPoint endPoint = new IPEndPoint(ipAddress, 5100);

listener.Start(); //Here i am getting error as Requested address is invalid

Note :- Firewall is turned off in laptop.

0 Votes 0 ·

Hello @pravinGavande-9647 ,

What is the purpose of the endpoint class?

On which example is this code based?

0 Votes 0 ·
Show more comments

Hi @pravinGavande-9647,
First you can check the valid IP address on the local computer (run ipconfig /all). If your IP address is not in this list, your address is invalid.
You cannot bind to an external IP. However, you can set up the router to forward incoming requests on a specific port to the computer's local IP.
If you are trying to access from the outside, it is best to use a non-static IP to subscribe to the DynDNS service, so even if the IP changes, you can always access.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Hi Daniel,

I read too many articles now and as per your input its correct. i can listen to only localhost or self IP which avaible on ipconfig only.. but now i want to capture the medical instrument device HL7 message on port 5100.
So can you tell me exactly should i use TCPclient or TCP listener for this ?.

I have used TCPlistener and tried to listen the port 5100 but medical machine always show me message like "Please check Network connection". and i already assigned one free IP of same DNS to Medical instrument. still same msg is coming.

Please guide me what i should do ?.

0 Votes 0 ·

Hi @pravinGavande-9647,
Please try to use TcpClient instead of TcpListener and pass the address of your medical device.
Best Regards,
Daniel Zhang


0 Votes 0 ·
Show more comments

1 Answer

APoblacion avatar image
1 Vote"
APoblacion answered pravinGavande-9647 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have two computers and when we used IPADDRESS.ANY then i can able to listen the message coming from computer1.

But when i connected Medical instrument and my laptop each other throw lan then medical instrument is saying check network connection.... so i think IPADRESS.ANY not work. i need to connect the machine with ipadress then machine knows that some one is listening then it will send data.

0 Votes 0 ·