AStreams SocketTimeoutException

Priamo

Member
Licensed User
Longtime User
Hi to everyone,
i have a problem with an app that use socket and Astreams for send and receice data from a server application .net. When the app is started the same exchange any data with the server and put itself in listen for a new data by the server. Until now its all right. After 90 seconds i get an error of time out, exactly:
java.net.SocketTimeoutException
Where i wrong?
I have tried the app on two device and the error is the same.
Can you help me with a suggestion? thanks
 

Priamo

Member
Licensed User
Longtime User
Yes,

B4X:
Sub Service_Start (StartingIntent As Intent)
   If MyIP="127.0.0.1" Then
      ToastMessageShow("Connessione non disponibile",True)
      CallSubDelayed2(Main,"StatoPagina","L-1")
   Else   
       Notify.SetInfo ("QZ service...","...in backgroud...",Main)
       Service.StartForeground(1,Notify)
       Socket2.Initialize("Socket2")
      Socket2.TimeOut=90000
       Socket2.Connect(IpSERVER,portServer,gaptimaWaitconnection)
   End If
End Sub
Sub Socket2_Connected(Connected As Boolean)As Boolean 
   If Connected = True Then
      AStreams2.Initialize(Socket2.InputStream , Socket2.OutputStream,"Astreams2")
      If IsOnLine=False Then
         StringaDaInviare="LogIn" & GapChar & CodiceUtente & GapChar &  MyIP & GapChar 
         SendData(StringaDaInviare)
      End If
   Else
      ToastMessageShow("Server not available",True)
      Socket2.Close
      CallSubDelayed2(Main,"StatoPagina","L-1")
   End If
End Sub
   Sub AStreams2_NewData(Buffer1() As Byte)
    Dim appo As String
   Dim msg() As String
   Dim cMsg As String 
   Dim t As String 
   Dim x As Int 
   Dim unsignedi As Int
   Dim signedb As Byte   
    appo = BytesToString(Buffer1, 0, Buffer1.Length, "ASCII")
   msg=Regex.split(GapChar,appo)
   Select Case msg(0).ToUpperCase
      Case "LogInOK".ToUpperCase
         If CodiceUtente=msg(1) Then
                  IsOnLine=True
                 NomeSquadra=msg(2)
               stato="Attesa".ToUpperCase 
            CallSubDelayed2(Main,"StatoPagina","C-0")
            CallSubDelayed2(Main,"StatoPagina","A-0")
             ToastMessageShow("Connected",True)
         End If
      Case "LogInDOPPIO".ToUpperCase
            AStreams2.Close
             Socket2.Close
           IsOnLine=False
         CallSubDelayed2(Main,"StatoPagina","L-1")
          ToastMessageShow("Giàesistente!!!",True)
      Case "LogOutOk".ToUpperCase
         If CodiceUtente=msg(1) Then
            CallSubDelayed(Main,"ChiudiApplicazione")
         End If
      Case "LogOutDaAdminNoMessage".ToUpperCase
         If CodiceUtente=msg(1) Then
            CallSubDelayed(Main,"ChiudiApplicazione")
         End If
      Case "Istruzioni".ToUpperCase
         CallSubDelayed2(Main,"StatoPagina","I-0")
           stato="Istruzioni".ToUpperCase 
      Case "Attesa".ToUpperCase
         CallSubDelayed2(Main,"StatoPagina","A-0")
           stato="Attesa".ToUpperCase 
      Case "VisualizzazioneVuota".ToUpperCase
         CallSubDelayed2(Main,"StatoPagina","V-0")
           stato="VisualizzazioneVuota".ToUpperCase 
      Case "Dm".ToUpperCase
          RE=""
          If msg(7)="1" Then
            RE="A"
         End If
          If msg(8)="1" Then
            RE="B"
         End If
          If msg(9)="1" Then
            RE="C"
         End If
          If msg(10)="1" Then
            RE="D"
         End If
          If msg(11)="1" Then
            RE="E"
         End If   
         ID_p=msg(16)
         ID_d=msg(15)
         CallSubDelayed2(Main,"StatoPagina","D-0")
           stato=msg(17) 
      Case "Rp".ToUpperCase
         CallSubDelayed2(Main,"StatoPagina","R-0")
           stato="Rp".ToUpperCase 
      Case "StopAgnostic".ToUpperCase
         CallSubDelayed2(Main,"StatoPagina","S-0")
      Case "TriggerSync".ToUpperCase
         CallSubDelayed2(Main,"StatoPagina","T-0")
         CallSubDelayed2(Main,"StatoPagina","T-1")
      Case "Cl".ToUpperCase
         If msg(1).ToUpperCase ="CP".ToUpperCase Then
            stato="CP".ToUpperCase 
            CallSubDelayed2(Main,"StatoPagina","CP-0")
         End If
         If msg(1).ToUpperCase ="CG".ToUpperCase Then
            stato="CG".ToUpperCase 
            CallSubDelayed2(Main,"StatoPagina","CG-0")
         End If
      Case "QZ".ToUpperCase
         stato="QZ".ToUpperCase 
         CallSubDelayed2(Main,"StatoPagina","Q-0")
      Case "NS".ToUpperCase
         stato="NS".ToUpperCase 
         CallSubDelayed2(Main,"StatoPagina","N-0")
      Case "CDQZ".ToUpperCase
         stato="CDQZ".ToUpperCase 
         CallSubDelayed2(Main,"StatoPagina","W-0")
   End Select
End Sub
Sub SendData(msg As String)
   Dim Buffer() As Byte    
   Buffer = msg.GetBytes("UTF8")
   AStreams2.Write(Buffer)
   If CloseApp Then
      CallSubDelayed(Main,"ChiudiApplicazione")   
   End If
End Sub
Sub AStreams2_Error
   AStreams2.Close
   Socket2.close
   Socket2.TimeOut=9000000
   Socket2.Connect(IpSERVER,portServer,gaptimaWaitconnection)
'   ToastMessageShow(LastException.Message ,True)
End Sub

Sub Socket2_Error
   AStreams2.Close
   Socket2.close
   Socket2.TimeOut=9000000
   Socket2.Connect(IpSERVER,portServer,gaptimaWaitconnection)
End Sub

Now i have added in the error method new code to restart the connection, but i would avoid this.
thanks Erel.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Not sure whether it is related but move the code from Service_Start to Service_Create. Service_Start can be called multiple times.
I suppose it is too. In fact it was a question I had but been too busy to analyse it and post here. Whenever service_start includes a new socket connection, I saw a server getting it as a new connection (I think this is the correct thing to do by the way), thus closing the socket which was supposed to remain open. At least so I saw, but as I said with not much investigation :)
 
Upvote 0

Priamo

Member
Licensed User
Longtime User
Thanks guys for your help,

i tried your suggestions, but the problem there is yet. I have saw that this problem depend of the setting of the timeout socket:

Socket.Initiate(myIp,port,90000)

Infact with setting timeout at 90000(1,5 minutes) the error starts after 90 sec.
with setting at 900000 the error starts after 15 minutes, ecc.

I don't know the limit of timeout, but also with 9000000(2,5 hours) all it's ok.

Now i tried with timeout=0, and surprise is...,

the error not starts more.
I ask you, if the setting Socket.Timeout=0 is correct, otherwise if let this setting, my app, would crash?
After 15 minutes, all it's ok?
I wait yours suggestions or advice.
thanks a lot to both.
 
Upvote 0
Top