TcpClient Exception: "The requested address is not valid in its context"

H

Horst Walter

What is wrong here?

IPAddress ipAddress = IPAddress.Parse("10.10.20.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port);
this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE

=> Exception: "The requested address is not valid in its context"


This works:
this.tcpClient = new TcpClient("10.10.20.1", this.port);

However, the latter is somehow slow, because it seems that in this
case a DNS resolution takes place. This is exactly what I'd like to
avoid.

Any ideas?
Regards HW
 
I

Ignacio Machin \( .NET/ C# MVP \)

This may be cause the IP is not a valid IP on your computer, or maybe the
port is binded to another application.

If you dont really need to especify the IP then create the IPEndPoint with a
0:
IPEndPoint ipEndPoint = new IPEndPoint( 0, this.port);

Hope this help,
 
L

Laszlo Szijarto

Is "10.10.20.1" the IP of your own machine?
Just guessing. Maybe the second overload of TcpClientConstructor can handle
that, but an IP on your own machine doesn't qualify as an "EndPoint".
 
H

Horst Walter

Firstly, thanks a lot for your help!

However, the solution still is not clear:

"10.10.20.1" in my case is a valid IP and NOT the IP of the computer
itself (where the prg. runs).

Furthermore this works (SAME PORT /IP):
this.tcpClient = new TcpClient("10.10.20.1", this.port);

So I do not believe it is some kind of general network problem.....

Regards
KB
 
R

Rich Blum

What is wrong here?

IPAddress ipAddress = IPAddress.Parse("10.10.20.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port);
this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE

=> Exception: "The requested address is not valid in its context"


This works:
this.tcpClient = new TcpClient("10.10.20.1", this.port);
Horst -

You are mixing apples with oranges. The TcpClient constructor that
uses the IPEndPoint defines a local endpoint for the socket to bind
to, while the constructor that uses the string defines a remote
endpoint for the socket to connect to. Thus, the first constructor is
looking to bind the socket to the local IP address and port specified
in the IPEndPoint object. Since you gave it a remote IP address it
couldn't perform the task and threw the Exception.

If you want to connect to a remote endpoint using an IPEndPoint
object, try using the Socket object. This allows you to use an
EndPoint, and shouldn't try the DNS lookup of the IP address. This
would look something like this:

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.10.20.1",
this.port);
sock.Connect(ipep);
NetworkStream ns = new NetworkStream(sock);

Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 
H

Horst Walter

Thanks a lot!
HW



Horst -

You are mixing apples with oranges. The TcpClient constructor that
uses the IPEndPoint defines a local endpoint for the socket to bind
to, while the constructor that uses the string defines a remote
endpoint for the socket to connect to. Thus, the first constructor is
looking to bind the socket to the local IP address and port specified
in the IPEndPoint object. Since you gave it a remote IP address it
couldn't perform the task and threw the Exception.

If you want to connect to a remote endpoint using an IPEndPoint
object, try using the Socket object. This allows you to use an
EndPoint, and shouldn't try the DNS lookup of the IP address. This
would look something like this:

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.10.20.1",
this.port);
sock.Connect(ipep);
NetworkStream ns = new NetworkStream(sock);

Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top