PowerTCP Sockets for .NET
Start(ConnectThreadStart,Int32,Object) Method
Example 




A ConnectThreadStart delegate referencing the method to invoke on an IO completion thread as each connection is accepted.
Local port to bind to.
Object to pass into the callback method when invoked; can be null.
Begin listening on the specified port on all IPv4 network interfaces.
Syntax
'Declaration
 
Public Overloads Sub Start( _
   ByVal callback As ConnectThreadStart, _
   ByVal localPort As Integer, _
   ByVal state As Object _
) 
'Usage
 
Dim instance As Server
Dim callback As ConnectThreadStart
Dim localPort As Integer
Dim state As Object
 
instance.Start(callback, localPort, state)
public void Start( 
   ConnectThreadStart callback,
   int localPort,
   object state
)
public: void Start( 
   ConnectThreadStart* callback,
   int localPort,
   Object* state
) 
public:
void Start( 
   ConnectThreadStart^ callback,
   int localPort,
   Object^ state
) 

Parameters

callback
A ConnectThreadStart delegate referencing the method to invoke on an IO completion thread as each connection is accepted.
localPort
Local port to bind to.
state
Object to pass into the callback method when invoked; can be null.
Remarks
This method does not block. Connections are accepted on IO completion threads and are reported to the specified delegate.
Example
The following example demonstrates an echo server using asynchronous read and write methods that utilize IO completion ports.
private void button1_Click(object sender, EventArgs e)
{
    //Start the echo server on port 7
    server1.Start(server1_NewConnection, 7, null);
}

private void server1_NewConnection(Tcp client, object state)
{
    //Read first data sent by client
    byte[] buffer = new byte[1024];
    client.ReadAsync(buffer, 0, buffer.Length, client_ReadAsyncCompleted, null);
}

private void client_ReadAsyncCompleted(TcpBase client, Data data, Exception ex, object state)
{
    //Echo data received back to client.
    //Data is null if client is not connected.
    if (data != null)
        client.WriteAsync(data.Buffer, data.Offset, data.Count, client_WriteAsyncCompleted, null);
}

private void client_WriteAsyncCompleted(TcpBase client, Data data, Exception ex, object state)
{
    //Read for more data.
    client.ReadAsync(data.Buffer, 0, data.Buffer.Length, client_ReadAsyncCompleted, null);
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Start the echo server on port 7
    server1.Start(AddressOf server1_NewConnection, 7, Nothing)
End Sub

Private Sub server1_NewConnection(ByVal client As Tcp, ByVal state As Object)
    'Read first data sent by client
    Dim buffer(1023) As Byte
    client.ReadAsync(buffer, 0, buffer.Length, AddressOf client_ReadAsyncCompleted, Nothing)
End Sub

Private Sub client_ReadAsyncCompleted(ByVal client As TcpBase, ByVal data As Data, ByVal ex As Exception, ByVal state As Object)
    'Echo data received back to client.
    'Data is null if client is not connected.
    If data IsNot Nothing Then
        client.WriteAsync(data.Buffer, data.Offset, data.Count, AddressOf client_WriteAsyncCompleted, Nothing)
    End If
End Sub

Private Sub client_WriteAsyncCompleted(ByVal client As TcpBase, ByVal data As Data, ByVal ex As Exception, ByVal state As Object)
    'Read for more data.
    client.ReadAsync(data.Buffer, 0, data.Buffer.Length, AddressOf client_ReadAsyncCompleted, Nothing)
End Sub
See Also

Reference

Server Class
Server Members
Overload List


PowerTCP Sockets for .NET Documentation Version 4.5
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic