Cannot connect using IP address

S

s_alexander04

Hello!

I have a problem with connecting to IP address using TcpClient class.
When I do:
TcpClient cl=new TcpClient("host_name.com",port);
It's OK, but when I change host-name to an IP address:
TcpClient cl=new TcpClient("aaa.bbb.ccc.ddd",port);
it throws SocketException: "No such host is known".

When I try such thing:
TcpClient cl=new TcpClient(new
IPEndPoint(IPAddress.Parse("aaa.bbb.ccc.ddd"),port));
I get SocketException with a message "The requested address is not
valid in its context"

When this:
TcpClient cl=new TcpClient(new
IPEndPoint(Dns.Resolve("aaa.bbb.ccc.ddd").AddressList[0],port));
I get SocketException with a message "No such host is known". But I
exactly know that it does exist and the IP address and port are
correct.

How should I connect using an IP address? Or maybe is there a solution
that works both for an IP address and a host name? I use VS2003, CF1.

Thanks in advance.


Alexander.
 
P

Paul G. Tobey [eMVP]

Your first try to correct the problem was close, but you've misinterpreted
what the constructor means:

TcpClient cl=new TcpClient(new
IPEndPoint(IPAddress.Parse("aaa.bbb.ccc.ddd"),port));

This is trying to create the indicated endpoint ***on the local device where
the code is running***. Since you've specified an IP address that
presumably is for some other device, this won't work! Try the empty
constructor and use the Connect() method to make the connection, instead.

TcpClient cl=new TcpClient();

// Connect.
cl.Connect(new
IPEndPoint(IPAddress.Parse(IPAddressEdit.Text),Int32.Parse(
PortNumberEdit.Text )));

Paul T.
 
S

s_alexander04

Thank you for answering my question!

(I should have read docs more carefully before posting it ;-)

Alexander.
 
P

Paul G. Tobey [eMVP]

I did the same thing when I was trying to figure out what you'd done wrong.
It looked right until I thought about why there were the set of constructor
patterns and Connect() methods that it has.

Paul T.
 

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