B4J Question Close correctly a socket

marcick

Well-Known Member
Licensed User
I have a GSM device that uses GPRS and communicate with a B4J server using TCP socket.
When the device want to close communication and shut down, which is the correct way to manage sockets and Atream ?
Actually the device just close its socket, terminate GPRS connection and shut down.
Normally the server recognize this and terminate also socket and astream by itself, but sometimes I need to wait a long timeout before this happens.

So I wonder if it is better the device, before it shut down, send a command like "closeAll" to tell the server to close immediately everything.
I'm trying to implement this and when the server receive the command execute an "Astream.Close" but this generate some errors.
 

PatrikCavina

Active Member
Licensed User
I think your approach to send the command "close" from the device and close the connection from the server is correct and should not present errors.
If the client is written with b4x, you can manage closing the connection with the astream_terminated event.
Which errors do you get from this implemantation?
 

marcick

Well-Known Member
Licensed User
The method works, but I have this error (I don't like to see red errors and always think something is not done in the correct way)

B4X:
java.net.SocketException: Socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.net.SocketInputStream.read(SocketInputStream.java:127)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:199)
    at java.lang.Thread.run(Thread.java:748)
 
Top