System.Net.Sockets.Socket Class
Summary
Provides a managed implementation of the socket API, supporting synchronous and asynchronous operations for sending and receiving data across a network using protocols such as TCP and UDP.
The Socket class is the primary class for low-level network communication. It allows you to create and manage network connections, send and receive data, and handle various network-related events.
Inheritance
Object > Socket
Remarks
Use the Socket class to implement network-aware applications. You can use it to build clients, servers, or peer-to-peer applications. The class supports both blocking (synchronous) and non-blocking (asynchronous) I/O operations.
For higher-level network communication abstractions, consider using classes like TcpClient, UdpClient, or the System.Net.Http namespace.
Close() or Dispose() method when you are finished with a Socket object to release unmanaged resources and prevent network port exhaustion.
Constructors
The Socket class has several constructors to initialize a new instance:
Socket(AddressFamily, SocketType, ProtocolType)Socket(SocketInformation)
Socket(AddressFamily, SocketType, ProtocolType)
Initializes a new instance of the Socket class with the specified address family, socket type, and protocol.
public Socket(
AddressFamily addressFamily,
SocketType socketType,
ProtocolType protocolType
);
Parameters
| Parameter | Description |
|---|---|
addressFamily |
An AddressFamily enumeration value that specifies the namespace of the IP address. For example, AddressFamily.InterNetwork for IPv4 or AddressFamily.InterNetworkV6 for IPv6. |
socketType |
A SocketType enumeration value that specifies the type of socket. For example, SocketType.Stream for TCP or SocketType.Dgram for UDP. |
protocolType |
A ProtocolType enumeration value that specifies the protocol to be used. For example, ProtocolType.Tcp or ProtocolType.Udp. |
Properties
Key properties of the Socket class include:
Available: Gets the amount of data, in bytes, received on the socket that has not been read by theNetworkStream.Blocking: Gets or sets a value indicating whether theSocketis in blocking mode.Connected: Gets a value indicating whether theSocketis connected to a remote host.Handle: Gets the native socket handle.LocalEndPoint: Gets the local endpoint of theSocket.RemoteEndPoint: Gets the remote endpoint of theSocket.
Methods
Commonly used methods:
Accept(): Accepts an incoming connection request.Bind(EndPoint): Binds theSocketto a local endpoint.Close(): Closes the socket connection.Connect(EndPoint): Establishes a connection to a remote host.Disconnect(Boolean): Disconnects the socket from the remote host.Listen(Int32): Sets the maximum number of pending connections.Receive(Byte[]): Receives data from the connected socket.Send(Byte[]): Sends data to the connected socket.SetSocketOption(SocketOptionLevel, SocketOptionName, Int32): Sets a socket option.Shutdown(SocketShutdown): Disables sends, receives, or both on the socket.
Connect(EndPoint remoteEP)
Establishes a synchronous connection to a remote device.
public void Connect(
EndPoint remoteEP
);
Example
try {
IPAddress ipAddress = IPAddress.Parse("192.168.1.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8080);
Socket clientSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(ipEndPoint);
Console.WriteLine("Connected to server.");
// ... send/receive data ...
clientSocket.Close();
} catch (Exception ex) {
Console.WriteLine("Connection failed: " + ex.Message);
}
Events
The Socket class supports asynchronous operations through events, typically used with SocketAsyncEventArgs:
Completed: This event is raised when an asynchronous socket operation is completed.