Android Question work in debug - not working in Release

boadzulu

Member
Licensed User
Longtime User
Hello
I have problem with following code which is used for sending string from uC to Android app(using Tcp socket).
In Debug mode its working fine - in Release it generate error.
There is app code:
Main
B4X:
#Region  Project Attributes
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName:
   #BridgeLogger: True
   '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
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Private EMessage As EditText
   Private EResponse As EditText
   Private EAddress As EditText
   Private EPort As EditText
  
   Private Client As TcpClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("PSTCPLayout")
   EResponse.Enabled = False
   EResponse.TextColor = Colors.Green
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Client_Connected
   Client.SendMessageString(EMessage.Text & Chr(13))
End Sub

Sub Client_ResponseReady
   EResponse.Text = Client.GetStringResponse
End Sub

Sub BSend_Click
   Dim s_address As String = EAddress.Text
   Dim s_port As Int = EPort.Text

   Client.Initialize(s_address, s_port, 5000)
   Client.RegisterConnectedEvent(Me, "Client_Connected")
   Client.RegisterResponseReadyEvent(Me, "Client_ResponseReady")
  
   Client.Connect
End Sub

TcpClient
B4X:
'Class module
Sub Class_Globals
   Private TCPSocket As Socket
   Private server_address As String
   Private server_port As Int
   Private timeout As Int
  
   Private response() As Byte
  
   Private event_method_name As String
   Private event_module_name As Object
  
   Private last_error As String
  
   Private responded_event_method_name As String
   Private responded_event_module As Object
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(s_address As String, s_port As Int, s_timeout As Int)
   server_address = s_address
   server_port = s_port
   timeout = s_timeout
   TCPSocket.Initialize("TCPSocket")
End Sub

Public Sub RegisterConnectedEvent(target_module_name As Object, target_method_name As String)
   event_method_name = target_method_name
   event_module_name = target_module_name
End Sub

Public Sub RegisterResponseReadyEvent(target_module_name As Object, target_method_name As String)
   responded_event_method_name = target_method_name
   responded_event_module = target_module_name
End Sub

Public Sub Connect
   TCPSocket.Connect(server_address, server_port, timeout)
End Sub

Public Sub Disconnect
   If TCPSocket.Connected Then TCPSocket.Close
End Sub

Public Sub isConnected As Boolean
   Return TCPSocket.Connected
End Sub

Private Sub TCPSocket_Connected (Successful As Boolean)
   If Successful Then
     CallSub(event_module_name, event_method_name)
   End If
End Sub

Public Sub GetStringResponse As String
   Dim cv As ByteConverter
   Return cv.StringFromBytes(response, "UTF8")
End Sub

Public Sub GetArrayOfByteResponse As Byte()
   Return response
End Sub

Public Sub SendMessageString(mess As String)
   SendMessageByteArray(mess.GetBytes("UTF8"))
End Sub

Public Sub SendMessageByteArray(mess() As Byte)
   Dim os As OutputStream
   Dim in_s As InputStream
   Dim buffer(1024) As Byte
   'Dim bufor As String
  
  
   os = TCPSocket.OutputStream
   os.WriteBytes(mess, 0, mess.Length)

   in_s = TCPSocket.InputStream

   ' Wait for data to arrive in the buffer, if no data defined timeout is reached
   Dim start As Long = DateTime.now
   Do Until (DateTime.now>start+timeout Or in_s.BytesAvailable > 0)
  'DoEvents
     'Sleep(2)
   Loop
  
   If in_s.BytesAvailable > 0 Then
     in_s.ReadBytes(buffer, 0, in_s.BytesAvailable)
     response = buffer
     CallSub(responded_event_module, responded_event_method_name)
   Else
     last_error = "No response from server"  
   End If
End Sub

Public Sub GetLastError As String
   Return last_error
End Sub

It's debug console:
Logger connected to: HUAWEI HUAWEI CUN-L21
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: android.os.NetworkOnMainThreadException
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1656)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1180)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:600)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:406)
at b4a.example.tcpclient$ResumableSub_SendMessageByteArray.resume(tcpclient.java:224)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1654)
... 8 more
--------- beginning of system
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
tcpclient_sendmessagebytearray (java line: 170)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1180)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:600)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:406)
at b4a.example.tcpclient._sendmessagebytearray(tcpclient.java:170)
at b4a.example.tcpclient._sendmessagestring(tcpclient.java:185)
at b4a.example.main._client_connected(main.java:377)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1044)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:978)
at b4a.example.tcpclient._tcpsocket_connected(tcpclient.java:194)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
android.os.NetworkOnMainThreadException

Can you point me out what I'm doing wrong?
Thanks!

Przemek
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

boadzulu

Member
Licensed User
Longtime User
Thanks for reply.
So - in few words - what I should change/modify in above code to get this app working properly?
Im sorry for that noob question.

Erel
Can you point me to app/samples (TCP socket client) which will work with AsyncStreams?
I tried to understand AsyncStream tutorial however it's easier for me to understand how somethign work when I can use "ready" code - modifying it for my requirments.
Thank you very much.

