Establishes a server stream which listens on the given URI using blocking or non-blocking I/O.

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

Syntax

Visual Basic (Declaration)
Public Sub Listen ( _
	uri As String, _
	nonBlocking As Boolean _
)
C#
public void Listen(
	string uri,
	bool nonBlocking
)
Visual C++
public:
void Listen(
	String^ uri, 
	bool nonBlocking
)
JavaScript
function listen(uri, nonBlocking);

Parameters

uri
Type: System..::.String
The URI indicating the stream upon which to listen.
nonBlocking
Type: System..::.Boolean
Set to true to prevent Accept calls from blocking. Any client connections accepted will also be non-blocking. If this parameter is set to false then the Accept call will block until a client connects or Shutdown()()() is invoked, and any client connections accepted will also use blocking I/O.

Remarks

This method establishes a server stream which listens on the given URI. The URI specifies the protocol, address, port and options associated with the server stream. The Stream object uses the protocol to load a protocol-specific driver. For example:

URIDescription
tcpip://localhost:17000 Listen on port 17000 using TCP/IP.
shmem://mymemory:1?bufsize=8192 Listen via a shared memory buffer. Use 8K buffers by default.
pipe:mypipe?bufsize=4096 Listen via a named pipe. Use 4K buffers for the pipe.

Examples

This example listens on TCP/IP port 18000 for a connection from a client using non-blocking I/O.
C# Copy Code
Stream server = new Stream();
String uri    = "tcpip://localhost:18000";
bool   done   = false;
Stream client;

try {
    server.Listen(uri, true);
    try {
        while (!done) {
            client = server.Accept(); /* poll for client connections */
            if (client != nullptr) {  /* a client has connected */

                /* ... communicate with client using non-blocking I/O ... */

                client.Close();
            } else {
                /* ... do other work since Accept always returns immediately ... */
            }
        }
    } 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, True)
    Try
        While Not done Do
            client = server.Accept()         ' poll for client connections
            If client != System.DBNull Then  ' a client has connected

                ' ... communicate with client using non-blocking I/O ...

                client.Close()
            Else
                ' ... do other work since Accept always returns immediately ...
            End If
        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, true);
    try {
        while (!done) {
            client = server->Accept(); /* poll for client connections */
            if (client != nullptr) {   /* a client has connected */

                /* ... communicate with client using non-blocking I/O ... */

                client->Close();
            } else {
                /* ... do other work since Accept always returns immediately ... */
            }
        }
    } 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 a listening stream cannot be opened then an exception is thrown. This situation typically arises if the underlying protocol cannot acquire the resources it needs. For example, the TCP/IP driver may not be able to listen on a given port because another server is already using that port.

See Also