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
01public static void AccessRemoteServer() 02 { 03 IObjectServer server = Db4oFactory.OpenServer(YapFileName, ServerPort); 04 server.GrantAccess(ServerUser, ServerPassword); 05 try 06 { 07 IObjectContainer client = Db4oFactory.OpenClient("localhost", ServerPort, ServerUser, ServerPassword); 08 // Do something with this client, or open more clients 09 client.Close(); 10 } 11 finally 12 { 13 server.Close(); 14 } 15 }

ClientServerExample.vb: AccessRemoteServer
01Public Shared Sub AccessRemoteServer() 02 Dim server As IObjectServer = Db4oFactory.OpenServer(YapFileName, ServerPort) 03 server.GrantAccess(ServerUser, ServerPassword) 04 Try 05 Dim client As IObjectContainer = Db4oFactory.OpenClient("localhost", ServerPort, ServerUser, ServerPassword) 06 ' Do something with this client, or open more clients 07 client.Close() 08 Finally 09 server.Close() 10 End Try 11 End Sub

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

ClientServerExample.cs: QueryRemoteServer
1public static void QueryRemoteServer(int port, string user, string password) 2 { 3 IObjectContainer client = Db4oFactory.OpenClient("localhost", port, user, password); 4 ListResult(client.Get(new Car(null))); 5 client.Close(); 6 }

ClientServerExample.vb: QueryRemoteServer
1Public Shared Sub QueryRemoteServer(ByVal port As Integer, ByVal user As String, ByVal password As String) 2 Dim client As IObjectContainer = Db4oFactory.OpenClient("localhost", port, user, password) 3 ListResult(client.[Get](New Car(Nothing))) 4 client.Close() 5 End Sub

Everything else is absolutely identical to the local server examplescreate it.

ClientServerExample.cs: DemonstrateRemoteReadCommitted
01public static void DemonstrateRemoteReadCommitted(int port, string user, string password) 02 { 03 IObjectContainer client1 = Db4oFactory.OpenClient("localhost", port, user, password); 04 IObjectContainer client2 = Db4oFactory.OpenClient("localhost", port, user, password); 05 Pilot pilot = new Pilot("Jenson Button", 97); 06 IObjectSet result = client1.Get(new Car(null)); 07 Car car = (Car)result.Next(); 08 car.Pilot = pilot; 09 client1.Set(car); 10 ListResult(client1.Get(new Car(null))); 11 ListResult(client2.Get(new Car(null))); 12 client1.Commit(); 13 ListResult(client1.Get(new Car(null))); 14 ListResult(client2.Get(new Car(null))); 15 client1.Close(); 16 client2.Close(); 17 }

ClientServerExample.vb: DemonstrateRemoteReadCommitted
01Public Shared Sub DemonstrateRemoteReadCommitted(ByVal port As Integer, ByVal user As String, ByVal password As String) 02 Dim client1 As IObjectContainer = Db4oFactory.OpenClient("localhost", port, user, password) 03 Dim client2 As IObjectContainer = Db4oFactory.OpenClient("localhost", port, user, password) 04 Dim pilot As Pilot = New Pilot("Jenson Button", 97) 05 Dim result As IObjectSet = client1.[Get](New Car(Nothing)) 06 Dim car As Car = DirectCast(result.[Next](), Car) 07 car.Pilot = pilot 08 client1.[Set](car) 09 ListResult(client1.[Get](New Car(Nothing))) 10 ListResult(client2.[Get](New Car(Nothing))) 11 client1.Commit() 12 ListResult(client1.[Get](New Car(Nothing))) 13 ListResult(client2.[Get](New Car(Nothing))) 14 client1.Close() 15 client2.Close() 16 End Sub

ClientServerExample.cs: DemonstrateRemoteRollback
01public static void DemonstrateRemoteRollback(int port, string user, string password) 02 { 03 IObjectContainer client1 = Db4oFactory.OpenClient("localhost", port, user, password); 04 IObjectContainer client2 = Db4oFactory.OpenClient("localhost", port, user, password); 05 IObjectSet result = client1.Get(new Car(null)); 06 Car car = (Car)result.Next(); 07 car.Pilot = new Pilot("Someone else", 0); 08 client1.Set(car); 09 ListResult(client1.Get(new Car(null))); 10 ListResult(client2.Get(new Car(null))); 11 client1.Rollback(); 12 client1.Ext().Refresh(car,2); 13 ListResult(client1.Get(new Car(null))); 14 ListResult(client2.Get(new Car(null))); 15 client1.Close(); 16 client2.Close(); 17 }

ClientServerExample.vb: DemonstrateRemoteRollback
01Public Shared Sub DemonstrateRemoteRollback(ByVal port As Integer, ByVal user As String, ByVal password As String) 02 Dim client1 As IObjectContainer = Db4oFactory.OpenClient("localhost", port, user, password) 03 Dim client2 As IObjectContainer = Db4oFactory.OpenClient("localhost", port, user, password) 04 Dim result As IObjectSet = client1.[Get](New Car(Nothing)) 05 Dim car As Car = DirectCast(result.[Next](), Car) 06 car.Pilot = New Pilot("Someone else", 0) 07 client1.[Set](car) 08 ListResult(client1.[Get](New Car(Nothing))) 09 ListResult(client2.[Get](New Car(Nothing))) 10 client1.Rollback() 11 client1.Ext().Refresh(car, 2) 12 ListResult(client1.[Get](New Car(Nothing))) 13 ListResult(client2.[Get](New Car(Nothing))) 14 client1.Close() 15 client2.Close() 16 End Sub