Problems socket communication between two activity

Vincent

Member
Licensed User
Longtime User
Hello
Once a socket closed, no possibility to open it again.
The application has two activities and i want to use socket communication in the activities.
thanks
 

Vincent

Member
Licensed User
Longtime User
Once Astreams is initialised, it's not possible to close the socket.
is it particular way to close a socket ?









Here is a part of the program


Sub Process_Globals
Dim socket1 As Socket
Dim astreams1 As AsyncStreams
End Sub

Sub Globals
Dim ouvert, deconnecte As Button
Dim vidange1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page1")
vidange1.Initialize("vidange")
End Sub


Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Socket1_Connected(Connected As Boolean) As Boolean
If Connected = True Then
astreams1.InitializePrefix(socket1.InputStream, False, Socket1.OutputStream, "astreams1")
End If

End Sub

Sub AStreams1_Error
Msgbox (LastException.Message , "")
End Sub

Sub ouvert_Click
socket1.Initialize("Socket1")
socket1.Connect("192.168.0.20",23,500)' IP, Port, Timeout
End Sub
Sub deconnecte_Click
astreams1.Close
socket1.Close
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
In my apps, I use aStreams and sockets in each separate activity. I'm not sure if I'm right on this, though.
I've too had some problems with closing sockets. Now, I close sockets and streams in the activity_pause section. Still, not sure if this is the correct way. Actually, I'm keeping some notes, to express all issues I've faced in a single thread.
 
Upvote 0

Vincent

Member
Licensed User
Longtime User
In my program, I want to communicate between several activity. (tcp / ip)
I send data and then I get data on a label.
Currently I get an error that appears when I change from the activity Main to activity Main3.

"An error has occured in Sub:main3$ResumeMessagerun (Java line: 190) java.lang.Exception: Sub socket1_connected was not found"

Thank you for your help





sorry for my english



Application with page1, page2 and page3

Sub Process_Globals
Dim socket1 As Socket
Dim astreams1 As AsyncStreams
End Sub

Sub Globals
Dim ouvert, deconnecte As Button
Dim vidange1, label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page1")
vidange1.Initialize("vidange")
If FirstTime Then
socket1.Initialize("Socket1")
socket1.Connect("192.168.0.20",23,500)' IP, Port, Timeout
End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Socket1_Connected(Connected As Boolean) As Boolean
If Connected = True Then
astreams1.InitializePrefix(socket1.InputStream, False, socket1.OutputStream, "astreams1")
End If

If astreams1.IsInitialized = False Then Return
If vidange1.Text.Length > 0 Then
Dim buffer() As Byte
buffer = vidange1.Text.GetBytes("UTF8")
astreams1.Write(buffer)

End If
End Sub

Sub AStreams1_Error
Msgbox (LastException.Message , "")
End Sub

Sub change_Click
Log("closing")
astreams1.Close
socket1.Close
StartActivity("Main3")
End Sub

Sub envoy1_Click
Dim buffer() As Byte
Dim COMMANDE As String
COMMANDE="SB000003P"
buffer = COMMANDE.GetBytes("UTF8") 'text
astreams1.Write(buffer)
End Sub

Sub AStreams1_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
label1.text=msg
Dim value As String
End Sub




'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 socket2 As Socket
Dim astreams2 As AsyncStreams
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 Change2 As Button
Dim deconnecte2 As Button
Dim ouvert2 As Button
Dim vidange2, label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page2")
vidange2.Initialize("vidange")

If FirstTime Then
socket2.Initialize("Socket2")
socket2.Connect("192.168.0.20",23,500)' IP, Port, Timeout
End If
End Sub


Sub Socket2_Connected(Connected As Boolean)As Boolean
If Connected = True Then
astreams2.InitializePrefix(socket2.InputStream, False, socket2.OutputStream, "astreams2")
End If

If astreams2.IsInitialized = False Then Return
If vidange2.Text.Length > 0 Then
Dim buffer() As Byte
buffer = vidange2.Text.GetBytes("UTF8")
astreams2.Write(buffer)
End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Change2_Click
Log("closing")
astreams2.Close
socket2.Close
StartActivity("Main3")
End Sub

Sub envoy2_Click
Dim buffer() As Byte
Dim COMMANDE As String
COMMANDE="STP"
buffer = COMMANDE.GetBytes("UTF8") 'text
astreams2.Write(buffer)
End Sub


Sub AStreams2_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
label2.text=msg
Dim value As String
End Sub




'Activity module
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.

Dim retour_page1 As Button
Dim retour_page2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page3")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub retour_page2_Click
Main2.socket2.Initialize("Socket2")
Main2.socket2.Connect("192.168.0.20",23,500)' IP, Port, Timeout
StartActivity("Main2")
End Sub
Sub retour_page1_Click
Main.socket1.Initialize("Socket1")
Main.socket1.Connect("192.168.0.20",23,500)' IP, Port, Timeout
StartActivity("Main")
End Sub
 
Upvote 0

Vincent

Member
Licensed User
Longtime User
sorry, I did not download the ZIP




B4X:
Sub Process_Globals
Dim socket1 As Socket
Dim astreams1 As AsyncStreams
End Sub

Sub Globals
Dim ouvert, deconnecte As Button
Dim vidange1, label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page1")
vidange1.Initialize("vidange")
If FirstTime Then
socket1.Initialize("Socket1")
socket1.Connect("192.168.0.20",23,500)' IP, Port, Timeout
End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Socket1_Connected(Connected As Boolean) As Boolean 
If Connected = True Then
astreams1.InitializePrefix(socket1.InputStream, False, socket1.OutputStream, "astreams1")
End If

If astreams1.IsInitialized = False Then Return
If vidange1.Text.Length > 0 Then
Dim buffer() As Byte
buffer = vidange1.Text.GetBytes("UTF8")
astreams1.Write(buffer)

End If
End Sub

Sub AStreams1_Error
Msgbox (LastException.Message , "")
End Sub

Sub change_Click
Log("closing")
astreams1.Close
socket1.Close
StartActivity("Main3")
End Sub

Sub envoy1_Click
Dim buffer() As Byte
Dim COMMANDE As String
COMMANDE="SB000003P"
buffer = COMMANDE.GetBytes("UTF8") 'text
astreams1.Write(buffer)
End Sub

Sub AStreams1_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
label1.text=msg
Dim value As String
End Sub







B4X:
'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 socket2 As Socket
Dim astreams2 As AsyncStreams
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 Change2 As Button
Dim deconnecte2 As Button
Dim ouvert2 As Button
Dim vidange2, label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page2")
vidange2.Initialize("vidange")

If FirstTime Then
socket2.Initialize("Socket2")
socket2.Connect("192.168.0.20",23,500)' IP, Port, Timeout
End If
End Sub


Sub Socket2_Connected(Connected As Boolean)As Boolean 
If Connected = True Then
astreams2.InitializePrefix(socket2.InputStream, False, socket2.OutputStream, "astreams2")
End If

If astreams2.IsInitialized = False Then Return
If vidange2.Text.Length > 0 Then
Dim buffer() As Byte
buffer = vidange2.Text.GetBytes("UTF8")
astreams2.Write(buffer)
End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Change2_Click
Log("closing")
astreams2.Close
socket2.Close
StartActivity("Main3")
End Sub

Sub envoy2_Click
Dim buffer() As Byte
Dim COMMANDE As String
COMMANDE="STP"
buffer = COMMANDE.GetBytes("UTF8") 'text
astreams2.Write(buffer)
End Sub


Sub AStreams2_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
label2.text=msg
Dim value As String
End Sub


B4X:
'Activity module
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.

Dim retour_page1 As Button
Dim retour_page2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("page3")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub retour_page2_Click
Main2.socket2.Initialize("Socket2")
Main2.socket2.Connect("192.168.0.20",23,500)' IP, Port, Timeout
StartActivity("Main2")
End Sub
Sub retour_page1_Click
Main.socket1.Initialize("Socket1")
Main.socket1.Connect("192.168.0.20",23,500)' IP, Port, Timeout   
StartActivity("Main")
End Sub
 

Attachments

  • new socket2.zip
    353.9 KB · Views: 213
Upvote 0
Top