Connects to a server listening 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 Function Connect ( _
uri As String, _
nonBlocking As Boolean, _
sendBufferSize As Integer, _
receiveBufferSize As Integer _
) As Boolean |
C# |
---|
public bool Connect(
string uri,
bool nonBlocking,
int sendBufferSize,
int receiveBufferSize
) |
Visual C++ |
---|
public:
bool Connect(
String^ uri,
bool nonBlocking,
int sendBufferSize,
int receiveBufferSize
) |
JavaScript |
---|
function connect(uri, nonBlocking, sendBufferSize, receiveBufferSize); |
Parameters
- uri
- Type: System..::.String
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.
- sendBufferSize
- Type: System..::.Int32
The size of the buffer to use for sending data over the stream, in bytes. This buffer
is referred to as the stream send buffer in this documentation.
- receiveBufferSize
- Type: System..::.Int32
The size of the buffer to use for receiveing data over the stream, in bytes. This buffer
is referred to as the stream receive buffer in this documentation.
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
Examples
This example connects to a remote host serving on TCP/IP port 18000 using
non-blocking I/O.
C# | Copy Code |
---|
Timeout timeout = new Timeout(30); /* 30 seconds */
Stream stream = new Stream();
String uri = "tcpip://localhost:18000";
int sendBufferSize = 1024;
int receiveBufferSize = 1024;
bool done = false;
try {
if (!stream.Connect(uri, true, sendBufferSize, receiveBufferSize)) {
/* 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 String = "tcpip://localhost:18000"
Dim sendBufferSize As Integer = 1024
Dim receiveBufferSize As Integer = 1024
Dim done As Boolean = False
Try
If Not stream.Connect(uri, true, sendBufferSize, receiveBufferSize) 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();
String^ uri = L"tcpip://localhost:18000";
int sendBufferSize = 1024;
int receiveBufferSize = 1024;
bool done = false;
try {
if (!stream->Connect(uri, true, sendBufferSize, receiveBufferSize)) {
/* 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.
|
See Also