Dart.PowerTCP.SslSockets Namespace > Server Class : Tag Property (Server) |
Gets or sets an object reference that can be used to associate this instance with any other.
[Visual Basic]
<DefaultValueAttribute()>
<DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")>
<BrowsableAttribute(False)>
Public Property Tag As Object
[C#]
[DefaultValueAttribute()]
[DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")]
[BrowsableAttribute(false)]
public object Tag {get; set;}
[C++]
[DefaultValueAttribute()]
[DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")]
[BrowsableAttribute(false)]
public: __property Object* get_Tag();
public: __property void set_Tag(
Object* value
);
[C++/CLI]
[DefaultValueAttribute()]
[DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")]
[BrowsableAttribute(false)]
public:
property Object^ Tag {
Object^ get();
void set (Object^ value);
}
Any object that is associated with the component; null if no objects are
associated with the component.
Any object that is associated with the object, null if no objects are associated with the object
Use this property to associate any type derived from Object class with the component.
A common use for the Tag property is to store data that is closely associated with the component.
Use this property to associate any object with the object.
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.