TlsServerSocket Class
Namespace: Rebex.Net
Assembly: Rebex.Tls.dll (version 8.0.9673)
Represents a server-side TLS-enabled TCP socket.
Syntax
public class TlsServerSocket : TlsSocket, ISocketExt, ISocket, IDisposable, IAsyncDisposable
Inherited Members
Remarks
This class is suitable for securing your socket communication using TLS 1.3, TLS 1.2, TLS 1.1 and TLS 1.0 protocols.
Examples
See https://www.rebex.net/tls/features/simple-tls-api.aspx#server-api for more code snippets.
See https://www.rebex.net/tls/samples.aspx for ready-to-use samples.
Starting the TLS server
// bind TCP listener to 0.0.0.0:8443
var listener = new TcpListener(IPAddress.Any, 8443);
listener.Start();
// accept incoming TCP connection
var socket = await listener.AcceptSocketAsync();
// initialize new TLS server socket
var tls = new TlsServerSocket(socket);
// attach logger for diagnostic purposes
tls.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\tls.log", LogLevel.Debug);
// configure some TLS properties
tls.Parameters.Version = TlsVersion.TLS12 | TlsVersion.TLS13;
tls.Parameters.AllowedCurves = TlsEllipticCurve.All;
// specify server certificate
var cert = CertificateChain.LoadPfx("cert.pfx", "password");
tls.Parameters.Certificate = cert;
// negotiate TLS
await tls.NegotiateAsync();
// print TLS cipher info
Console.WriteLine("Secured with: {0}", tls.Cipher);
// run main session loop
await RunTlsSessionLoop(tls);
tls.Close();
For simplicity, the example above handles a single connection at a time. Visit the TlsProxy sample for more information about handling multiple connections.
Example of the RunTlsSessionLoop() method
private async Task RunTlsSessionLoop(TlsServerSocket tls)
{
var buffer = new byte[1024];
while (true)
{
// initialize new array segment
var segment = new ArraySegment<byte>(buffer);
// receive data
int n = await tls.ReceiveAsync(segment);
if (n == 0)
break;
// update array segment
segment = new ArraySegment<byte>(buffer, 0, n);
// echo data back
await tls.SendAsync(segment);
}
}
Constructors
| Name | Description |
|---|---|
| TlsServerSocket(Socket) | Initializes an instance of the TlsServerSocket class with the specified base socket. |
Properties
| Name | Description |
|---|---|
| ApplicationProtocol | Gets the negotiated application protocol; or null reference if no protocol was negotiated. Inherited from TlsSocket. |
| Available | Gets the amount of data that has been received from the network and is available to be read. Inherited from TlsSocket. |
| Cipher | Gets the active cipher. Inherited from TlsSocket. |
| ClientCertificate | Gets the client's certificate chain. Inherited from TlsSocket. |
| CompressionMethod | Gets the active data compression method. Inherited from TlsSocket. |
| Connected | Gets a value indicating whether a TlsSocket is connected to a remote resource. Inherited from TlsSocket. |
| Context | Gets or sets a custom context object associated with this TlsSocket. Inherited from TlsSocket. |
| Entity | Gets the entity. Inherited from TlsSocket. |
| Factory | Gets the instance of ISocketFactory that created this ISocket. Inherited from TlsSocket. |
| Information | Gets socket information on the underlying socket object. Inherited from TlsSocket. |
| IsSecure | Gets the current security state of the connection. Inherited from TlsSocket. |
| LocalEndPoint | Gets the local endpoint. Inherited from TlsSocket. |
| LogWriter | Gets or sets the logger used by this object. Inherited from TlsSocket. |
| Parameters | Gets or sets the security parameters of the current socket. Inherited from TlsSocket. |
| RemoteEndPoint | Gets the remote endpoint. Inherited from TlsSocket. |
| ServerCertificate | Gets the server's certificate chain. Inherited from TlsSocket. |
| Session | Gets the connection session Inherited from TlsSocket. |
| Socket | Gets the internal Socket value (only for sockets based on Socket or ProxySocket, otherwise null is returned). Inherited from TlsSocket. |
| Timeout | Gets or sets the length of time, in milliseconds, until the operation times out, or the value -1 or 0 to indicate that the operation does not time out. Inherited from TlsSocket. |
Methods
| Name | Description |
|---|---|
| BeginConnect(EndPoint, AsyncCallback, Object) | Begins an asynchronous request for a connection to a network device. Inherited from TlsSocket. |
| BeginConnect(String, Int32, AsyncCallback, Object) | Begins an asynchronous request for a connection to a network device. Inherited from TlsSocket. |
| BeginNegotiate(AsyncCallback, Object) | Negotiates TLS/SSL cipher. Inherited from TlsSocket. |
| BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) | Ends a pending asynchronous read. Inherited from TlsSocket. |
| BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) | Sends data asynchronously to a connected ISocket. Inherited from TlsSocket. |
| Close() | Forces a TlsSocket connection to close. Inherited from TlsSocket. |
| CloseAsync() | Asynchronously releases resources used by the TlsSocket. Inherited from TlsSocket. |
| Connect(EndPoint) | Establishes a connection to a remote device. Inherited from TlsSocket. |
| Connect(String, Int32) | Establishes a connection to a remote device. Inherited from TlsSocket. |
| Dispose() | Disposes the object. Inherited from TlsSocket. |
| Dispose(Boolean) | Releases the unmanaged resources used by the TlsSocket and optionally releases the managed resources. Inherited from TlsSocket. |
| DisposeAsync() | Asynchronously releases resources used by the TlsSocket. Inherited from TlsSocket. |
| DisposeAsync(Boolean) | Asynchronously releases the unmanaged resources used by the TlsSocket and optionally releases the managed resources. Inherited from TlsSocket. |
| EndConnect(IAsyncResult) | Ends a pending asynchronous connection request. Inherited from TlsSocket. |
| EndNegotiate(IAsyncResult) | Ends a pending asynchronous negotiation. Inherited from TlsSocket. |
| EndReceive(IAsyncResult) | Ends a pending asynchronous read. Inherited from TlsSocket. |
| EndSend(IAsyncResult) | Ends a pending asynchronous send. Inherited from TlsSocket. |
| GetChannelBinding(ChannelBindingType) | Gets an instance of the ChannelBinding that matches the |
| GetConnectionState() | Gets a value that indicates whether the SocketState is currently connected to a remote host. Inherited from TlsSocket. |
| GetHashCode() | Gets a hash code of this object. Inherited from TlsSocket. |
| HasChannelBinding(ChannelBindingType) | Gets an indication whether a provider has a ChannelBinding that matches the |
| Negotiate() | Negotiates TLS/SSL cipher. Inherited from TlsSocket. |
| NegotiateAsync() | Negotiates TLS/SSL cipher. Inherited from TlsSocket. |
| Poll(Int32, SocketSelectMode) | Determines the status of the socket. Inherited from TlsSocket. |
| Receive(ArraySegment<Byte>) | Receives data from a connected TlsSocket. Inherited from TlsSocket. |
| Receive(Byte[]) | Receives data from a connected TlsSocket. Inherited from TlsSocket. |
| Receive(Byte[], Int32, Int32) | Receives the specified amount of data from a connected TlsSocket into a specific location of the receive buffer and using the supplied flags. Inherited from TlsSocket. |
| Receive(Byte[], Int32, Int32, SocketFlags) | Receives the specified amount of data from a connected TlsSocket into a specific location of the receive buffer and using the supplied flags. Inherited from TlsSocket. |
| Receive(Byte[], Int32, SocketFlags) | Receives the specified amount of data from a connected TlsSocket using the supplied flags. Inherited from TlsSocket. |
| Receive(Byte[], SocketFlags) | Receives data from a connected TlsSocket using the supplied flags. Inherited from TlsSocket. |
| ReceiveAsync(ArraySegment<Byte>) | Receives data from a connected TlsSocket. Inherited from TlsSocket. |
| Renegotiate() | Renegotiates TLS/SSL cipher (if allowed by the other side). Only supported by TLS 1.2 or earlier. Inherited from TlsSocket. |
| Send(ArraySegment<Byte>) | Sends data to a connected TlsSocket. Inherited from TlsSocket. |
| Send(Byte[]) | Sends data to a connected TlsSocket. Inherited from TlsSocket. |
| Send(Byte[], Int32, Int32) | Sends the specified amount of data to a connected TlsSocket starting at the indicated location in the data and using the supplied flags.| Inherited from TlsSocket. |
| Send(Byte[], Int32, Int32, SocketFlags) | Sends the specified amount of data to a connected TlsSocket starting at the indicated location in the data and using the supplied flags. Inherited from TlsSocket. |
| Send(Byte[], Int32, SocketFlags) | Sends the specified amount of data to a connected TlsSocket using the supplied flags. Inherited from TlsSocket. |
| Send(Byte[], SocketFlags) | Sends data to a connected TlsSocket using the supplied flags. Inherited from TlsSocket. |
| SendAsync(ArraySegment<Byte>) | Sends data to a connected TlsSocket. Inherited from TlsSocket. |
| Shutdown(SocketShutdown) | Disables sends and receives on a TlsSocket. Inherited from TlsSocket. |
| ShutdownAsync() | |
| ToString() | Returns a string representation of the current TlsSocket. Inherited from TlsSocket. |
| Unprotect() | Reverts back to an unencrypted socket. Usage of this method is strongly discouraged. Inherited from TlsSocket. |
Events
| Name | Description |
|---|---|
| ClientHelloReceived | Occurs when a TLS ClientHello has been received. |
| ValidatingCertificate | Occurs when a client certificate needs to be validated. |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IAsyncDisposable.DisposeAsync() | Asynchronously releases resources used by the TlsSocket. Inherited from TlsSocket. |