B4J Question java.net.SocketException: Permission denied: connect

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

I use httpjob to get Remote I/O data every 250ms day by day. Why i get following message once in a while ?
B4X:
java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:133)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at anywheresoftware.b4a.http.HttpClientWrapper.executeWithTimeout(HttpClientWrapper.java:310)
    at anywheresoftware.b4a.http.HttpClientWrapper.access$0(HttpClientWrapper.java:308)
    at anywheresoftware.b4a.http.HttpClientWrapper$ExecuteHelper.run(HttpClientWrapper.java:207)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I use a timer to calling following code

B4X:
Public Sub GetDigitalInput(wiseUrl As String,wiseAuths As String,tag As Object) As Boolean
    Dim job1 As HttpJob
    Dim au As String="Basic " & su.EncodeBase64(wiseAuths.GetBytes("utf8"))          
    job1.Initialize("GetDi",Me,tag)
    Try
        job1.download("http://" & wiseUrl & "/di_value/slot_0")
        job1.GetRequest.Timeout = ConnectionTimeOut
        job1.GetRequest.SetHeader("Authorization",au)
        Return True
    Catch
        db.dtLog("GetDigitalInput Exp:")
        Return False
    End Try
End Sub
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
The Try / Catch block is not needed. I don't see how it can throw an error here.

Does it work fine after this error?

It must go through a period of time to connect successfully Or Power reset the computer again :(
 
Last edited:
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
Maybe 250ms is too fast for the server and it thinks it has a DoS attack. Maybe try slowing down the request rate?
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi
this app run in a PC. It has two netcard(A,B).
A is our remote I/O network 192.168.1.~ for data collection.
B is customer office network 192.168.56.~ for view web site.

When i use A and B, My app could't connect all remote I/O.
but I use A only, My app could connect all remote I/O. Why ... ??

I use following code to connect remote i/o module
B4X:
Public Sub GetDigitalInput(wiseUrl As String,wiseAuths As String,tag As Object) As Boolean
    Dim job1 As HttpJob
    Dim au As String="Basic " & su.EncodeBase64(wiseAuths.GetBytes("utf8"))        
    job1.Initialize("GetDi",Me,tag)
    Try
        job1.download("http://" & wiseUrl & "/di_value/slot_0")
        job1.GetRequest.Timeout = ConnectionTimeOut
        job1.GetRequest.SetHeader("Authorization",au)
        Return True
    Catch
        db.dtLog("GetDigitalInput Exp:")
        Return False
    End Try
End Sub
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Hi
this app run in a PC. It has two netcard(A,B).
A is our remote I/O network 192.168.1.~ for data collection.
B is customer office network 192.168.56.~ for view web site.

When i use A and B, My app could't connect all remote I/O.
but I use A only, My app could connect all remote I/O. Why ... ??

It could have something to do with routing (network level). Technically, you should be able to access your sites via a standard web browser (not using your app). You may get authentication errors, but at least you are connecting. So with only A active, you should be able to get to all your IO web sites via the browser. Then activate B. If you still can get to all your IO web sites, then you have an app issue. If suddenly you cannot get to your IO web sites, then you have a routing issue (which is outside of your apps scope).
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
I have found the question:
when i use two netcard in same computer, I could ping the remote i/o but i couldn't connect it with httpjob(successful always return false). it is fail too when i connect with browser.

P.S the remote i/o has builtin WebServer
data collect computer,remote i/o working in 192.1.168~
Office compuer working in 192.168.3.~
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
java.net.SocketException: Permission denied: connect
when i use two netcard in same computer, I could ping the remote i/o but i couldn't connect it with httpjob(successful always return false). it is fail too when i connect with browser.

Ok, I'm guessing here, but I think you are having the following issue:

1) The data collect computer's web server has a security setting that only allows access to it from certain subnet(s). In this case, some rule allows access from the 192.168.1.* network, but not others (at least not the subnet your office computers are working in).

2) When you install the second NIC, Windows binds your application to the NIC with the office subnet, not the I/O subnet.

Because of this, your application and web browsers are blocked from accessing your I/O web server, since the I/O web server thinks you are accessing it from an office network IP address, not an I/O network IP address.

Solutions:

1) Change whatever firewall, access rule that is in place on the I/O web server that does not allow access from the office network. If security is a concern, you could (if not already) give your computer a static office IP and only add this static IP to the firewall/access rule.

2) Try something programmatically that makes you app bind to the right interface (out of my scope and may not be possible in Java).

3) Change the binding order in Windows. Looks like this may be feasible in Windows 8.1 and earlier, but not anymore in Windows 10 (as described in Adjusting the Network Protocol Bindings in Windows 10).

The least invasive solution would be #3 (for the I/O web server, especially if we are talking about multiple I/O servers), but with Windows 10 you may need to resort to #1. #2 may not even be possible.
 
Upvote 0
Top