See Also

Ping Class  | Ping Members

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Language

Visual Basic

C#

C++

C++/CLI

Show All

See Also Languages PowerTCP SSL Sockets for .NET

Tag Property

Dart.PowerTCP.SslSockets Namespace > Ping Class : Tag Property (Ping)

Gets or sets an object reference that can be used to associate this instance with any other.

[Visual Basic]
<DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")> <BrowsableAttribute(False)> <DefaultValueAttribute()> Public Property Tag As Object
[C#]
[DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")] [BrowsableAttribute(false)] [DefaultValueAttribute()] public object Tag {get; set;}
[C++]
[DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")] [BrowsableAttribute(false)] [DefaultValueAttribute()] public: __property Object* get_Tag(); public: __property void set_Tag(    Object* value );
[C++/CLI]
[DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")] [BrowsableAttribute(false)] [DefaultValueAttribute()] public: property Object^ Tag {    Object^ get();    void set (Object^ value); }

Return Type

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

Remarks

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.

Example

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
  }
}
                

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Ping Class  | Ping Members


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.