Android Question Best way to handle sockets in a tcp client.

ardillaprogramadora

Member
Licensed User
I know and understand that android does not allow synchronous sockets, so we have to use async. My problem is that i dont know how to optimize my code acording to me needs.

To summary, i have:

a socket, a connected event, the input/output streams and the receivedata event.

Imagine i have to code an app that needs to send a lot of text like:
-order 1
-get response
-order 2
-get response
...

what is the best way to handle the responses from the server? im testing in a local network so no network congestion packet loss etc, but what if in production order 2 arrives first? how can i know that the data received is "from" order 1 without modifying the code at the server (for example, including a number in every response that would match the order number)? tcp/ip stack in android would do it for me?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
if you using one socket the transfer is like a drinking straw. (request/response)
for multiple sockets you can send the server the count first, then the data with date and id/nr.
tcp transport is error free but if the connection break you need to handle this.
it is necessary that the orders comes in any sort at server?
i guess u have a database maybe u can use a auto increment field + a sort field nr. / date time
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
but what if in production order 2 arrives first?
How would that happen to begin with? Is order 1 not sent first? BTW, who is sending the orders and who is getting the response?
 
Upvote 0

ardillaprogramadora

Member
Licensed User
How would that happen to begin with? Is order 1 not sent first? BTW, who is sending the orders and who is getting the response?
sorry i meant response 2 arrives before response 1. And yes, order 1 is sent before order 2, but due to network congestion/path selection response 2 could arrive before response 1, right? my question was about what could be the easiest way to handle that situation with async sockets...
My server is a database in c++ and client b4a.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
with async sockets
do you using one socket (with order 1,2) or more sockets parallel?
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
which library belonging this "receivedata event" or which librarys do you use ?
Socket Class from Network Library have only Connected Event.
 
Upvote 0

ardillaprogramadora

Member
Licensed User
which library belonging this "receivedata event" or which librarys do you use ?
Socket Class from Network Library have only Connected Event.
Just async... no special libraries, the event is the newdata event.... here is my testing code:
B4X:
dim mysocket as socket
Dim tcpsocket As AsyncStreams
Dim  tcpinput        As InputStream
Dim tcpoutput        As OutputStream

Sub mysocket_Connected(Connected As Boolean)As Boolean
  
    If Connected = True Then     
        tcpsocket.Initialize(myscoket.InputStream,mysocket.OutputStream,"tcpsocket")
        tcpinput = myscoket.InputStream
        tcpoutput = mysocket.OutputStream
        Dim msg As String = "testing"
        Dim Buffer() As Byte
        Buffer = msg.GetBytes("UTF8")
        tcpsocket.Write(Buffer)
    End If
  
End Sub

Sub tcpsocket_newdata (Buffer() As Byte)
    Dim Msg As String
  
    Msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  
    ToastMessageShow(Msg,True)
    mysocket.Close()
End Sub

Sub Button1_Click
  

  
    mysocket.Initialize("mysocket")
    mysocket.Connect("192.168.1.1",65000,1500)
  

End Sub
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
sorry i meant response 2 arrives before response 1. And yes, order 1 is sent before order 2, but due to network congestion/path selection response 2 could arrive before response 1, right
As long as you use the same socket to communicate with the server, message order should not be an issue. TCP should keep track of packet order and therefore should also keep track of message order (even though it has no clue about your message order). If you continuously open/close a socket (use a new socket for each communication), then order of messages is not guaranteed (only the order of the packets that make up the message). Lost messages is another issue (and should raise an error), at which point I don't know how you would recover, because TCP is totally unaware of your order process system.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i made this test without asyncstream, i can use other menüs if it is still in this click event.
it should be split into more handy sub's and be part of a class.

B4X:
Sub Socket_Click
 
    Dim Socket1 As Socket 'Network Lib
    Socket1.Initialize("Socket")
 
    Socket1.Connect("192.168.178.50",80,3000)
 
    Wait For Socket_Connected(Successful As Boolean)
    Log(Successful)
 
    'if Successful ...
 
    Log("Go on")
 
    Dim Message As String
    Message = "Hello PC"
 
    Dim b() As Byte = Message.GetBytes("UTF-8")
 
    Log("Send")
 
    Dim o As OutputStream
    o=Socket1.OutputStream
    o.WriteBytes(b,0,b.Length)
    o.Flush
 

    Dim i As InputStream
    i=Socket1.InputStream
 
    Dim buffer(1024) As Byte
    Dim count As Int = 0
    Dim length As Int = 0
 
    'response loop (need exit condition)
   Do Until count=-1
     
       'you can also send here more     
       length = i.BytesAvailable '?
       count = i.ReadBytes(buffer, 0, length)
       Sleep(0)
       Log(BytesToString(buffer,0,length,"UTF-8")) 
       'Log("Run")
   Loop
 
End Sub
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top