Skip to Content
C# Cookbook
book

C# Cookbook

by Stephen Teilhet, Jay Hilyard
January 2004
Beginner to intermediate
864 pages
22h 18m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook

Chapter 13. Networking

.NET provides many classes to help make network programming easier and more accessible than many environments that preceded it. There is a great deal of functionality to assist you in building “web-aware” applications; performing simple tasks like downloading files, sending and receiving HTTP requests, and writing TCP/IP clients and servers. In areas where Microsoft had not provided a direct managed way to access networking functionality (like named pipes), there is always P/Invoke to allow you to perform networking actions via the Win32 API, which we’ll show you in this chapter. With all of the functionality at your disposal in the System.Networking namespace, you’ll be writing web utilities in no time.

13.1. Converting an IP Address to a Hostname

Problem

You have an IP address that you need to resolve into a hostname.

Solution

Use the Dns.Resolve method to get the hostname for an IP address. In the following code, an IP address is passed resolved, and the hostname is printed to the console:

using System;
using System.Net;

//...

// use the Dnss class to resolve the address 
IPHostEntry iphost = Dns.Resolve("127.0.0.1");
    
// HostName property holds the hostname
string hostName = iphost.HostName;

Discussion

The System.Net.Dns class is provided for simple DNS resolution functionality. The Resolve method returns an IPHostEntry that can be used to access the hostname via the HostName property.

See Also

See the “DNS Class” and “IPHostEntry Class” topics in the MSDN documentation. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
ASP.NET Cookbook

ASP.NET Cookbook

Michael A Kittel, Geoffrey T. LeBlond

Publisher Resources

ISBN: 0596003390Supplemental ContentCatalog PageErrata