You are here: Client-Server > Networked > Network Server

Network Server

In order to make the embedded server operate over a TCP/IP network, we just need to specify a port number greater than zero and set up one or more accounts for our client(s).

ClientServerExample.cs: AccessRemoteServer
private static void AccessRemoteServer()
     {
            IObjectServer server = Db4oClientServer.
OpenServer(Db4oClientServer.NewServerConfiguration(), Db4oFileName, ServerPort);
      server.GrantAccess(ServerUser, ServerPassword);
      try
       {
                IObjectContainer client = Db4oClientServer.
OpenClient(Db4oClientServer.NewClientConfiguration(), "localhost", 
ServerPort, ServerUser, ServerPassword);
        // Do something with this client, or open more clients
        client.Close();
      }
      finally
       {
        server.Close();
      }
    }

ClientServerExample.vb: AccessRemoteServer
Public Shared Sub AccessRemoteServer()
            Dim server As IObjectServer = Db4oClientServer. _ 
OpenServer(Db4oClientServer.NewServerConfiguration(), _ 
Db4oFileName, ServerPort)
            server.GrantAccess(ServerUser, ServerPassword)
            Try
                Dim client As IObjectContainer = Db4oClientServer. _ 
OpenClient(Db4oClientServer.NewClientConfiguration(), _ 
"localhost", ServerPort, ServerUser, ServerPassword)
                ' Do something with this client, or open more clients
                client.Close()
            Finally
                server.Close()
            End Try
        End Sub

The client connects providing host, port, user name and password.

ClientServerExample.cs: QueryRemoteServer
private static void QueryRemoteServer(int port, string user, string password)
     {
            IObjectContainer client = Db4oClientServer.
OpenClient(Db4oClientServer.NewClientConfiguration(), 
"localhost", port, user, password);
            ListResult(client.Query<Car>());
      client.Close();
    }

ClientServerExample.vb: QueryRemoteServer
Public Shared Sub QueryRemoteServer(ByVal port As Integer, ByVal user _ 
As String, ByVal password As String)
            Dim client As IObjectContainer = Db4oClientServer. _ 
OpenClient(Db4oClientServer.NewClientConfiguration(), _ 
"localhost", port, user, password)
            ListResult(client.Query(Of Car)())
            client.Close()
        End Sub

Everything else is absolutely identical to the local server examples.

ClientServerExample.cs: DemonstrateRemoteReadCommitted
private static void DemonstrateRemoteReadCommitted(int port, 
string user, string password)
     {
            IObjectContainer client1 = Db4oClientServer.
OpenClient(Db4oClientServer.NewClientConfiguration(), 
"localhost", port, user, password);
            IObjectContainer client2 = Db4oClientServer.
OpenClient(Db4oClientServer.NewClientConfiguration(), 
"localhost", port, user, password);
      Pilot pilot = new Pilot("Jenson Button", 97);
            IList<Car> result = client1.Query<Car>();
            Car car = result[0];
      car.Pilot = pilot;
      client1.Store(car);
      ListResult(client1.Query<Car>());
            ListResult(client2.Query<Car>());
      client1.Commit();
            ListResult(client1.Query<Car>());
            ListResult(client2.Query<Car>());
      client1.Close();
      client2.Close();
    }

ClientServerExample.vb: DemonstrateRemoteReadCommitted
Public Shared Sub DemonstrateRemoteReadCommitted(ByVal port As Integer, _ 
ByVal user As String, ByVal password As String)
            Dim client1 As IObjectContainer = Db4oClientServer. _ 
OpenClient(Db4oClientServer.NewClientConfiguration(), _ 
"localhost", port, user, password)
            Dim client2 As IObjectContainer = Db4oClientServer. _ 
OpenClient(Db4oClientServer.NewClientConfiguration(), _ 
"localhost", port, user, password)
            Dim pilot As Pilot = New Pilot("Jenson Button", 97)
            Dim result As IList(Of Car) = client1.Query(Of Car)()
            Dim car As Car = result(0)
            car.Pilot = pilot
            client1.Store(car)
            ListResult(client1.Query(Of Car)())
            ListResult(client2.Query(Of Car)())
            client1.Commit()
            ListResult(client1.Query(Of Car)())
            ListResult(client2.Query(Of Car)())
            client1.Close()
            client2.Close()
        End Sub

ClientServerExample.cs: DemonstrateRemoteRollback
private static void DemonstrateRemoteRollback(int port, 
string user, string password)
     {
            IObjectContainer client1 = Db4oClientServer.
OpenClient(Db4oClientServer.NewClientConfiguration(), 
"localhost", port, user, password);
            IObjectContainer client2 = Db4oClientServer.
OpenClient(Db4oClientServer.NewClientConfiguration(), 
"localhost", port, user, password);
      IList<Car> result = client1.Query<Car>();
      Car car = result[0];
      car.Pilot = new Pilot("Someone else", 0);
      client1.Store(car);
      ListResult(client1.Query<Car>());
            ListResult(client2.Query<Car>());
      client1.Rollback();
      client1.Ext().Refresh(car,2);
            ListResult(client1.Query<Car>());
            ListResult(client2.Query<Car>());
      client1.Close();
      client2.Close();
    }

ClientServerExample.vb: DemonstrateRemoteRollback
Public Shared Sub DemonstrateRemoteRollback(ByVal port As Integer, ByVal user As String, _ 
ByVal password As String)
            Dim client1 As IObjectContainer = Db4oClientServer. _ 
OpenClient(Db4oClientServer.NewClientConfiguration(), _ 
"localhost", port, user, password)
            Dim client2 As IObjectContainer = Db4oClientServer. _ 
OpenClient(Db4oClientServer.NewClientConfiguration(), _ 
"localhost", port, user, password)
            Dim result As IList(Of Car) = client1.Query(Of Car)()
            Dim car As Car = result(0)
            car.Pilot = New Pilot("Someone else", 0)
            client1.Store(car)
            ListResult(client1.Query(Of Car)())
            ListResult(client2.Query(Of Car)())
            client1.Rollback()
            client1.Ext().Refresh(car, 2)
            ListResult(client1.Query(Of Car)())
            ListResult(client2.Query(Of Car)())
            client1.Close()
            client2.Close()
        End Sub

Download example code:

VB.NET c#