I whipped up some code to help out with a little logging, but surprisingly often it crashes with java.io.IOException: Socket Closed in the line commented with CRASH HERE. I've wrapped everything carefully to make sure the socket should be in good shape, but even though it apparently is connected, it's considered closed in the line after. How is this even possible? Am I missing something obvious?
logThis_Error and logThis_Terminated are never called.
And on the receiving end is just a dead simple receiver that dumps out whatever is sent to it, so I'm very sure that side isn't part of the problem. (It's just the command ncat -k -l 5010 running on a Linux machine.)
logThis_Error and logThis_Terminated are never called.
And on the receiving end is just a dead simple receiver that dumps out whatever is sent to it, so I'm very sure that side isn't part of the problem. (It's just the command ncat -k -l 5010 running on a Linux machine.)
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
' For logging purposes only
Private socket As Socket
End Sub
Public Sub logThis (text As String)
Dim astream As AsyncStreams
Dim data() As Byte
Dim Successful As Boolean = False
If Not(socket.IsInitialized) Then
socket.Initialize("socket")
End If
If Not(socket.Connected) Then
socket.Connect("192.168.1.14", 5010, 30000)
Wait For socket_Connected (Successful As Boolean)
End If
' Exit early if it wasn't successful
If Not(Successful) Then
Log("logThis: Not successful")
Return
End If
If socket.Connected Then
astream.Initialize(Null, socket.OutputStream, "logThis") ' <-- CRASH HERE
data = (text & CRLF).GetBytes("UTF8")
astream.Write(data)
Else
Log("logThis: COULD NOT CONNECT")
End If
End Sub
Public Sub logThis_Error
Log("logThis: ERROR")
If LastException.IsInitialized Then
Log(LastException.Message)
Else
Log("LogThis: No exception message available")
End If
End Sub
Public Sub logThis_Terminated
Log("logThis: TERMINATED")
If LastException.IsInitialized Then
Log(LastException.Message)
Else
Log("LogThis: No exception message available")
End If
End Sub