How to send and receive data by socket?

dreamworld

Active Member
Licensed User
Longtime User
I could not find enough samples that can help me to undersand how the socket works in B4A.

In VB.net, I wrote the following codes

private sub start()
dim thread1 as new thread(new threadstart(addressof sendandreceivedata))
thread1.start()
end sub

private sub sendandreceivedata()
dim socket1 as new socket(...)
try
socket1.connect(...)
socket1.send(...)
socket1.receive(...)
socket1.send(...)
socket1.receive(...)
...
catch ex as exception
finally
socket1.close
end sub

How to realize this in B4A?
The way that socket works in B4A is really confusing. I wrote the following codes

Sub Socket1_Connected (Successful As Boolean)
If Successful = False Then
Msgbox(LastException.Message, "Error")
Else
Dim OStream As OutputStream = Socket1.OutputStream
OStream.WriteBytes("ZXHT".GetBytes("ASCII"))
Dim IStream As InputStream = Socket1.InputStream
While IStream.BytesAvailable>0
IStream.readbytes(...)

Loop
End If
wltxq.Close
End Sub

Is the B4A socket blocked or unblocked?
When InputStream readbytes, how to ensure that all datas have been received from server.
When can the program exit the while loop?
Socket is so important, but samples of it is so few. I only found a sample of serversocket.
 
Top