Establishes a listening server stream which listens on the given URI
using blocking I/O.
Namespace:
Quanser.CommunicationsAssembly: Quanser.Communications (in Quanser.Communications.dll)
Syntax
| Visual Basic (Declaration) |
|---|
Public Sub Listen ( _ uri As Uri _ ) |
| C# |
|---|
public void Listen( Uri uri ) |
| Visual C++ |
|---|
public: void Listen( Uri^ uri ) |
| JavaScript |
|---|
function listen(uri); |
Parameters
- uri
- Type: System..::.Uri
The URI indicating the stream upon which to listen.
Remarks
This method establishes a server stream which listens on the given URI. It uses blocking I/O. To use non-blocking I/O, use the Listen(String, Boolean) method. 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:
| URI | Description |
|---|---|
| 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.
| C# | |
|---|---|
Stream server = new Stream();
Uri uri = new 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 | |
|---|---|
Dim server As New Stream()
Dim uri As New Uri("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++ | |
|---|---|
Stream^ server = gcnew Stream();
Uri^ uri = gcnew(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
| Exception | Condition |
|---|---|
| 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. |