Android Question Websocket client example not connecting - connection refused error

superkan

Member
Licensed User
Used the example provided in https://www.b4x.com/android/forum/threads/websocket-client-library.40221/page-4#posts
B4J command line app runs within the B4J IDE:
B4J IDE Log Messages:
Waiting for debugger to connect...
Program started.
2023-05-23 22:16:12.605:INFO :oejs.Server:main: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.1+13
2023-05-23 22:16:12.804:INFO :oejss.DefaultSessionIdManager:main: Session workerName=node0
2023-05-23 22:16:12.853:INFO :oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@50b5ac82{/,file:///D:/ANDROI~1/WEBSOC~1/B4JSER~1/B4A-WE~1/Objects/www,AVAILABLE}
2023-05-23 22:16:12.925:INFO :oejs.RequestLogWriter:main: Opened D:\Android-B4A-works\Websockets\b4j server\B4A-WebSocket\Objects\logs\b4j-2023_05_23.request.log
2023-05-23 22:16:13.069:INFO :oejs.AbstractConnector:main: Started ServerConnector@709ba3fb{HTTP/1.1, (http/1.1)}{0.0.0.0:51042}
2023-05-23 22:16:13.096:INFO :oejs.Server:main: Started Server@6580cfdd{STARTING}[11.0.9,sto=0] @1424ms
Emulated network latency: 100ms

I don't know where the magic number 192.168.0.100 in the B4a app comes from (a comment should have helped) in the
whose address is this:
Private link As String = "ws://192.168.0.100:51042/ws"

I suppose it is the IP address of computer running the b4j app. I plugged the computer ip address (same network as b4a app) into the above string. I even added the manifest permission for internet. But, nothing helps.

Toast message appears containing connection refused

Android 8
targetSDK 29
Firewall of PC may be a problem as it usually prompts me when an exe tries to access a network. But, I have no idea about B4J CLI apps. I used the recommended port.
 
Last edited:
Solution
The problem was
1st) missing internet permission in manifest when first installed
missing permission in original example (old):
AddPermission(android.permission.INTERNET)

2nd) added the permission but forgot to uninstall the app and re-install (same release mode)

It works fine now!

DonManfred

Expert
Licensed User
Longtime User
Use the correct address in your local network if that net/subnet does not match your configuration.

Are you trying to run a b4j-code in android?
 
Upvote 0

superkan

Member
Licensed User
Use the correct address in your local network if that net/subnet does not match your configuration.

Are you trying to run a b4j-code in android?
Hi Don, whose address in the network you are referring to? I have experience of using AsyncStreams/TCP-IP on this wifi network to connect the PC and Android. There shouldn't be any problem for Websockets.

No I am not running B4J code in B4a. B4a is the Websockets Client.

The Server code in that example is just this much (unchanged by me):
B4J Code in B4J IDE:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Public srvr As Server
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("")
    srvr.Port = 51042
    srvr.AddWebSocket("/ws", "B4A")
    srvr.Start
    StartMessageLoop
End Sub



B4a Code for reference from that example (unchanged except for the ip address):
B4A Example Code for Websockets Client:
#Region  Project Attributes
    #ApplicationLabel: Websockets Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private wsh As WebSocketHandler
    Private link As String = "ws://IP ADDRESS OF PC:51042/ws"
End Sub

Sub Globals
    Private btnConnect As Button
    Private lblServerTime As Label
    Private lblStatus As Label
    Private btnSend As Button
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        wsh.Initialize(Me, "wsh")
    End If
    Activity.LoadLayout("1")
End Sub

Sub wsh_ServerTime(Params As List)
    'example of a server push message
    lblServerTime.Text = "Server Time: " & Params.Get(0)
End Sub

Sub btnSend_Click
    Dim data As Map
    data.Initialize
    data.Put("message", EditText1.Text)
    wsh.SendEventToServer("Device_Message", data)
    EditText1.RequestFocus
    EditText1.SelectAll
End Sub

Sub EditText1_EnterPressed
    If btnSend.Enabled Then btnSend_Click
End Sub

Sub wsh_Connected
    UpdateStatus
End Sub

Sub wsh_Closed (Reason As String)
    UpdateStatus
    ToastMessageShow(Reason, True)
End Sub

Sub Activity_Resume
     UpdateStatus
End Sub

Sub UpdateStatus
    If wsh.ws.Connected Then
        lblStatus.Text = "Status: Connected"
    Else
        lblStatus.Text = "Status: Disconnected"
    End If
    btnConnect.Enabled = Not(wsh.ws.Connected)
    btnSend.Enabled = wsh.ws.Connected
End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

Sub btnConnect_Click
    wsh.Connect(link)
    lblStatus.Text = "Status: Connecting..."
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi Don, whose address in the network you are referring to?
192.168.x.x

But away from that. You can only use such a IP from android if you are connected to the SAME network (in this case 192.168.0.x)
IP ADDRESS OF PC
You must use the public IP address of the PC if you are not in the same network. The port must be open in your router and routed to the correct IP in the PCs network.
 
Upvote 0

superkan

Member
Licensed User
192.168.x.x

But away from that. You can only use such a IP from android if you are connected to the SAME network (in this case 192.168.0.x)

You must use the public IP address of the PC if you are not in the same network. The port must be open in your router and routed to the correct IP in the PCs network.
I couldn't understand what you are trying to say. I AM ON THE SAME NEWTORK.
I want to understand WHOSE i.e. WHICH ENTITY'S ADDRESS is hardcoded into the B4a standard example as "192.168.0.100".

I have tried putting 1) Server's IP, 2) Mobile device's IP 3)hardcoded magic number but without success. Connection refused. I just added some extra details to the question if you can see it. I suspect it might be a firewall issue because in this same scenario I daily use AsyncStreams/TCP-IP.

(caps for emphasis only :p)
 
Upvote 0

superkan

Member
Licensed User
Don´t scream to me! I´m out here. Have a nice day
OMG Don Are you okay? Did you see my message completely. I just markeddown the text like that. I should have bolded it instead! In my culture, it isn't considered shouting, Here's the part of my message:

1684866942391.png
 
Upvote 0

superkan

Member
Licensed User
ip number (IPv4) of your windows PC
Finally someone helped keep this thread short and meaningful!

I was repeating on and on, trying to help people help me, helping people to see, ENDLESSLY, and not being disrespectful when i could easily have been with proper quoting of reply segments.

Not a single functional Websocket example in this day and age. People do not appreciate the effort of putting a question to help others. Not a single proper consolidated Firebase sample. Programmers will remain programmers, without comments in their code, not taking time to read texts, not knowing what sells, and not appreciating markup/redundancy. Bunch of losers.

Thanks friend, this points me in some good direction.
 
Last edited:
Upvote 0

superkan

Member
Licensed User
The problem was
1st) missing internet permission in manifest when first installed
missing permission in original example (old):
AddPermission(android.permission.INTERNET)

2nd) added the permission but forgot to uninstall the app and re-install (same release mode)

It works fine now!
 
Upvote 0
Solution
Top