B4J Question IP-address on new connection

knutf

Active Member
Licensed User
Longtime User
Hello

How can I know the ip address of an incoming connection to ServerSocket?

KF
 

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
Hello

How can I know the ip address of an incoming connection to ServerSocket?

KF
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
  Log("Request of : " & req.RequestURI & " from: " & req.RemoteAddress)
End Sub
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
Thank you Gabino, but I think your answer is related to the library jServer.

I'm using the ServerSocket class in the library jNetwork...

KF
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
My application stops at the line: Dim r as Reflector
It's a Non-UI application, I think that is the reason.

I tried using JavaObject instead:

B4X:
    Dim JO As JavaObject = mySocket   
    JO = JO.GetField("socket")
    Return JO.RunMethod("getInetAddress",Null)

Now the application stops at the line: JO = JO.GetField("socket")

The errorlistening:

B4X:
java.lang.RuntimeException: Field: Socket not found in: anywheresoftware.b4a.objects.SocketWrapper
    at anywheresoftware.b4j.object.JavaObject$FieldCache.getField(JavaObject.java:261)
    at anywheresoftware.b4j.object.JavaObject.GetField(JavaObject.java:135)
    at b4j.example.connection._getip(connection.java:81)
    at b4j.example.main._server_newconnection(main.java:181)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:563)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:224)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA$3.run(BA.java:178)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:103)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:292)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.main(main.java:28)
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
Ok,

This minimal Non-UI application:
B4X:
Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private Server As ServerSocket
End Sub

Sub AppStart (Args() As String)
    Server.Initialize(4000,"Server")
    Server.listen
    StartMessageLoop
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("connected")
    Dim r As Reflector
    r.Target = NewSocket
    r.Target = r.GetField("socket")
    Log(r.RunMethod("getInetAddress"))
End Sub

causes the following error
B4X:
Program started.
connected
Error occurred on line: 19 (main).
java.lang.RuntimeException: java.lang.NoClassDefFoundError: javafx/beans/value/ChangeListener
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA$3.run(BA.java:178)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:103)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:292)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.main(main.java:28)
Caused by: java.lang.NoClassDefFoundError: javafx/beans/value/ChangeListener
    at b4j.example.main._server_newconnection(main.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:563)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:221)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    ... 16 more
Caused by: java.lang.ClassNotFoundException: javafx.beans.value.ChangeListener
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 29 more
java.lang.RuntimeException: java.util.NoSuchElementException
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.main(main.java:28)
Caused by: java.util.NoSuchElementException
    at java.util.LinkedList.removeFirst(LinkedList.java:268)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:303)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    ... 3 more
Program terminated (StartMessageLoop was not called).

The following libraries are selected: jCore, jNetwork, jReflection
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
Wow, thank you very much. I get a good feeling of being important in this society. The new property RemoteAddress worked as expected.
 
Upvote 0
Top