17

I am trying to connect to a sensor using network, the sensor's ip is 192.168.2.44 on port 3000;

My Code:

byte[] byteReadStream = null; // holds the data in byte buffer
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("192.168.2.44"), 3000);//listen on all local addresses and 8888 port
TcpListener tcpl = new TcpListener(ipe);
while (true)
{
    //infinite loop
    tcpl.Start(); // block application until data and connection
    TcpClient tcpc = tcpl.AcceptTcpClient();
    byteReadStream = new byte[tcpc.Available]; //allocate space
    tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
    Console.WriteLine(Encoding.Default.GetString(byteReadStream) + "\n");
}

enter image description here

But when I run this code, I get this error:

The requested address is not valid in its context

peterh
  • 11,875
  • 18
  • 85
  • 108
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

4 Answers4

22

Use IPAddress.Any to listen. This seems to be your goal:

//listen on all local addresses

The listening address you have specified is invalid for some reason. There is no need to specify a numeric address.

usr
  • 168,620
  • 35
  • 240
  • 369
  • the error is changed to this :Only one usage of each socket address (protocol/network address/port) is normally permitted – Ehsan Akbar Jun 17 '15 at 11:30
  • i changed to this : IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 3000); – Ehsan Akbar Jun 17 '15 at 11:31
  • What does the new error message tell you? What is your interpretation of that message? – usr Jun 17 '15 at 11:35
  • @usr can you please explain why this works? And the actual device IP doesn't – mrid Aug 01 '18 at 05:15
  • Likely, the IP that he specified is not an IP that the local computer owns. Maybe he specified some other device's IP but he needs his' device IP. IPAddress.Any is an easy solution and most of the time what you want anyway. Often, you don't want to restrict listening to any specific NIC. @mrid – usr Aug 01 '18 at 14:08
  • What are the disadvantages of using `IPAddress.Any` ? is there any Gotchu's? For e.g in a network if another service or app is using port `5000` by listening on port `5000` does listening affects other apps e.g. kicks them off the port? what if the request is frequency(does it affects anything else on port 5000`? is it a good idea to use port `5000` is it reserved? – Dev Sep 02 '19 at 15:43
  • @Decoder94 Any is appropriate if you want to listen on all IP addresses. This is often the case. Access is often controlled using a firewall. Any has no impact on port conflicts. Essentially, the OS prevents you from using a port twice. If that happens it's usually a configuration error that must be fixed. – usr Sep 04 '19 at 08:21
  • @usr Do conflicts only happen on the same IP with same port only? OR can i have conflict when: PC1 IP:5000, PC2 IP:5000, IPhone/Randomdevice IP:5000. if all these three pcs are in the same network, Does using the same port causes any Issues(such as conflicts)? – Dev Sep 04 '19 at 12:05
  • 1
    @Decoder94 only on the same IP. It's because a TCP connection is identified by (IP1, Port1, IP2, Port2). There can be no duplicates in this set. Different "PCs" can overlap port usage. There is no mechanism that would synchronize across machines what the respective operating system considers opened or closed. Opening a port is not a network level concept. It's an OS concept. You could write an OS that does not have the very notion of an open port. – usr Sep 11 '19 at 08:30
11

The TcpListener listens for connections from TCP network client, on a given port on your local machine. That is, for incoming connections. Your code will be acting as a "server" of sorts.

The requested address is not valid in its context

Simply, it means that the IP address given is not used by any network interface on your machine.

Use IPAddress.Any to listen on any IP address (i.e. network interface).

However, it might be the case that you need to connect to the sensor (on port 3000), not the other way around.

EDIT: The new exception just tells you that you have two applications trying to listen to the same interface/port combination. Do you have two instances running at the same time?

Micke
  • 2,251
  • 5
  • 33
  • 48
  • the error is changed :Only one usage of each socket address (protocol/network address/port) is normally permitted – Ehsan Akbar Jun 17 '15 at 11:35
0

One of the reason for this issue could be the presence of defaultProxy section in the configuration file which would be routing every outgoing call via proxy address mentioned in this setting. Ensure that either there is a proxy server/service listening at the proxy address, or comment this section to stop the routing. In case the application configuration file does not have it, and this error is still there, check for the defaultProxy section in the machine.config file. machine.config should be available in folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config. In our case it was the defaultProxy in machine.config.

DiligentKarma
  • 5,198
  • 1
  • 30
  • 33
0

Tech / Situation: I got this error while trying to run a Web API (ASP.Net Core 6) using my local IP so that I could connect to the API from a React Native App (maybe there's an easier way to do that).

Problem: I was at work, everything worked fine. I got home, and it gave the above error.

Realisation: Realised the router at work and at home generates (?) different local IP addresses (I don't know much about IPs and routers).

Solution:

  1. Find your local IP
  2. Copy Paste this and replace your localhost with it.
Jahkento
  • 1
  • 1
  • 3