B4J Question multiple server ports

OMS

Member
hi,
I have a b4j server app.
And now I need my server to respond to more than one port.
Is it possible for server object to cover multiple ports OR do I have to declare two server objects?
And if I start two server objects, what will happen to server performance?
Is it an accepable practice?
 

OMS

Member
Well, some of my web service users reside in a vLan that only one specific port is open for them.

They asked me for the ports different from others.

Thank you
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Well, some of my web service users reside in a vLan that only one specific port is open for them.

They asked me for the ports different from others.

Thank you

I think that this is preferably to do with a manageable switch or a simple router and forwarding port... If we are talking for a vlan for local network...

-For the history you can run many b4j web servers but is a solution / for those need run different kind of b4j servers... not just for a port...
 
Upvote 0

OMS

Member
You can duplicate another B4J server and use different port. I think you need to find out the performance after testing.
thanks for your reply
My web service has many handlers and coding.
If I create anther project only for a new port then I will have to copy every line of codes in multiple projects and running multiple 10 mega byte JAR files on the server.

Now I have declared multiple Server objects in my project And it seems to work fine But I am looking for the best practice.
Something like this:
 
Upvote 0

OMS

Member
I think that this is preferably to do with a manageable switch or a simple router and forwarding port... If we are talking for a vlan for local network...

-For the history you can run many b4j web servers but is a solution / for those need run different kind of b4j servers... not just for a port...
Thanks for your reply.
I will consult my network admin to see if it can be done without the users sense they are redirected.

Though in IIS it is easy to define as many ports as I like for my web site, Jetty configuration is not so easy.
I guess my network admin will wonder at my request.?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of adding another connector to the server:
B4X:
srvr.Start
Dim jserver As JavaObject = srvr.As(JavaObject).GetField("server")
Dim connectors() As Object = jserver.RunMethod("getConnectors", Null)
Dim jo As JavaObject
Dim NewPort As JavaObject
NewPort.InitializeNewInstance("org.eclipse.jetty.server.ServerConnector", Array(jserver))
NewPort.RunMethod("setPort", Array(52042))
Dim NewConnectors() As Object = jo.InitializeArray("org.eclipse.jetty.server.Connector", Array(connectors(0), connectors(1), NewPort))
jserver.RunMethod("setConnectors", Array(NewConnectors))
jserver.RunMethod("stop", Null)
jserver.RunMethod("start", Null)
StartMessageLoop
If you haven't added a SSL port then change this: connectors(0), connectors(1), NewPort to connectors(0), NewPort.
 
Upvote 0

OMS

Member
I tested it and it was OK.
But adding another SSL port seems to be more complicated:

Please kindly help
 
Upvote 0

OMS

Member
is it possible to use the existing SSL connector to create a clone, and just change its port?
like this:
B4X:
Dim myServerObj As JavaObject = srvr
    Dim jserver As JavaObject = myServerObj.GetField("server")
    Dim connectors() As Object = jserver.RunMethod("getConnectors", Null)
   
    Log("Length is " & connectors.Length)
    Log( connectors(0))
    Log( connectors(1))
   
    Dim jo As JavaObject
    Dim NewPort As JavaObject = CopyObject(connectors(0))   'assuming that connectors(0) is a SSL connector
    'NewPort.InitializeNewInstance("org.eclipse.jetty.server.ServerConnector", Array(jserver))
    NewPort.RunMethod("setPort", Array(18087))
   
    Dim NewConnectors() As Object = jo.InitializeArray("org.eclipse.jetty.server.Connector", Array(connectors(1),connectors(0), NewPort))
    jserver.RunMethod("setConnectors", Array(NewConnectors))
    connectors = jserver.RunMethod("getConnectors", Null)
    Log("Length is " & connectors.Length)
    Log( connectors(0))
    Log( connectors(1))
    Log( connectors(2))
    jserver.RunMethod("stop", Null)
    jserver.RunMethod("start", Null)


Sub CopyObject(Obj As Object) As Object
    Dim raf As RandomAccessFile
    raf.Initialize(File.DirApp, "object.dat", False)
    raf.WriteObject(Obj, True, 0)
    Dim newObj As Object
    newObj = raf.ReadObject(0)
    raf.Close
    Return newObj
End Sub

Sub CopyObject2(Obj As Object) As Object
    Dim raf As RandomAccessFile
    Dim n=256 As Int
    Dim notdone=True As Boolean
    Do While notdone
        'Msgbox(n,"n")
        notdone=False
        Dim buffer(n) As Byte
        Try
            raf.Initialize3(buffer,False)
            raf.WriteObject(Obj, True, 0)
            Dim newObj As Object
            newObj = raf.ReadObject(0)
        Catch
            n=n*10
            notdone=True
        End Try
        raf.Close
    Loop
    Return newObj
End Sub

I tried this code but it does not work. ( the resulting object is not initialized )
 
Upvote 0

OMS

Member
Creating multiple SSL connectors is more complicated. Use a reverse proxy instead.
Thank you Erel.
Port proxy worked successfully.:D
just by runing this command on my server:
B4X:
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=18087 connectaddress=127.0.0.1 connectport=443
 
Upvote 0
Top