Android Question Network problems solved

luke2012

Well-Known Member
Licensed User
Longtime User

luke2012

Well-Known Member
Licensed User
Longtime User
The network connection code seems to run successfully after a couple of mod that I have done to the original tutorial (sample).

I have only a couple of doubts:

1) Doubt1 - "Disconnect"

Within my App I need to send some data to the target (server) device and just after the send I need to close the connection. Is my code correct ?

B4X:
Public Sub ConnectToServer(Host As String, Buffer As String)

Log("Trying to connect to: " & Host)

CloseExistingConnection

Dim client As Socket

client.Initialize("client")

client.Connect(Host, PORT, 10000)


Wait For Client_Connected (Successful As Boolean)

If Successful Then

astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")

UpdateState (True)


'Print & disconnect

PrintBuffer (Buffer)

CallSubDelayed (Me, "Disconnect")


Else


Log("Failed to connect: " & LastException)

LastError = LastException

CallSub2 (Main, "ShowError", PRT1)

EndIf

EndSub

'client & astream events follow (like the Erel tutorial)....

2) Doubt2 - "Connect"
Is my code correct in order to send to 2 servers ?

B4X:
'*** Main

Sub JobDone (Job As HttpJob)

  If Job.JobName = "CheckJob" Then
      CreateBufferAndSend
  End If

End Sub


Private Sub CreateBufferAndSend
  
    Private Buffer4Printer1 As String = "Buffer1..."
    CallSub2 (Me, "send_Click", Buffer4Printer1)

    Private Buffer4Printer2 As String = "Buffer2..."
    CallSub2 (Me, "send1_Click", Buffer4Printer2)

End Sub

Sub send_Click (Buffer As String)

     CallSub2 (Starter, "Connect", Buffer)

End Sub

Sub send1_Click (Buffer1 As String)

      CallSub2 (Starter, "Connect1", Buffer1)

End Sub

'*** Starter

Public Sub Connect (Buffer As String)

       If connected = False Then
           ConnectToServer (IP, Buffer)
       End If

End Sub

Public Sub Connect1 (Buffer As String)

    If connected1 = False Then
       ConnectToServer1 (IP1, Buffer)
    End If

End Sub

Thanks in advance for your reply
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that the extra lines in your code makes it difficult to read the code.

1. Why are you using CallSub to call a sub in the current modules?
2. You haven't posted the code in PrintBuffer. Does it send data with AStream.Write? It is an asynchronous method. Don't close the connection immediately.
B4X:
PrintBuffer(...)
astream.SendAllAndClose
'the Terminated event will be raised when the connection is closed
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User

1. Yes: PrintBuffer calls SendData (Buffer.GetBytes(...)) than astream.Write.
I'm using "CallSubDelayed (Me, "Disconnect")" because I want to ensure that the disconnect sub will be called only after the astream.Write method has finished to send the whole buffer to the target connected device (probably I'm wrong).

CallSubDelayed IDE help says: "The sub will be called when the message is processed. This is useful in cases where you want to do something "right after" the current sub...

So you are telling me that I have to wait for the astream_Teminated event and only within this event I can call "Disconnect" Sub?
Otherwise my posted code (post #2 - 1. Doubt1) could introduce bugs when I send the buffer to the connected target device ?

Instead of astream.Write(data) I have to use astream.SendAllAndClose?

Or I have to call the astream.SendAllAndClose just after the PrintBuffer?
In this case all the PrintBuffer and SendData code remain the same. Correct?

If the second case is correct, which return flag I have to check to ensure that the write was executed successfully (both) ?

StreamWriteOK = astream.Write(data)
StreamWriteOK1 = astream.SendAllAndClose


B4X:
Public Sub SendData (data() As Byte)
          If connected Then
                  StreamWriteOK = astream.Write(data)
                  Log ("StreamWriteOK = " & StreamWriteOK)
          End If
End Sub

Sub PrintBuffer (Buffer As String)
      SendData (Buffer.GetBytes("UTF8"))
End Sub

So the _Teminated event should be ...

B4X:
Sub AStream_Terminated
     UpdateState(False)
     Disconnect
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
CallSubDelayed has nothing to do with AsyncStreams. It sends a message to the internal message queue which calls the sub when it is processed. It will happen very quickly.

A simple solution is to wait for a short delay and then call disconnect:
B4X:
AStream.Write(...)
Sleep(200)
Disconnect
Another option is to call AStream.SendAllAndClose which closes the connection for you after all the queued data was sent.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User

My target is to implement the most reliable solution to connect, send (data) and disconnect.

So in this case which solution is the most reliable and technical correct between A and B ?

Solution A:

PrintBuffer(...)
astream.SendAllAndClose'the Terminated event will be raised when the connection is closed
.....

B4X:
Public Sub SendData (data() As Byte)
          If connected Then
                  StreamWriteOK = astream.Write(data)
                  Log ("StreamWriteOK = " & StreamWriteOK)
          End If
End Sub

Sub PrintBuffer (Buffer As String)
      SendData (Buffer.GetBytes("UTF8"))
End Sub

So the _Teminated event should be ...

B4X:
Sub AStream_Terminated
     UpdateState(False)
     Disconnect
End Sub


Solution B

B4X:
AStream.Write(...)
Sleep(200)
Disconnect

Thanks in advance for your reply
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…