Namespace System.Net

Provides classes for creating network-aware applications. This namespace contains classes for working with sockets, HTTP, FTP, SMTP, and DNS.

Overview

The System.Net namespace is a fundamental part of the .NET Framework, enabling developers to build applications that communicate over networks. It abstracts the complexities of network protocols, allowing you to send and receive data, access resources on the internet, and manage network connections with relative ease.

This namespace provides a comprehensive set of classes and enumerations for various network programming tasks:

  • Protocol Support: Built-in support for common internet protocols like HTTP, FTP, SMTP, and POP3.
  • Socket Programming: Low-level access to network sockets for custom protocol implementation or fine-grained control.
  • Network Information: Classes to query and manage network interfaces, host names, and IP addresses.
  • Web Services: Tools for interacting with web services, including sending HTTP requests and handling responses.
  • Security: Classes for securing network communications, such as SSL/TLS integration.

Sockets

The System.Net.Sockets namespace provides a managed implementation of the Windows Sockets API, allowing you to send and receive data using TCP and UDP protocols. This is the foundation for many network applications.

Key Classes:

  • Socket: Represents a network socket.
  • TcpClient: Provides a client side TCP connection.
  • UdpClient: Provides UDP network services.
  • IPAddress: Represents an Internet Protocol (IP) address.

HTTP and Web Requests

The System.Net namespace, particularly the System.Net.Http and System.Net.WebClient classes (though WebClient is often found in System.Net.Extensions or older .NET versions), simplifies making HTTP requests to web servers.

Key Classes:

  • HttpClient: Sends HTTP requests and receives responses from a resource identified by a URI.
  • HttpWebRequest: Represents a request to a resource that can be identified by a URI.
  • HttpWebResponse: Represents a response from a resource.
  • Uri: Represents a Uniform Resource Identifier (URI) reference.

FTP Operations

The System.Net.FtpWebRequest class allows you to programmatically perform file transfer operations using the FTP protocol.

Key Classes:

SMTP and Email

For sending emails, the System.Net.Mail namespace (often used in conjunction with System.Net) provides classes to construct and send email messages.

Key Classes:

  • MailMessage: Represents an email message.
  • SmtpClient: Provides an application-level support for the Simple Mail Transfer Protocol (SMTP) to send email.

DNS Resolution

The System.Net namespace includes classes for performing Domain Name System (DNS) lookups.

Key Classes:

  • Dns: Provides simple DNS name resolution.
  • IPHostEntry: Provides the host name and all the IP addresses associated with a network endpoint.

Common Tasks

Here are some common scenarios you might encounter when working with System.Net: