简体   繁体   中英

Client server connection via socket in c#

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM