Android Question [Solved] Astream or Socket Problem

hogiebaer

Active Member
Licensed User
Longtime 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)

..
B4X:
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:

hogiebaer

Active Member
Licensed User
Longtime User
Ok, i have a dirty solution

B4X:
    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:
Upvote 0

hogiebaer

Active Member
Licensed User
Longtime 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)

B4X:
sub checkclose_tick
  if astream.outputqueuesize<>0 then return
  checkclose_tick.enable=false
  astream.close
  socket1.close
end sub
 
Upvote 0
Top