Dart.PowerTCP.SslSockets Namespace > Server Class : Connections Property |
Returns an ArrayList containing Tcp objects representing active connections.
[Visual Basic]
<BrowsableAttribute(False)>
Public ReadOnly Property Connections As ArrayList
[C#]
[BrowsableAttribute(false)]
public ArrayList Connections {get;}
[C++]
[BrowsableAttribute(false)]
public: __property ArrayList* get_Connections();
[C++/CLI]
[BrowsableAttribute(false)]
public:
property ArrayList^ Connections {
ArrayList^ get();
}
An ArrayList collection containing Tcp objects representing the active connections.
Any time a client successfully connects to the server, a Tcp object representing the connection is added to the Server.Connections ArrayList. To access a particular TCP connection, use an index into the ArrayList such as Server.Connections[0]. Because an ArrayList returns Objects, you must first cast the ArrayList item into a Tcp object
Tcp tcp = (Tcp)Server.Connections[0];
if referencing a Tcp instance in this manner. A much more
common usage would involve iterating through the Server.Connections collection to send/receive data to/from
all Tcp instances i.e.
foreach(Tcp tcp in Server.Connections)
tcp.Send("This is being sent to all!");
Note: A Tcp object will not sense if the remote host has disconnected unless it is in the process of, or attempts, a Send or Receive. Therefore, the child Tcp object will not automatically be removed from Server.Connections if a Send or Receive has not been attempted since the disconnect.
The following example demonstrates a simple multi-client chat server.
[Visual Basic]
Private Sub StartServer()
' Begin listening for connections on port 8888.
Server1.Listen(8888)
End Sub
Private Sub Server1_Connection(ByVal sender As Object, ByVal e As ConnectionEventArgs) Handles Server1.Connection
' This event is raised on a new thread when a connection is received.
Try
' Send username command.
e.Tcp.Stream.Write("Please enter your username: ")
' Read until a CRLF is reached
Dim found As Boolean = False
Dim s As String = e.Tcp.Stream.Read(vbCrLf, 1024, found)
If (found) Then
' Trim(whitespace)
s = s.Trim()
' Associate username with TCP instance
e.Tcp.Tag = s
e.Tcp.Stream.Write("Go ahead and chat" + vbCrLf)
Else
' Disconnect
e.Tcp.Stream.Write("Bad Input" + vbCrLf)
e.Tcp.Stream.Close()
End If
Do While (e.Tcp.Connected)
' Receive data.
found = False
Dim text As String = e.Tcp.Stream.Read(vbCrLf, 1024, found)
' Echo data back to all clients
Dim tcp As Tcp
For Each tcp In Server1.Connections
' Preface text with user name and send
tcp.Stream.Write(e.Tcp.Tag.ToString() + ": " + text + vbCrLf)
Next
Loop
Catch ex As Exception
' eat exception
End Try
End Sub
[C#]
private void StartServer()
{
// Begin listening for connections on port 8888.
server1.Listen(8888);
}
private void server1_Connection(object
sender, ConnectionEventArgs e)
{
// This event is raised on a new thread when a connection is received.
try
{
// Send username command.
e.Tcp.Stream.Write("Please enter your username: ");
// Read until a CRLF is reached
bool found = false;
string s = e.Tcp.Stream.Read("\r\n", 1024, ref found);
if(found)
{
// Trim whitespace
s = s.Trim();
// Associate username with TCP instance
e.Tcp.Tag = s;
e.Tcp.Stream.Write("Go ahead and chat\r\n");
}
else
{
// Disconnect
e.Tcp.Stream.Write("Bad Input\r\n");
e.Tcp.Stream.Close();
}
while(e.Tcp.Connected)
{
// Receive data.
found = false;
string text = e.Tcp.Stream.Read("\r\n", 1024, ref found);
// Echo data back to all clients
foreach(Tcp tcp in
server1.Connections)
{
// Preface text with user name (from Tcp.Tag) and send
tcp.Stream.Write(e.Tcp.Tag.ToString() + ": " + text + "\r\n");
}
}
}
catch(Exception ex)
{
// eat exception
}
}
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.