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.
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.
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
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
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.