Android Question [Solved] Astream or Socket Problem

Discussion in 'Android Questions' started by hogiebaer, Jun 8, 2015.

  1. hogiebaer

    hogiebaer Active Member Licensed User

    Hi Devs,

    I have a big problem with AStreams or Sockets.
    When printing on a receipt printer over port 9100 i use

    Socket1.Connect(printer_ip,9100,4000)

    ..
    Code:
    sub socket1_Connected (Successful As Boolean)
    if successful=true then
      astream.initialize(....)
      Astream.Write(data)
      Astream.close
      Socket1.Close
    end if
    end sub
    This code works. Most of the time. But sometimes the Astream.Write doesnt finish, or doesnt get started at all before the Astream.Close event happens, or the socket close.

    If i don´t close the stream/socket it works, but i have to close the stream/socket, because the printer
    can´t reach from another station, if the connection is online !!

    What can i do ????

    Thx

    Use b4a 4.30, network(1.25), randomaccessfile(2.00)
     
    Last edited: Jun 8, 2015
  2. hogiebaer

    hogiebaer Active Member Licensed User

    Ok, i have a dirty solution

    Code:
    astream.write(data)
        
    Do While astream.OutputQueueSize<>0
             
    DoEvents
        
    Loop
        astr.Close
        psock.Close
    but it could be very dangerous, i hate "doevents"

    other solutions ?
     
    Last edited: Jun 8, 2015
  3. Erel

    Erel Administrator Staff Member Licensed User

    Does the printer return any response when it is done printing? If not then you should use a timer instead of the Do While loop.
     
  4. hogiebaer

    hogiebaer Active Member Licensed User

    Hi Erel, no response, the only "error" is for me the "connected=false" or "astream events".
    The way over a timer is a good idea.

    i could start a timer after write (100msec)

    Code:
    sub checkclose_tick
      
    if astream.outputqueuesize<>0 then return
      checkclose_tick.enable=
    false
      astream.close
      socket1.close
    end sub
     
    Erel likes this.
Loading...