Dart.Sockets Namespace : Server Class |
'Declaration Public Class Server Inherits SocketBase
'Usage Dim instance As Server
public class Server : SocketBase
public __gc class Server : public SocketBase
public ref class Server : public SocketBase
Use the Server component to listen for incoming connections. When a connection is established from a TCP client, a new thread is created on which the server and client communicate.
What follows are short usage descriptions. For more information, see the appropriate member topic.
Listening for connections: Use Server.Start (specifying the port to listen on) to listen for incoming TCP connections.
Determining if the server is active: The Socket property is non-null when the server is active.
Handling incoming connections: When a TCP client connects to the server, a user-specified delegate is raised. A Tcp object is provided to the delegate, representing the client/server connection. Use the Tcp object to send and receive data to/from the connected client (both synchronous and asynchronous operation is supported). To facilitate memory release, dispose of the object and call GC.Collect when the connection is terminated.
Stop accepting incoming connections: Use Close to stop accepting incoming connections. Any existing connections will complete normally.
Stop the server and close all connections: Use Abort.
Communicating securely: Call TcpBase.AuthenticateAsServer for each incoming connection.
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
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) { //While connection is open, read and echo data back to client byte[] buffer = new byte[1024]; Data data = client.Read(buffer); while (data != null) { client.Write(data.Buffer, data.Offset, data.Count); data = client.Read(buffer); } }
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) 'While connection is open, read and echo data back to client Dim buffer(1023) As Byte Dim data As Data = client.Read(buffer) Do While data IsNot Nothing client.Write(data.Buffer, data.Offset, data.Count) data = client.Read(buffer) Loop End Sub
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Dart.Sockets.ComponentBase
Dart.Sockets.SocketBase
Dart.Sockets.Server