How do the ServerSocket look up the IP-address of the connected client

DSDominno

Member
Licensed User
Hello,

(Sorry for my bad English, I am a French developer (Hard and Soft) on PIC, Asm, vb6 and others materials and languages...)

I have build a service TCP server application (ServerSocket and AsyncStreams) that run very nice and speedy with multiple clients in the same time.

In HTTP mode It can decode all the Methods (GET,POST, HEAD, PUT, DELETE), it build automatic entete response with "Content-Type","Content-Length",... . I have successfully passed tests with W3C on HTML 4.01 transitional, css and java.

It can act as Http-Server , JBUS/MODBUS-TCP Server, JBUS/MODBUS Server and other binary protocols (used for control/commands in automation Building) , on the same socket (the protocol of the response is automatically switched and passed as argument when the response is asked to the main service).

But i want to block connection for IP Client non authorized.

Question :

- How do the server to retrieve the IP-address of the connected clients when the event sockServeurWeb_NewConnection fire ?


Notes :
- I am working with Basic4Android version 2.52, library NetWork version 1.23 and different target devices with Android 4.0 and sup (Acer, Arnova and Chinese tablets).

- I have try the library B4a "HttpServer" version 1.00 that contain the interesting property "RemoteAddress" in the object "ServletRequest".

But i can not use this library for three reasons :

1 - It can act only in HTTP mode and not in binary (for JBUS/MODBUS and other binary protocols for automation building).
2 - The response is too slow !! (with Opera, Firefox, IE and Chrome).
3 - I can not destroy immediately the socket of the response, in case of the blocked IP.

Elements of responses :

1 - Must i create a new class which include ServerSocket with additional properties and events ? and if yes how to ?
2 - Must i create or adding functions on a new lib based on library NetWork 1.23 ? and if yes : can I modify the source and how to ?


Thank you for yours responses.
 

DSDominno

Member
Licensed User
thank you and other question

The code run very well. My problem is solved. It is a good and speedy solution.

It may be other methods for the "socket" that run with reflector.

Where can i find documentation on this methods ?

Thank you for your response.
 

tdocs2

Well-Known Member
Licensed User
You can use this code:
B4X:
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
   Dim r As Reflector
   r.Target = NewSocket
   r.Target = r.GetField("socket")
   r.Target = r.RunMethod("getInetAddress") 'InetAddress
   Dim ip As String = r.RunMethod("getHostAddress")
End Sub
Thank you, Erel.

I incorporated this code in FileTransfer here and it works as expected.

It seems a bit paradoxical that r.RunMethod("getHostAddress") actually gets sender or client IP, but it does.

Best regards.

Sandy
 

davepamn

Active Member
Licensed User
The ServerSocket1_NewConnection event is required in B4i, otherwise an error is thrown.
 

janderkan

Active Member
Licensed User
I have used the information in this post to retreive the port the socket are connected on.

Dim r As Reflector
r.Target = NewSocket
r.Target = r.GetField("socket")
Dim Port As Int = r.RunMethod("getPort")
Log("Connected on port " & Port)

In the same way I can get the sockets KeepAlive state.

Dim r As Reflector
r.Target = NewSocket
r.Target = r.GetField("socket")
Dim KA As Boolean = r.RunMethod("getKeepAlive")
Log("KeepAlive: " & KA)

But when I try to change the KeepAlive like this

Dim r As Reflector
r.Target = NewSocket
r.Target = r.GetField("socket")
r.RunMethod2("setKeepAlive", "-1", "java.lang.Boolean")

I get error NoSuchMethod:

r.RunMethod2("setKeepAlive", "-1", "java.lang.Bo
java.lang.NoSuchMethodException: setKeepAlive [class java.lang.Boolean]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getDeclaredMethod(Class.java:685)
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod2(Reflection.java:817)
at dk.jdk.linkmaster.server._server_newconnection(server.java:662)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA$3.run(BA.java:334)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
at dalvik.system.NativeStart.main(Native Method)
 

janderkan

Active Member
Licensed User
Thank you, Erel.

I incorporated this code in FileTransfer here and it works as expected.

It seems a bit paradoxical that r.RunMethod("getHostAddress") actually gets sender or client IP, but it does.

Best regards.

Sandy
When you are at the serverside then it is the remote socket that initiated the connection.
So the remote are the host.
 
Top