Regards

Przemek
 
Upvote 0

boadzulu

Member
Licensed User
Longtime User
Erel, Thank you.
After compiling - when I try to run that default sampel code - app close with following error.
It will be great if you will tell me whats wrong now.
Thanks!

Logger connected to: HUAWEI HUAWEI CUN-L21
--------- beginning of main
*** Service (starter) Create ***
starter_service_create (java line: 387)
java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:387)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 20 more
*** Service (starter) Create ***
starter_service_create (java line: 387)
java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:387)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 20 more
java.lang.RuntimeException: Unable to create service b4a.example.starter: java.lang.RuntimeException: java.net.BindException: bind failed: EACCES (Permission denied)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3149)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: java.lang.RuntimeException: java.net.BindException: bind failed: EACCES (Permission denied)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:223)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
... 9 more
Caused by: java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:387)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
... 12 more
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 20 more
*** Service (starter) Create ***
starter_service_create (java line: 387)
java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:387)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 20 more
--------- beginning of system
*** Service (starter) Create ***
starter_service_create (java line: 387)
java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:387)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 20 more
java.lang.RuntimeException: Unable to create service b4a.example.starter: java.lang.RuntimeException: java.net.BindException: bind failed: EACCES (Permission denied)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3149)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: java.lang.RuntimeException: java.net.BindException: bind failed: EACCES (Permission denied)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:223)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
... 9 more
Caused by: java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:387)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
... 12 more
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 20 more
Copying updated assets files (1)
*** Service (starter) Create ***
Error occurred on line: 16 (Starter)
java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:456)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 25 more
** Service (starter) Start **
 
Upvote 0

boadzulu

Member
Licensed User
Longtime User
Ok, I added permision to manifest file but still getting that error(below).
For your information - I need to connect with uC working with ESP8266.
After opening connection on port 23 - I want to send some strings (like "L200") and read answer sent from uC.
I see that problem is releated with line:
server.Initialize(PORT, "server")

Logger connected to: HUAWEI HUAWEI CUN-L21
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
Error occurred on line: 16 (Starter)
java.net.BindException: bind failed: EACCES (Permission denied)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:137)
at java.net.ServerSocket.<init>(ServerSocket.java:105)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at b4a.example.starter._service_create(starter.java:456)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3129)
at android.app.ActivityThread.access$2200(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1625)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
at libcore.io.Posix.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
at libcore.io.IoBridge.bind(IoBridge.java:102)
... 25 more
** Service (starter) Start **
 
Upvote 0

boadzulu

Member
Licensed User
Longtime User
Ok, so I moved it forward - using very easy code:
https://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/page-2#post-121661
I can start connection and send string to uC - but I can't read from buffer string sent from uC
Every string sent by uC to buffer is confirmed by Chr(13) - but label from above code(named Received from SVR) is not updated with incoming string.
I see on console of ESP8266 that uC is sending string out to app - but I don't have idea why app can't see that.
I tried to log incoming strings - but looks that nothing arrived to incoming buffer.
From hardware point of view - it's 100% correct(checked on another platform).
Maybe I should send string ended with exact char? like Chr(10), Chr(26) or crlf?

Again - thank you for your explanations fro dumb like me :)
BIG THANK YOU.
It's a code used from sample:
B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: True
   #ApplicationLabel: Test Client Socket
   #VersionCode: 2
   #VersionName: 1.1
   #SupportedOrientations: portrait
   #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim CltSock As Socket
   Dim Astreams As AsyncStreams
   ' If you install the "SocketServer" on local machine - change this ip to "127.0.0.1"
   Dim ip As String : ip = "192.168.1.30"
 
   Dim port As Int: port = 23
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim btn_client As Button
   Dim lbl_status As Label
   Dim EditText1 As EditText
   Dim txt_out As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("ClientForm")
   EditText1.Text = ip
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AStreams_NewData (Buffer() As Byte)
  Dim msg As String
  msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
 
   lbl_status.Text = msg
 
End Sub

Sub btn_client_Click
   CltSock.Initialize("Client")
   CltSock.Connect(ip,port,20000)
 
End Sub

Sub Client_Connected(ConStatus As Boolean)
   If ConStatus = True Then
     Msgbox("Connection Successful","")
  Astreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
  Else
      Msgbox(LastException.Message, "Error connecting")
   
   End If 

End Sub

Sub EditText1_EnterPressed
   ip = EditText1.Text
End Sub
Sub txt_out_EnterPressed
    If Astreams.IsInitialized = False Then Return
  If txt_out.Text.Length > 0 Then
     Dim sNewLine As String
     sNewLine = txt_out.text & Chr(13)
  Dim buffer() As Byte
  buffer = sNewLine.GetBytes("UTF8")
  Astreams.Write(buffer)
 
     ToastMessageShow("Sending:" & sNewLine,False)
  End If
End Sub
 
Last edited:
  • Like
Reactions: eps
Upvote 0
Top