/* * Mentalis.org Security Library * * Copyright © 2002-2005, The KPD-Team * All rights reserved. * http://www.mentalis.org/ * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Neither the name of the KPD-Team, nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Net; using System.Net.Sockets; using System.Collections; namespace Org.Mentalis.Security.Ssl { /// /// Implements the Berkeley sockets interface. /// /// /// The VirtualSocket class implements exactly the same methods as the System.Net.Sockets.Socket class, /// however all these methods are marked as virtual so they can be overridden in derived classes. /// In addition to the constructor specified by the Socket class, the VirtualSocket class also /// has a constructor that accepts an already created Socket; this can be very useful is you have /// to override the Accept and BeginAccept/EndAccept methods in a derived class. /// public class VirtualSocket { /// /// Initializes a new instance of the VirtualSocket class. /// /// One of the values. /// One of the values. /// One of the values. /// The combination of , , and results in an invalid socket. /// The parameter specifies the addressing scheme that the VirtualSocket uses, the parameter specifies the type of the VirtualSocket, and specifies the protocol used by the VirtualSocket. The three parameters are not independent. Some address families restrict which protocols can be used with them, and often the socket type is implicit in the protocol. If the combination of address family, socket type, and protocol type results in an invalid VirtualSocket, a SocketException is thrown.
The AddressFamily enumeration defines the valid address families, the SocketType enumeration defines the valid socket types, and the ProtocolType enumeration defines the valid protocol types.
public VirtualSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) { m_InternalSocket = new Socket(addressFamily, socketType, protocolType); } /// /// Initializes a new instance of the VirtualSocket class. /// /// The accepted socket. /// is a null reference (Nothing in Visual Basic). protected VirtualSocket(Socket internalSocket) { if (internalSocket == null) throw new ArgumentNullException(); m_InternalSocket = internalSocket; } /// /// Gets or sets the internal value. /// /// An instance of the Socket class. protected Socket InternalSocket { get { return m_InternalSocket; } set { m_InternalSocket = value; } } /// /// Gets or sets a value that indicates whether the VirtualSocket is in blocking mode. /// /// true if the VirtualSocket will block; otherwise, false. The default is true. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. public virtual bool Blocking { get { return InternalSocket.Blocking; } set { InternalSocket.Blocking = value; } } /// /// Gets the address family of the VirtualSocket. /// /// One of the values. /// AddressFamily specifies the addressing scheme that an instance of the VirtualSocket class can use. This property is read-only and is set when the VirtualSocket is created. public virtual AddressFamily AddressFamily { get { return InternalSocket.AddressFamily; } } /// /// Gets the amount of data that has been received from the network and is available to be read. /// /// The number of bytes of data that has been received from the network and are available to be read. /// If you are using a Stream VirtualSocket type, the available data is generally the total amount of data queued on the current instance. If you are using a message-oriented VirtualSocket type such as Dgram, the available data is the first message in the input queue. /// An error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. public virtual int Available { get { return InternalSocket.Available; } } /// /// Gets a value indicating whether a VirtualSocket is connected to a remote resource. /// /// true if the VirtualSocket is connected to a remote resource; otherwise, false. /// Gets the connection state of the VirtualSocket. This property will return the latest known state of the VirtualSocket. When it returns false, the VirtualSocket was either never connected, or no longer connected. When it returns true, the VirtualSocket was connected at the time of the last I/O operation.
Note There is no guarantee that the VirtualSocket is still Connected even though Connected returns true.
public virtual bool Connected { get { return InternalSocket.Connected; } } /// /// Gets the operating system handle for the VirtualSocket. /// /// An representing the operating system handle for the VirtualSocket. public virtual IntPtr Handle { get { return InternalSocket.Handle; } } /// /// Gets the local endpoint. /// /// The local endpoint that the VirtualSocket is using for communications. /// The LocalEndPoint property contains the network connection information associated with the local network device. LocalEndPoint is set by calling the Bind method. /// An error occurs while reading the property. /// The VirtualSocket has been closed. public virtual EndPoint LocalEndPoint { get { return InternalSocket.LocalEndPoint; } } /// /// Gets the protocol type of the VirtualSocket. /// /// One of the values. /// ProtocolType is set when the VirtualSocket is created, and specifies the protocol used by that VirtualSocket. public virtual ProtocolType ProtocolType { get { return InternalSocket.ProtocolType; } } /// /// Gets the remote endpoint. /// /// The remote endpoint that the VirtualSocket is using for communications. /// The RemoteEndPoint property gets the network connection information associated with the remote host. RemoteEndPoint is set by VirtualSocket methods that establish a connection to a remote host. /// An error occurs while reading the property. /// The VirtualSocket has been closed. public virtual EndPoint RemoteEndPoint { get { return InternalSocket.RemoteEndPoint; } } /// /// Gets the type of the VirtualSocket. /// /// One of the values. /// SocketType is set when the class is created. public virtual SocketType SocketType { get { return InternalSocket.SocketType; } } /// /// Creates a new VirtualSocket to handle an incoming connection request. /// /// A VirtualSocket to handle an incoming connection request. /// The VirtualSocket is invalid. /// The VirtualSocket has been closed. /// The Accept method extracts the first connection request from the queue of pending requests and creates a new VirtualSocket to handle it. public virtual VirtualSocket Accept() { return new VirtualSocket(InternalAccept()); } /// /// Creates a new Socket to handle an incoming connection request. /// /// A Socket to handle an incoming connection request. /// The VirtualSocket is invalid. /// The VirtualSocket has been closed. /// The InternalAccept method extracts the first connection request from the queue of pending requests and creates a new to handle it. protected virtual Socket InternalAccept() { return InternalSocket.Accept(); } /// /// Begins an asynchronous request to create a new VirtualSocket to accept an incoming connection request. /// /// The delegate. /// An object containing state information for this request. /// An that references the asynchronous VirtualSocket creation. /// An operating system error occurs while creating the VirtualSocket. /// The VirtualSocket has been closed. /// The BeginAccept method starts an asynchronous request to create a VirtualSocket to handle an incoming connection request. You must create a callback method that implements the AsyncCallback delegate. This callback method should use the method to retrieve the VirtualSocket. public virtual IAsyncResult BeginAccept(AsyncCallback callback, object state) { return InternalSocket.BeginAccept(callback, state); } /// /// Begins an asynchronous request for a connection to a network device. /// /// An EndPoint that represents the remote device. /// The delegate. /// An object that contains state information for this request. /// An that references the asynchronous connection. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while creating the VirtualSocket. /// The VirtualSocket has been closed. /// The BeginConnect method starts an asynchronous request for a remote host connection. You must create a callback method that implements the AsyncCallback delegate. This callback method should use the method to return the VirtualSocket. public virtual IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback callback, object state) { return InternalSocket.BeginConnect(remoteEP, callback, state); } /// /// Begins to asynchronously receive data from a connected VirtualSocket. /// /// The storage location for the received data. /// The zero-based position in the buffer parameter at which to store the received data. /// The number of bytes to receive. /// A bitwise combination of the values. /// The delegate. /// An object containing state information for this request. /// An that references the asynchronous read. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// VirtualSocket has been closed. /// is outside the bounds of buffer or size is either smaller or larger than the buffer size. /// The BeginReceive method starts asynchronously reading data from a VirtualSocket. You must create a callback method that implements the AsyncCallback delegate. This callback method should use the method to return the data read from the VirtualSocket. public virtual IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) { return InternalSocket.BeginReceive(buffer, offset, size, socketFlags, callback, state); } /// /// Begins to asynchronously receive data from a specified network device. /// /// The storage location for the received data. /// The zero-based position in the buffer parameter at which to store the data. /// The number of bytes to receive. /// A bitwise combination of the values. /// An that represents the source of the data. /// The delegate. /// An object containing state information for this request. /// An that references the asynchronous read. /// is a null reference (Nothing in Visual Basic).
-or-
///
is a null reference (Nothing in Visual Basic).

-or-

is outside the bounds of buffer.
/// An operating system error occurs while accessing the VirtualSocket. /// The specified offset or size exceeds the size of buffer. /// The VirtualSocket has been closed. public virtual IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state) { return InternalSocket.BeginReceiveFrom(buffer, offset, size, socketFlags, ref remoteEP, callback, state); } /// /// Sends data asynchronously to a connected VirtualSocket. /// /// The data to send. /// The zero-based position in the buffer parameter at which to begin sending data. /// The number of bytes to send. /// A bitwise combination of the values. /// The delegate. /// An object containing state information for this request. /// An that references the asynchronous send. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The specified offset or size exceeds the size of buffer. /// The VirtualSocket has been closed. /// The BeginSend method starts asynchronously sending data through a socket. You must create a callback method that implements the AsyncCallback delegate. This callback method should use the method to complete sending data. public virtual IAsyncResult BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) { return InternalSocket.BeginSend(buffer, offset, size, socketFlags, callback, state); } /// /// Sends data asynchronously to a specific remote host. /// /// The data to send. /// The zero-based position in the buffer parameter at which to begin sending data. /// The number of bytes to send. /// A bitwise combination of the values. /// An that represents the remote device. /// The delegate. /// An object containing state information for this request. /// An that references the asynchronous send. /// is a null reference (Nothing in Visual Basic).
-or-

is a null reference (Nothing in Visual Basic).
/// An operating system error occurs while accessing the VirtualSocket. /// The specified offset or size exceeds the size of buffer. /// The VirtualSocket has been closed. /// The BeginSendTo method starts asynchronously sending data through a socket. You must create a callback method that implements the AsyncCallback delegate. This callback method should use the method to complete sending data. public virtual IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback callback, object state) { return InternalSocket.BeginSendTo(buffer, offset, size, socketFlags, remoteEP, callback, state); } /// /// Associates a VirtualSocket with a local endpoint. /// /// The local to associate with the VirtualSocket. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// If you want to use a specific local endpoint, you can call the Bind method before you call the or methods. public virtual void Bind(EndPoint localEP) { InternalSocket.Bind(localEP); } /// /// Forces a VirtualSocket connection to close. /// ///

The property is set to false when the socket is closed.

The application should call before calling Close to ensure that all pending data is sent or received before the VirtualSocket is closed.

public virtual void Close() { InternalSocket.Close(); } /// /// Establishes a connection to a remote device. /// /// An that represents the remote device. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// The Connect method establishes a network connection between and the device identified by . Once the connection has been made, you can send data to the remote device with the method, or receive data from the remote device with the method. public virtual void Connect(EndPoint remoteEP) { InternalSocket.Connect(remoteEP); } /// /// Ends an asynchronous request to create a new VirtualSocket to accept an incoming connection request. /// /// Stores state information for this asynchronous operation as well as any user defined data. /// A VirtualSocket to handle the incoming connection. /// is a null reference (Nothing in Visual Basic). /// was not created by a call to . /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// The EndAccept method completes a request for a connection that was started with the BeginAccept method. public virtual VirtualSocket EndAccept(IAsyncResult asyncResult) { return new VirtualSocket(InternalEndAccept(asyncResult)); } /// /// Ends an asynchronous request to create a new to accept an incoming connection request. /// /// Stores state information for this asynchronous operation as well as any user defined data. /// A VirtualSocket to handle the incoming connection. /// is a null reference (Nothing in Visual Basic). /// was not created by a call to . /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// The InternalEndAccept method completes a request for a connection that was started with the InternalBeginAccept method. protected virtual Socket InternalEndAccept(IAsyncResult asyncResult) { return InternalSocket.EndAccept(asyncResult); } /// /// Ends a pending asynchronous connection request. /// /// Stores state information for this asynchronous operation as well as any user-defined data. /// is a null reference (Nothing in Visual Basic). /// was not returned by a call to the method. /// EndConnect was previously called for the asynchronous connection. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// To maintain the asynchronous nature of the operation, call this method from the callback delegate. You can pass either the returned from BeginConnect or the callback delegate used as an input parameter to BeginConnect as the asyncresult parameter. The EndConnect method blocks. public virtual void EndConnect(IAsyncResult asyncResult) { InternalSocket.EndConnect(asyncResult); } /// /// Ends a pending asynchronous read. /// /// Stores state information for this asynchronous operation as well as any user defined data. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// was not returned by a call to the method. /// EndReceive was previously called for the asynchronous read. /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// To maintain the asynchronous nature of the operation, call this method from the callback delegate. You can pass either the returned from or the callback delegate used as an input parameter to BeginReceive as the asyncResult parameter. The EndReceive method blocks until the read ends. public virtual int EndReceive(IAsyncResult asyncResult) { return InternalSocket.EndReceive(asyncResult); } /// /// Ends a pending asynchronous read from a specific endpoint. /// /// Stores state information for this asynchronous operation as well as any user defined data. /// The source . /// If successful, the number of bytes received. If unsuccessful, returns 0 if the connection is closed by the remote host. /// is a null reference (Nothing in Visual Basic). /// was not returned by a call to the BeginReceiveFrom method. /// EndReceiveFrom was previously called for the asynchronous read. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// To maintain the asynchronous nature of the operation, call this method from the callback delegate. You can pass either the returned from or the callback delegate used as an input parameter to BeginReceiveFrom. as the asyncResult parameter. The EndReceiveFrom method frees any resources allocated by the BeginReceiveFrom method. The EndReceiveFrom method blocks until read ends. public virtual int EndReceiveFrom(IAsyncResult asyncResult, ref EndPoint endPoint) { return InternalSocket.EndReceiveFrom(asyncResult, ref endPoint); } /// /// Ends a pending asynchronous send. /// /// Stores state information for this asynchronous operation as well as any user defined data. /// If successful, the number of bytes sent to the VirtualSocket; otherwise, an invalid VirtualSocket error. /// is a null reference (Nothing in Visual Basic). /// was not returned by a call to the method. /// EndSend was previously called for the asynchronous read. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// To maintain the asynchronous nature of the operation, call this method from the callback delegate. You can pass either the returned from or the callback delegate used as an input parameter to BeginSend as the asyncResult parameter. The EndSend method frees any resources allocated by the BeginSend method. The EndSend method blocks until the send ends.
The EndSend method frees any resources allocated by the BeginSend method.
public virtual int EndSend(IAsyncResult asyncResult) { return InternalSocket.EndSend(asyncResult); } /// /// Ends a pending asynchronous send to a specific location. /// /// Stores state information for this asynchronous operation as well as any user defined data . /// If successful, the number of bytes sent; otherwise, an invalid VirtualSocket error. /// is a null reference (Nothing in Visual Basic). /// was not returned by a call to the method. /// EndSendTo was previously called for the asynchronous read. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. ///
To maintain the asynchronous nature of the operation, call this method from the callback delegate. You can pass either the returned from or the callback delegate used as an input parameter to BeginSendTo as the asyncResult parameter. The EndSendTo method frees any resources allocated by the BeginSendTo method. The EndSendTo method blocks until send is complete.

The EndSendTo method frees any resources allocated by the BeginSendTo method.
public virtual int EndSendTo(IAsyncResult asyncResult) { return InternalSocket.EndSendTo(asyncResult); } /// /// This member overrides Object.GetHashCode. /// /// A hash code for the current VirtualSocket. public override int GetHashCode() { return InternalSocket.GetHashCode(); } /// /// Gets the value of a specified socket option. /// /// One of the values. /// One of the values. /// The value of the option. When the optionName parameter is set to Linger the return value is an instance of the LingerOption. When optionName is set to AddMembership or DropMembership, the return value is an instance of the MulticastOption. When optionName is any other value, the return value is an integer. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// VirtualSocket options determine the behavior of the current instance. Upon successful completion, GetSocketOption returns an object describing the requested option. For example, if you specify Linger as the option, a LingerOption is returned. public virtual object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName) { return InternalSocket.GetSocketOption(optionLevel, optionName); } /// /// Gets the specified VirtualSocket option setting. /// /// One of the values. /// One of the values. /// The buffer that is to receive the option setting. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// Socket options determine the behavior of the current Socket. Upon successful completion of this method, the array specified by the optionValue parameter contains the value of the specified Socket option. When the length of the optionValue array is smaller than the number of bytes required to store the value of the specified Socket option, a is thrown. public virtual void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue) { InternalSocket.GetSocketOption(optionLevel, optionName, optionValue); } /// /// Returns the value of the specified Socket option and returns in an array. /// /// One of the values. /// One of the values. /// The length, in bytes, of the expected return value. /// An array of bytes containing the value of the socket option. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// The optionLength parameter sets the maximum size of the returned byte array. If the option value requires fewer bytes, the array will contain only that many bytes. If the option value requires more bytes, a SocketException will be thrown. public virtual byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength) { return InternalSocket.GetSocketOption(optionLevel, optionName, optionLength); } /// /// Sets low-level operating modes for the VirtualSocket. /// /// The control code of the operation to perform. /// The input data required by the operation. /// The output data returned by the operation. /// The number of bytes in optionOutValue parameter. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// The IOControl method provides low-level access to the operating system socket underlying the current instance of the VirtualSocket class. For more information about IOControl, see the WSAIoct documentation in MSDN. public virtual int IOControl(int ioControlCode, byte[] optionInValue, byte[] optionOutValue) { return InternalSocket.IOControl(ioControlCode, optionInValue, optionOutValue); } /// /// Places a VirtualSocket in a listening state. /// /// The Maximum length of the queue of pending connections. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// In a listening state, the VirtualSocket will poll for incoming connection attempts. If you want to listen using a specific network interface on a specific port, you must call the Bind method first. public virtual void Listen(int backlog) { InternalSocket.Listen(backlog); } /// /// Determines the status of the VirtualSocket. /// /// The time to wait for a response, in microseconds. /// One of the values. /// See the Socket documentation for the return values. /// The mode parameter is not one of the SelectMode values. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// Set microSeconds parameter to a negative integer if you would like to wait indefinitely for a response. public virtual bool Poll(int microSeconds, SelectMode mode) { return InternalSocket.Poll(microSeconds, mode); } /// /// Receives data from a connected VirtualSocket in a specific location of the receive buffer. /// /// The storage location for received data. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

This overload only requires you to provide a receive buffer. The offset defaults to 0, size defaults to the buffer length, and the socketFlags value defaults to None.

///

The Blocking determines the behavior of this method when no incoming data is available. When false, a SocketException is thrown. When true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all the data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

Note If you specify the OutOfBand flag as the socketFlags parameter, and the Socket is configured for in-line reception of out-of-band (OOB) data (using the OutOfBandInline option) and OOB data is available, then only OOB data is returned. When the Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but is not removed from the system buffer.

///
public virtual int Receive(byte[] buffer) { return InternalSocket.Receive(buffer); } /// /// Receives data from a connected VirtualSocket in a specific location of the receive buffer. /// /// The storage location for received data. /// A bitwise combination of the values. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

This overload only requires you to provide a receive buffer and the necessary SocketFlags. The offset defaults to 0, and the size defaults to the buffer length.

///

The Blocking determines the behavior of this method when no incoming data is available. When false, a SocketException is thrown. When true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all the data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

Note If you specify the OutOfBand flag as the socketFlags parameter, and the Socket is configured for in-line reception of out-of-band (OOB) data (using the OutOfBandInline option) and OOB data is available, then only OOB data is returned. When the Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but is not removed from the system buffer.

///
public virtual int Receive(byte[] buffer, SocketFlags socketFlags) { return InternalSocket.Receive(buffer, socketFlags); } /// /// Receives data from a connected VirtualSocket in a specific location of the receive buffer. /// /// The storage location for received data. /// The number of bytes to receive. /// A bitwise combination of the SocketFlags values. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// The size exceeds the size of buffer. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

This overload only requires you to provide a receive buffer, the number of bytes you want to send, and the necessary SocketFlags. The offset defaults to 0.

///

The Blocking determines the behavior of this method when no incoming data is available. When false, a SocketException is thrown. When true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all the data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

Note If you specify the OutOfBand flag as the socketFlags parameter, and the Socket is configured for in-line reception of out-of-band (OOB) data (using the OutOfBandInline option) and OOB data is available, then only OOB data is returned. When the Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but is not removed from the system buffer.

///
public virtual int Receive(byte[] buffer, int size, SocketFlags socketFlags) { return InternalSocket.Receive(buffer, size, socketFlags); } /// /// Receives data from a connected VirtualSocket in a specific location of the receive buffer. /// /// The storage location for received data. /// The location in buffer to store the received data. /// The number of bytes to receive. /// A bitwise combination of the SocketFlags values. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// The specified offset or size exceeds the size of buffer. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

The Blocking determines the behavior of this method when no incoming data is available. When false, a SocketException is thrown. When true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all the data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

Note If you specify the OutOfBand flag as the socketFlags parameter, and the Socket is configured for in-line reception of out-of-band (OOB) data (using the OutOfBandInline option) and OOB data is available, then only OOB data is returned. When the Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but is not removed from the system buffer.

///
public virtual int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags) { return InternalSocket.Receive(buffer, offset, size, socketFlags); } /// /// Receives a datagram in a specific location in the data buffer and stores the endpoint. /// /// The storage location for received data. /// An , passed by reference, that represents the remote server. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

This overload only requires you to provide a receive buffer, and EndPoint representing the remote host. The offset defaults to 0. The size defaults to the buffer length and the socketFlags value defaults to None.

///

If you use a connectionless protocol, the remoteEP parameter contains the EndPoint associated with the Socket that sent the data. If you use a connection-oriented protocol, remoteEP is left unchanged. You must set the LocalEndPoint property before calling this method. When no incoming data is available, and the Blocking property is false, a SocketException is thrown. When Blocking is true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, then buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

When the OutOfBand flag is specified as the socketFlags parameter and the Socket is configured for in-line reception of out-of-band (OOB) data (using OutOfBandInline) and OOB data is available, only OOB data is returned. When the SocketFlags. Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but not removed from the system buffer.

///
public virtual int ReceiveFrom(byte[] buffer, ref EndPoint remoteEP) { return InternalSocket.ReceiveFrom(buffer, ref remoteEP); } /// /// Receives a datagram in a specific location in the data buffer and stores the endpoint. /// /// The storage location for received data. /// A bitwise combination of the SocketFlags values. /// An , passed by reference, that represents the remote server. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

This overload only requires you to provide a receive buffer, the necessary SocketFlags, and the EndPoint representing the remote host. The offset defaults to 0. The size defaults to the buffer length.

///

If you use a connectionless protocol, the remoteEP parameter contains the EndPoint associated with the Socket that sent the data. If you use a connection-oriented protocol, remoteEP is left unchanged. You must set the LocalEndPoint property before calling this method. When no incoming data is available, and the Blocking property is false, a SocketException is thrown. When Blocking is true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, then buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

When the OutOfBand flag is specified as the socketFlags parameter and the Socket is configured for in-line reception of out-of-band (OOB) data (using OutOfBandInline) and OOB data is available, only OOB data is returned. When the SocketFlags. Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but not removed from the system buffer.

///
public virtual int ReceiveFrom(byte[] buffer, SocketFlags socketFlags, ref EndPoint remoteEP) { return InternalSocket.ReceiveFrom(buffer, socketFlags, ref remoteEP); } /// /// Receives a datagram in a specific location in the data buffer and stores the endpoint. /// /// The storage location for received data. /// The number of bytes to receive. /// A bitwise combination of the SocketFlags values. /// An , passed by reference, that represents the remote server. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// The size parameter exceeds the size of buffer. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

This overload only requires you to provide a receive buffer, the number of bytes you want to receive, the necessary SocketFlags, and the EndPoint representing the remote host. The offset defaults to 0.

///

If you use a connectionless protocol, the remoteEP parameter contains the EndPoint associated with the Socket that sent the data. If you use a connection-oriented protocol, remoteEP is left unchanged. You must set the LocalEndPoint property before calling this method. When no incoming data is available, and the Blocking property is false, a SocketException is thrown. When Blocking is true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, then buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

When the OutOfBand flag is specified as the socketFlags parameter and the Socket is configured for in-line reception of out-of-band (OOB) data (using OutOfBandInline) and OOB data is available, only OOB data is returned. When the SocketFlags. Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but not removed from the system buffer.

///
public virtual int ReceiveFrom(byte[] buffer, int size, SocketFlags socketFlags, ref EndPoint remoteEP) { return InternalSocket.ReceiveFrom(buffer, size, socketFlags, ref remoteEP); } /// /// Receives a datagram in a specific location in the data buffer and stores the endpoint. /// /// The storage location for received data. /// The position in the buffer parameter to store the received data. /// The number of bytes to receive. /// A bitwise combination of the SocketFlags values. /// An , passed by reference, that represents the remote server. /// The number of bytes received. /// is a null reference (Nothing in Visual Basic). /// The specified offset or size exceeds the size of buffer. /// An operating system error occurs while accessing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

If you use a connectionless protocol, the remoteEP parameter contains the EndPoint associated with the Socket that sent the data. If you use a connection-oriented protocol, remoteEP is left unchanged. You must set the LocalEndPoint property before calling this method. When no incoming data is available, and the Blocking property is false, a SocketException is thrown. When Blocking is true, this method blocks and waits for data to arrive. For Stream Socket types, if the remote Socket was shut down gracefully, and all data was received, this method immediately returns zero, regardless of the blocking state.

///

If you are using a message-oriented Socket, and the message is larger than the size of the buffer parameter, then buffer is filled with the first part of the message, and a SocketException is thrown. With unreliable protocols the excess data is lost; with reliable protocols, the data is retained by the service provider.

///

When the OutOfBand flag is specified as the socketFlags parameter and the Socket is configured for in-line reception of out-of-band (OOB) data (using OutOfBandInline) and OOB data is available, only OOB data is returned. When the SocketFlags. Peek flag is specified as the socketFlags parameter, available data is copied into the receive buffer but not removed from the system buffer.

///
public virtual int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP) { return InternalSocket.ReceiveFrom(buffer, offset, size, socketFlags, ref remoteEP); } /// /// Sends data to a connected VirtualSocket, starting at the indicated location in the data. /// /// The data to be sent. /// The number of bytes sent to the VirtualSocket. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

Use Send for connection-oriented protocols only. For connectionless protocols, either use SendTo or call Connect first, and then call Send. This overload only requires you to provide a data buffer. The offset defaults to 0, the size defaults to the buffer length, and SocketFlags value defaults to None.

///

You must set the LocalEndPoint property of the current instance before calling this method.

///
public virtual int Send(byte[] buffer) { return InternalSocket.Send(buffer); } /// /// Sends data to a connected VirtualSocket, starting at the indicated location in the data. /// /// The data to be sent. /// A bitwise combination of the SocketFlags values. /// The number of bytes sent to the VirtualSocket. /// is a null reference (Nothing in Visual Basic). /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

Use Send for connection-oriented protocols only. For connectionless protocols, either use SendTo or call Connect first, and then call Send.

///

This overload only requires you to provide a data buffer and SocketFlags. The offset defaults to 0, and the size parameter defaults to the buffer length.

///

You must set the LocalEndPoint property of the current instance before calling this method.

///

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed. If you specify the OutOfBand flag as the socketflags parameter, only out-of-band (OOB) data is sent.

///

If you set the Blocking property to true, and buffer space is not available within the underlying protocol, this method blocks.

///

If you are using a message-oriented Socket, and the size of the buffer is greater than the maximum message size of the underlying protocol, no data is sent and Socket will throw a SocketException.

///
public virtual int Send(byte[] buffer, SocketFlags socketFlags) { return InternalSocket.Send(buffer, socketFlags); } /// /// Sends data to a connected VirtualSocket, starting at the indicated location in the data. /// /// The data to be sent. /// The number of bytes to send. /// A bitwise combination of the SocketFlags values. /// The number of bytes sent to the VirtualSocket. /// is a null reference (Nothing in Visual Basic). /// The size parameter exceeds the size of buffer. /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

Use Send for connection-oriented protocols only. For connectionless protocols, either use SendTo or call Connect first, and then call Send.

///

This overload only requires you to provide a data buffer, SocketFlags, and the number bytes to be sent. The offset defaults to 0.

///

You must set the LocalEndPoint property of the current instance before calling this method.

///

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed. If you specify the OutOfBand flag as the socketflags parameter, only out-of-band (OOB) data is sent.

///

If you set the Blocking property to true, and buffer space is not available within the underlying protocol, this method blocks.

///

If you are using a message-oriented Socket, and the size of the buffer is greater than the maximum message size of the underlying protocol, no data is sent and Socket will throw a SocketException.

///
public virtual int Send(byte[] buffer, int size, SocketFlags socketFlags) { return InternalSocket.Send(buffer, size, socketFlags); } /// /// Sends data to a connected VirtualSocket, starting at the indicated location in the data. /// /// The data to be sent. /// The position in the data buffer to begin sending data. /// The number of bytes to send. /// A bitwise combination of the SocketFlags values. /// The number of bytes sent to the VirtualSocket. /// is a null reference (Nothing in Visual Basic). /// The offset or size parameter exceeds the size of buffer. /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

Use Send for connection-oriented protocols only. For connectionless protocols, either use SendTo or call Connect first, and then call Send.

///

This overload gives you the flexibility to specify the Send starting position in the data buffer, the number bytes you are sending, and the necessary SocketFlags.

///

You must set the LocalEndPoint property of the current instance before calling this method.

///

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed. If you specify the OutOfBand flag as the socketflags parameter, only out-of-band (OOB) data is sent.

///

If you set the Blocking property to true, and buffer space is not available within the underlying protocol, this method blocks.

///

If you are using a message-oriented Socket, and the size of the buffer is greater than the maximum message size of the underlying protocol, no data is sent and Socket will throw a SocketException.

///
public virtual int Send(byte[] buffer, int offset, int size, SocketFlags socketFlags) { return InternalSocket.Send(buffer, offset, size, socketFlags); } /// /// Sends data to a specific endpoint, starting at the indicated location in the data. /// /// The data to be sent. /// The representing the destination location for the data. /// The number of bytes sent. /// is a null reference (Nothing in Visual Basic).
-or-

The remoteEP parameter is a null reference (Nothing).
/// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

If you are using a connection-oriented protocol or a connected Socket using a connectionless protocol, remoteEP overrides the endpoint specified in RemoteEndPoint. If you are using an unconnected Socket with a connectionless protocol, this method sets the LocalEndPoint property of the current instance to a value determined by the protocol. You must subsequently receive data on the LocalEndPoint. This overload only requires you to provide a data buffer, and the remote EndPoint. The offset defaults to 0. The size defaults to the buffer length, and SocketFlags value defaults to None.

///
public virtual int SendTo(byte[] buffer, EndPoint remoteEP) { return InternalSocket.SendTo(buffer, remoteEP); } /// /// Sends data to a specific endpoint, starting at the indicated location in the data. /// /// The data to be sent. /// A bitwise combination of the SocketFlags values. /// The representing the destination location for the data. /// The number of bytes sent. /// is a null reference (Nothing in Visual Basic).
-or-

The remoteEP parameter is a null reference (Nothing).
/// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

If you are using a connection-oriented protocol or a connected Socket using a connectionless protocol, remoteEP overrides the endpoint specified in RemoteEndPoint. If you are using an unconnected Socket with a connectionless protocol, this method sets the LocalEndPoint property of the current instance to a value determined by the protocol. You must subsequently receive data on the LocalEndPoint.

///

This overload only requires you to provide a data buffer, SocketFlags, and the remote EndPoint. The offset defaults to 0, and size defaults to the buffer length.

///

Note If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed. If you specify the OutOfBand flag as the socketflags parameter, only out-of-band (OOB) data is sent. If you set the Blocking property to true, and buffer space is not available within the underlying protocol, this method blocks. If you are using a message-oriented Socket, and the size of buffer is greater than the maximum message size of the underlying protocol, no data is sent and Socket will throw a SocketException. If you are using a connection-oriented Socket, remoteEp is ignored.

///
public virtual int SendTo(byte[] buffer, SocketFlags socketFlags, EndPoint remoteEP) { return InternalSocket.SendTo(buffer, socketFlags, remoteEP); } /// /// Sends data to a specific endpoint, starting at the indicated location in the data. /// /// The data to be sent. /// The number of bytes to send. /// A bitwise combination of the values. /// The representing the destination location for the data. /// The number of bytes sent. /// is a null reference (Nothing in Visual Basic).
-or-

The remoteEP parameter is a null reference (Nothing).
/// The specified size exceeds the size of buffer. /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

If you are using a connection-oriented protocol or a connected Socket using a connectionless protocol, remoteEP overrides the endpoint specified in RemoteEndPoint. If you are using an unconnected Socket with a connectionless protocol, this method sets the LocalEndPoint property of the current instance to a value determined by the protocol. You must subsequently receive data on the LocalEndPoint.

///

This overload only requires you to provide a data buffer, SocketFlags, the number bytes to be sent and the remote EndPoint. The offset defaults to 0.

///

Note If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed. If you specify the OutOfBand flag as the socketflags parameter, only out-of-band (OOB) data is sent. If you set the Blocking property to true, and buffer space is not available within the underlying protocol, this method blocks. If you are using a message-oriented Socket, and the size of buffer is greater than the maximum message size of the underlying protocol, no data is sent and Socket will throw a SocketException. If you are using a connection-oriented Socket, remoteEp is ignored.

///
public virtual int SendTo(byte[] buffer, int size, SocketFlags socketFlags, EndPoint remoteEP) { return InternalSocket.SendTo(buffer, size, socketFlags, remoteEP); } /// /// Sends data to a specific endpoint, starting at the indicated location in the data. /// /// The data to be sent. /// The position in the data buffer to begin sending data. /// The number of bytes to send. /// A bitwise combination of the values. /// The representing the destination location for the data. /// The number of bytes sent. /// is a null reference (Nothing in Visual Basic).
-or-

The remoteEP parameter is a null reference (Nothing).
/// The offset or size parameter exceeds the size of buffer. /// An operating system error occurs while accessing the socket. /// The VirtualSocket has been closed. /// ///

If you are using a connection-oriented protocol or a connected Socket using a connectionless protocol, remoteEP overrides the endpoint specified in RemoteEndPoint. If you are using an unconnected Socket with a connectionless protocol, this method sets the LocalEndPoint property of the current instance to a value determined by the protocol. You must subsequently receive data on the LocalEndPoint.

///

Note If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed. If you specify the OutOfBand flag as the socketflags parameter, only out-of-band (OOB) data is sent. If you set the Blocking property to true, and buffer space is not available within the underlying protocol, this method blocks. If you are using a message-oriented Socket, and the size of buffer is greater than the maximum message size of the underlying protocol, no data is sent and Socket will throw a SocketException. If you are using a connection-oriented Socket, remoteEp is ignored.

///
public virtual int SendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP) { return InternalSocket.SendTo(buffer, offset, size, socketFlags, remoteEP); } /// /// Sets the specified option to the specified value. /// /// A value. /// A value. /// A byte array representing the value of the option. /// The VirtualSocket has been closed. /// The VirtualSocket has been closed. /// Socket options determine the behavior of the current Socket. Use this overload to set those Socket options that require a byte array as an option value.
Windows 98, Windows NT 4.0 Platform Note: You must call the Bind method before using AddMembership as the optionName parameter.
public virtual void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue) { InternalSocket.SetSocketOption(optionLevel, optionName, optionValue); } /// /// Sets the specified option to the specified value. /// /// A value. /// A value. /// A value of the option. /// The VirtualSocket has been closed. /// The VirtualSocket has been closed. /// Socket options determine the behavior of the current Socket. For an option with a Boolean data type, specify a nonzero value to enable the option, and a zero value to disable the option. For an option with an integer data type, specify the appropriate value. Socket options are grouped by level of protocol support.
Windows 98, Windows NT 4.0 Platform Note: You must call the Bind method before using AddMembership as the optionName parameter.
public virtual void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue) { InternalSocket.SetSocketOption(optionLevel, optionName, optionValue); } /// /// Sets the specified option to the specified value. /// /// A value. /// A value. /// A LingerOption or MulticastOption containing the value of the option. /// The VirtualSocket has been closed. /// The VirtualSocket has been closed. /// is a null reference (Nothing in Visual Basic). /// Socket options determine the behavior of the current Socket. Use this overload to set those Socket options that require anything other than an integer or Boolean as an option value. For example, to set the Linger option, you must create an instance of LingerOption and pass it to SetSocketOption as the optionvalue parameter.
Windows 98, Windows NT 4.0 Platform Note: You must call the Bind method before using AddMembership as the optionName parameter.
public virtual void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue) { InternalSocket.SetSocketOption(optionLevel, optionName, optionValue); } /// /// Disables sends and receives on a VirtualSocket. /// /// The value specifying the operation that will no longer be allowed. /// An error occurs while closing the VirtualSocket. /// The VirtualSocket has been closed. /// ///

Setting how to Send, specifies that subsequent calls to Send are not allowed. With TCP sockets, a FIN will be sent after all data is sent and acknowledged by the receiver.

///

Setting how to Receive, specifies that subsequent calls to Receive are not allowed. This has no effect on lower protocol layers. For TCP sockets, the connection is reset if data is waiting to be received or if more data arrives after the Socket is disabled. For UDP sockets, datagrams are accepted and queued.

///

Setting how to Both disables both sends and receives as described above.

///

To finish closing the Socket, a call to Close must be made after the call to Shutdown. You should not attempt to reuse the Socket.

///
public virtual void Shutdown(SocketShutdown how) { InternalSocket.Shutdown(how); } /// Holds the value of the property. private Socket m_InternalSocket; } }