Accepts a connection to a listening communication stream by a client and uses default send and receive buffer sizes.

Namespace:  Quanser.Communications
Assembly:  Quanser.Communications (in Quanser.Communications.dll)

Syntax

Visual Basic (Declaration)
Public Function Accept As Stream
C#
public Stream Accept()
Visual C++
public:
Stream^ Accept()
JavaScript
function accept();

Return Value

This method returns a Stream object representing a new connection to the client if the connection is established. If non-blocking I/O is being used, then it may also return nullptr, which indicates that no client connection was pending. Use the Poll(Timeout, Int32) method with the Accept flag to determine when a client connection is pending.

Remarks

This method accepts a connection to a listening communication stream by a client. The client connects using one of the Connect methods.

If the Listen method that created the stream was called with nonBlocking flag set to false then this call will block until a client connects. The client stream returned will also be a blocking stream.

If the Listen method that created the stream was called with nonBlocking flag set to true then this call will not block. If there is no pending client connection then it will return a nullptr (or System.DBNull in Visual Basic). The Poll(Timeout, Int32) method may be used with the Accept flag to determine when a client connection is pending. In this case, the client stream returned will also be a non-blocking stream.

Examples

This example listens on TCP/IP port 18000 for a connection from a client, and uses the default send and receive buffer sizes.
C# Copy Code
Stream server = new Stream();
String uri    = "tcpip://localhost:18000";
bool   done   = false;
Stream client;

try {
    server.Listen(uri);
    try {
        while (!done) {
            client = server.Accept();

            /* ... communicate with client ... */

            client.Close();
        }
    } catch (Exception ex) {
        Console.WriteLine("Error communicating with client on URI '" + uri + "'. " + ex);
    }
    server.Close();
} catch (Exception ex) {
    Console.WriteLine("Unable to listen on URI '" + uri + "'. " + ex);
}
Visual Basic Copy Code
Dim server As New Stream()
Dim uri As String = "tcpip://localhost:18000"
Dim done As Boolean = False
Dim client As Stream

Try
    server.Listen(uri)
    Try
        While Not done Do
            client = server.Accept()

            ' ... communicate with client ...

            client.Close()
        End While
    Catch ex As Exception
        Console.WriteLine("Error communicating with client on URI '" & uri & "'. " & ex.ToString())
    End Try
    server.Close()
Catch ex As Exception
    Console.WriteLine("Unable to listen on URI '" & uri & "'. " & ex.ToString())
End Try
Visual C++ Copy Code
Stream^ server = gcnew Stream();
String^ uri    = L"tcpip://localhost:18000";
bool    done   = false;
Stream^ client;

try {
    server->Listen(uri);
    try {
        while (!done) {
            client = server->Accept();

            /* ... communicate with client ... */

            client->Close();
        }
    } catch (Exception ex) {
        Console::WriteLine(L"Error communicating with client on URI '" + uri + "'. " + ex);
    }
    server->Close();
} catch (Exception ex) {
    Console::WriteLine(L"Unable to listen on URI '" + uri + "'. " + ex);
}

Exceptions

ExceptionCondition
Quanser.Communications..::.StreamException If the connection cannot be accepted then an exception is thrown. This situation typically arises if the Accept method is called on a connected stream rather than a listening stream.

See Also