Dart.PowerTCP.SslSockets Namespace > Server Class : Connection Event |
Raised when a connection is accepted.
[Visual Basic]
<DescriptionAttribute("Raised when a connection is accepted.")>
Public Event Connection() As ConnectionEventHandler
[C#]
[DescriptionAttribute("Raised when a connection is accepted.")]
public event ConnectionEventHandler Connection();
[C++]
[DescriptionAttribute("Raised when a connection is accepted.")]
public: __event ConnectionEventHandler* Connection();
[C++/CLI]
[DescriptionAttribute("Raised when a connection is accepted.")]
public:
event ConnectionEventHandler^ Connection();
The event handler receives an argument of type ConnectionEventArgs containing data related to this event. The following ConnectionEventArgs properties provide information specific to this event.
Property | Description |
---|---|
Tcp | Gets the Tcp object representing the new connection |
This event is raised when a TCP connection from a client is accepted. A ConnectionEventArgs object is passed into the event which contains a Tcp object representing the connection with the client. In addition, a Tcp object is added to the Server.Connections collection.
Typical usage would involve accessing the Tcp object contained within the ConnectionEventArgs within the Server.Connection event to send/receive data to/from the connected client.
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 the code required to build a simple echo server.
[Visual Basic]
Private Sub StartServer()
' Begin listening for connections on port 7.
Server1.Listen(7)
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
' Keep receiving data until connection is closed
Do While (e.Tcp.Connected)
' Receive data.
Dim seg As Segment = e.Tcp.Receive()
' Echo data back to client
e.Tcp.Send(seg.ToString())
Loop
Catch ex As Exception
'eat exception
End Try
End Sub
[C#]
private void StartServer()
{
// Begin listening for connections on port 7.
server1.Listen(7);
}
private void server1_Connection(object
sender, ConnectionEventArgs e)
{
// This event is raised on a new thread when a connection is received.
try
{
// Keep receiving data until connection is closed
while(e.Tcp.Connected)
{
// Receive data.
Segment seg = e.Tcp.Receive();
// Echo data back to client.
e.Tcp.Send(seg.ToString());
}
}
catch(Exception ex)
{
// eat any exceptions
}
}
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.