Android Question How to bind a Socket ?

freedom2000

Well-Known Member
Licensed User
Longtime User
@Erel ,

Following discussion on my "PowerPoint Mouse", and even if I admit that I should "reverse" the client/server sides to have a wider chance of success with firewall issues, I still have a problem with my very secured business PC...

Let's asssume that my PC ONLY accepts one fixed port both for incoming and outgoing TCP.
What I would need is to force the local port (on my device) to this fixed value so that it is seen from my PC as "allowed".
A network guy told me that I could achieve this using socket "bind". I have searched on the net and found something which could be usefull.

B4X:
public void bind (SocketAddress localAddr)

Added in API level 1
Binds this socket to the given local host address and port specified by the SocketAddress localAddr. If localAddr is set to null, this socket will be bound to an available local address on any free port.

Parameters
localAddr    the specific address and port on the local machine to bind to.
Throws
IllegalArgumentException    if the given SocketAddress is invalid or not supported.
IOException    if the socket is already bound or an error occurs while binding.

My questions are :
1) how could I access this method in B4A ?
2) if I "bind" to a local port could I also "connect" to a remote host address and port ?


For further explaination here is the log of my firewal (extract)

Message : blocked incoming TCP - Source 192.168.43.1 : (37968) Destination 192.168.43.251 : (5500)

the source address is my device IP given when in wifi hotspot mode
37968 is the local port number given "at random ?" when connecting to the socket
destination IP is the PC server address connected to my device (hotspot) and 5500 is the allowed port

I just want to force the "37968" port number to 5500 to give a chance to the packet to enter the PC !

Thanks
 

freedom2000

Well-Known Member
Licensed User
Longtime User
This is a community forum. Please don't limit the question to a single member...

Sorry for that, I wasn't aware that the question was limited to you.

Regarding the bind method, my security guy said that it could solve the problem...
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
If a firewall blocks an incoming connection on port 5500 then you cannot connect to this port from a different machine.

Yes for sure the ports must be opened

But the firewall is indeed configured to allow incoming and outgoing tcp trafic on this particular port.
AND it seems (tested under DOS between 2 machines) that it only works if the outgoing connection to the server is also binded to this particular port (don't ask me why ...)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see. Can't you set the the remote port to Any port ?

SS-2014-04-30_08.42.34.png


You can call bind with Reflector. However it is a bit tricky and it will also not solve the problem. The user will still need to configure the firewall for your app to work.

A better solution (like done in B4A designer, B4A Bridge and the debugger) is to make the PC connect to the Android. This way you don't need to open any port.
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
I see. Can't you set the the remote port to Any port ?


You can call bind with Reflector. However it is a bit tricky and it will also not solve the problem. The user will still need to configure the firewall for your app to work.

A better solution (like done in B4A designer, B4A Bridge and the debugger) is to make the PC connect to the Android. This way you don't need to open any port.

Yes you are right and I am in the process of rewriting the socket part of my code to do what you propose. This will be for the publshed version of the code.

However for my particular problem, I can't modify the PC's configuration (strict security policy).
Could you please indicate me how I could do the binding with reflection lib (I am not familiar with this lib and don't fully understand how it works)

Thank you for your help.
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
Dim r As Reflector
r.Target = YourSocket 'should be initialized
Dim jo As JavaObject = r.GetField("socket")
Dim address As JavaObject
address.InitializeNewInstance("java.net.InetSocketAddress", Array As Object(<local port>))
jo.RunMethod("bind", Array As Object(address))

@Erel ,

It works really well ;) Your reflector binding does the trick, my security guy was right.

Erel, thank you Sooooo much. My App can now work properly in our secured environment

Next step : follow your advice and put the server on the device for a wider audience.
 
Upvote 0
Top