Assembly: Quanser.Communications (in Quanser.Communications.dll)
Syntax
Visual Basic (Declaration) |
---|
Public Function Connect ( _ uri As Uri, _ nonBlocking As Boolean _ ) As Boolean |
C# |
---|
public bool Connect( Uri uri, bool nonBlocking ) |
Visual C++ |
---|
public: bool Connect( Uri^ uri, bool nonBlocking ) |
JavaScript |
---|
function connect(uri, nonBlocking); |
Parameters
- uri
- Type: System..::.Uri
The URI indicating the listening stream to which to connect.
- nonBlocking
- Type: System..::.Boolean
Set to true to make this connection use non-blocking I/O. Otherwise blocking I/O will be used.
Return Value
This method returns true if the connection is established. If non-blocking I/O is being used, then it may also return false, which indicates that the connection is in progress but has not yet been completed. Finish establishing the connection, in this case, by using the Poll(Timeout, Int32) method with the Connect flag.Remarks
This method connects to a listening stream referenced by 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:
URI | Description |
---|---|
tcpip://remhost:17000 | Connect to a remote host called 'remhost' on port 17000 using TCP/IP. |
shmem://mymemory:1?bufsize=8192 | Connect via a shared memory buffer to a server. Use 8K buffers by default. |
pipe:mypipe?bufsize=4096 | Connect via a named pipe to a server. Use 4K buffers for the pipe. |
If the nonBlocking flag is set to false, then this method will block until the connection is made. Some protocols allow a timeout to be specified in the URI or have a default timeout such that the Connect will not block indefinitely.
If the nonBlocking flag is set to true, then this method will not block. If the connection is completed immediately then true is returned. If the connection cannot be completed immediately then false is returned. In this case, the connection may be completed using the Poll(Timeout, Int32) method with the Connect flag.
Examples
C# | Copy Code |
---|---|
Timeout timeout = new Timeout(30); /* 30 seconds */ Stream stream = new Stream(); Uri uri = new Uri("tcpip://localhost:18000"); bool done = false; try { if (!stream.Connect(uri, true)) { /* Connection is in progress. Wait up to 30 seconds for connection to complete. */ if (stream.Poll(timeout, Stream.PollFlag.Connect) != Stream.PollFlag.Connect) { throw new Exception("Timed out waiting to connect"); } } try { /* ... communicate with client using non-blocking I/O ... */ } catch (Exception ex) { Console.WriteLine("Error communicating with client on URI '" + uri + "'. " + ex); } stream.Close(); } catch (Exception ex) { Console.WriteLine("Unable to connect to URI '" + uri + "'. " + ex); } |
Visual Basic | Copy Code |
---|---|
Dim timeout As New Timeout(30) ' 30 seconds Dim stream As New Stream() Dim uri As As New Uri("tcpip://localhost:18000") Dim done As Boolean = False Try If Not stream.Connect(uri, true) Then ' Connection is in progress. Wait up to 30 seconds for connection to complete. If stream.Poll(timeout, Stream.PollFlag.Connect) <> Stream.PollFlag.Connect Then Throw New Exception("Timed out waiting to connect") End If End If Try ' ... communicate with client using non-blocking I/O ... Catch ex As Exception Console.WriteLine("Error communicating with client on URI '" & uri & "'. " & ex.ToString()) End Try stream.Close() Catch ex As Exception Console.WriteLine("Unable to connect to URI '" & uri & "'. " & ex.ToString()) End Try |
Visual C++ | Copy Code |
---|---|
Timeout^ timeout = gcnew Timeout(30); /* 30 seconds */ Stream^ stream = new Stream(); Uri^ uri = gcnew Uri(L"tcpip://localhost:18000"); bool done = false; try { if (!stream->Connect(uri, true)) { /* Connection is in progress. Wait up to 30 seconds for connection to complete. */ if (stream->Poll(timeout, Stream::PollFlag::Connect) != Stream::PollFlag::Connect) { throw gcnew Exception(L"Timed out waiting to connect"); } } try { /* ... communicate with client using non-blocking I/O ... */ } catch (Exception ex) { Console::WriteLine("Error communicating with client on URI '" + uri + "'. " + ex); } stream->Close(); } catch (Exception ex) { Console::WriteLine("Unable to connect to URI '" + uri + "'. " + ex); } |
Exceptions
Exception | Condition |
---|---|
Quanser.Communications..::.StreamException | If the connection cannot be made then an exception is thrown. This situation typically arises if there is no server listening on the given URI. For non-blocking I/O, an exception may not be thrown until the Poll(Timeout, Int32) call if the Connect method returns false, since the connection is still in progress in that case. |