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
| C# | |
|---|---|
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 | |
|---|---|
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++ | |
|---|---|
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
| Exception | Condition |
|---|---|
| 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. |