-1

I am trying to establish a client and server asynchronous connection using sockets in c#. I have in fact download the examples for client and for server. I am wandering what stands that line: IPHostEntry ipHostInfo = Dns.GetHostEntry("host.contoso.com"); What am I suppose to retrieve in case of server and client in IPhostEntry? Should that line return host, ip and port of every device (either the server or client)?

EDIT: I copy in the place of host.contoso.com the ip of the server which already run and I got the following message: An address incompatible with the requested protocol was used.

EDIT: I add in fact IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1:11000"); and I am receiving no such host is known.

konstantin
  • 853
  • 4
  • 16
  • 50

1 Answers1

6

From the examples you only have one ipHostInfo on the CLIENT code, the server will not have this as the server will act as...well...the server itself. Code from the examples:

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

This code will establish the server (will use the current IP on the machine it is running and use port 11000.

    IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

This code for the client will connect to that server (supposedly host.contoso.com will be the name of the server in your particular case most likely will be the IP of your computer or the IP of the computer were you are running your server on).

ProgrammerV5
  • 1,915
  • 2
  • 12
  • 22
  • Just the ip? Or the ip+port? – konstantin Dec 15 '15 at 14:24
  • I tried to copy in the place of host.contoso.com the ip of the server which already run and I got the following message: An address incompatible with the requested protocol was used. – konstantin Dec 15 '15 at 14:26
  • `Dns.Resolve(string)` is meant for looking up the IP address of the provided host address. A host address can be anything that the DNS has registered as a named entity. Usually this takes the form of `www.google.com`, `www` is the sub-domain, `google.com` is what is colloquially known as the domain, while `com` is technically the top-level domain. The host name: `www.google.com` when passed to `Dns.Resolve(string)` will return the IP address of that named address. If you already have the IP address, you do not need to try to resolve it. – gmiley Dec 15 '15 at 14:32
  • And the port is not obtained from the host name, it is resolved from the `protocol` portion of a URL. For example: `http://www.google.com` (or `http://www.google.com:80` is also identical, and can serve to override the `http` portion) the protocol is `http`, which is default port 80 (but could overridden to be anything really). For your application, you will either need to hardcode a standard port that it will use, or allow it to be configurable, but the client will then need to know and provide that port number along with the host address or IP. – gmiley Dec 15 '15 at 14:36
  • Thus what should I add for IPAddress and IPENdPoint? – konstantin Dec 15 '15 at 14:37
  • Yes, your server is listening on port 11000, so your client needs to specify the IP address where the server is running plus port 11000. For example to connect to your own computer on the loopback address: `127.0.0.1:11000` – gmiley Dec 15 '15 at 14:39
  • I am getting the message no such host is known. – konstantin Dec 15 '15 at 14:45
  • One of your problems may be that it is returning an IPv6 entry instead of IPv4 you will need to modify your code to specify the type to return. Instead of me writing up a whole explanation, I found one already: http://stackoverflow.com/questions/2370388/socketexception-address-incompatible-with-requested-protocol also, the `127.0.0.1` is an example using your loopback address to your own machine, this may not be what you want. You need to specify your servers IP address and the port 11000. I do not know what your addresses are, you will need to determine all of that yourself. – gmiley Dec 15 '15 at 14:50
  • I have already came across that thread. I have changed the server code to listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); – konstantin Dec 15 '15 at 14:54