Android Question Cannot establish connection with tcp server

Cenny

Active Member
Licensed User
Longtime User
I am working on a project where information will be sent to a server using tcp protocol.
I used the code from the ‘ Android tutorial [B4X] Network + AsyncStreams + B4XSerializator’.
The address is: abouttimetcp.flyingneurons.io
The port to use is 40640
The server answers to the client who sent the data.
However, I cannot establish a connection with the server.

[CODE lang="b4x" title="Starter"]#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: 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.
Private server As ServerSocket
Private astream As AsyncStreams
Public IsConnected As Boolean
End Sub

Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
ListenForConnections
End Sub

private Sub SetState (connected As Boolean)
IsConnected=connected
CallSub(Main,"StateChanged")
End Sub

private Sub ListenForConnections
server.Initialize(1024,"server")
Log($"my IP is : ${server.GetMyWifiIP}"$)
server.Listen
Wait For server_NewConnection(Successful As Boolean, NewSocket As Socket)
Log("New connection")
If Successful Then
If astream.IsInitialized Then
astream.Close
End If
astream.Initialize(NewSocket.InputStream,NewSocket.OutputStream,"astream")
SetState(True)
End If

End Sub

private Sub astream_NewData (Buffer() As Byte)

End Sub

private Sub astream_Terminated
SetState(False)
End Sub

private Sub astream_Error
astream_Terminated
End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub

Sub Service_Destroy

End Sub[/CODE]


[CODE lang="b4x" title="Main"]#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.
Private xui As XUI
Private socket As Socket
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
Private Button1 As Button
Private lblState As Label
Private IsConnected As Boolean
Private astream As AsyncStreams
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
SetState(False)
End Sub

Sub Activity_Resume
StateChanged
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
SetState(False)

If astream.IsInitialized Then
astream.Close
End If

If socket.IsInitialized Then
socket.Close
End If

socket.Initialize("socket")
socket.Connect("15.236.175.34",40640,5000)
Wait For socket_connected (successful As Boolean)
If successful Then
SetState(True)
astream.Initialize(socket.InputStream,socket.OutputStream,"astream")
End If
End Sub

Private Sub SetState(connected As Boolean)
IsConnected=connected
If Starter.IsConnected Then
lblState.Text="State: Connected"
Else
lblState.Text="State: Disconnected"
End If

End Sub


Public Sub StateChanged
If Starter.IsConnected Then
lblState.Text="State: Connected"
Else
lblState.Text="State: Disconnected"
End If
End Sub

private Sub astream_NewData (Buffer() As Byte)

End Sub

private Sub astream_Terminated
SetState(False)
End Sub

private Sub astream_Error
astream_Terminated
End Sub
[/CODE]

[CODE lang="b4x" title="Manifest"]'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
AddPermission(android.permission.INTERNET)
'End of default text.
[/CODE]
 
It's not any problem to establish connection with tcp server.
In your Main, you can add log(successful) ,and the connecting to abouttimetcp.flyingneurons.io is true .
[CODE lang="b4x" title="Main"] socket.Connect("15.236.175.34",40640,5000)
Wait For socket_connected (successful As Boolean)
If successful Then
log(successful) 'it is true'
SetState(True)
astream.Initialize(socket.InputStream,socket.OutputStream,"astream")
End If[/CODE]

What is your TCP Server, does it support AsyncStreams + B4XSerializator?
I guess your tcpserver is not written in b4x
 
Last edited:
Upvote 0
as I wrote in another your thread, you can't connect because the port is closed !!
First you need to solve this problem, then you will can advance to the second

Immagine2.png
 
Upvote 0
Yes but it is still a waste of time.

And your server doesn't support AsyncStreams in prefix mode and it doesn't support B4XSerializator.
I was not aware of that.
Can B4XPages tackle this problem?
 
Upvote 0
as I wrote in another your thread, you can't connect because the port is closed !!
First you need to solve this problem, then you will can advance to the second
The port is open and you can connect to the server. the key problem is the server doesn't support AsyncStreams+B4XSerializator.
I was not aware of that.
Can B4XPages tackle this problem?
As Erel said in #8, do you know which protocol is used in the server? if it is a B4J server, I think it can tackle this problem.
 
Upvote 0
The port is open and you can connect to the server. the key problem is the server doesn't support AsyncStreams+B4XSerializator.

As Erel said in #8, do you know which protocol is used in the server? if it is a B4J server, I think it can tackle this problem.
All I know is that the server uses tcp protocol.
No idea if it is a B4J server.
 
Upvote 0
Back
Top