Android Question Socket is not connected error [fixed, not solved]

Didier9

Well-Known Member
Licensed User
Longtime User
I am getting this error:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 345 (Starter)
java.net.SocketException: Socket is not connected
at java.net.Socket.getInputStream(Socket.java:937)
at anywheresoftware.b4a.objects.SocketWrapper.getInputStream(SocketWrapper.java:239)
at b4a.sabus.starter._tcpsck1_connected(starter.java:1077)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

when the AStreams_Initialize() function is called in this function block.

B4X:
Sub TCPSck1_Connected( success As Boolean )
	'If TCPSck1.Connected = True Then
	If success Then
		If AStreams.IsInitialized = False Then
			AStreams.Initialize( TCPSck1.InputStream, TCPSck1.OutputStream, "Astreams" )
		End If
	End If
End Sub

(I removed unnecessary code for clarity.)

The socket opens with success = TRUE, yet a couple lines of code below, ASreams_Initailize() fails because the socket is closed?

The strange part is that this code worked a little while ago, and I have not made purposeful changes to the way the socket is opened. I have a similar app that works fine on the same machine that uses a lot of the same code, talking to the same target.

At the moment, I am not specifically looking for a fix to this, just some guidance as to what could cause that kind of problem (tcp_Connected( ) is True but tcp socket is closed). The app is pretty big and without the target that this is intended to communicate with, it's won't be very useful to post it.

Thanks for any insight.
 

QtechLab

Active Member
Licensed User
Longtime User
I am getting this error:


when the AStreams_Initialize() function is called in this function block.

B4X:
Sub TCPSck1_Connected( success As Boolean )
    'If TCPSck1.Connected = True Then
    If success Then
        If AStreams.IsInitialized = False Then
            AStreams.Initialize( TCPSck1.InputStream, TCPSck1.OutputStream, "Astreams" )
        End If
    End If
End Sub

(I removed unnecessary code for clarity.)

The socket opens with success = TRUE, yet a couple lines of code below, ASreams_Initailize() fails because the socket is closed?

The strange part is that this code worked a little while ago, and I have not made purposeful changes to the way the socket is opened. I have a similar app that works fine on the same machine that uses a lot of the same code, talking to the same target.

At the moment, I am not specifically looking for a fix to this, just some guidance as to what could cause that kind of problem (tcp_Connected( ) is True but tcp socket is closed). The app is pretty big and without the target that this is intended to communicate with, it's won't be very useful to post it.

Thanks for any insight.
Hi Didier9,

try to connect the server with a program like Putty (for raw tcp connection) and check if the connection remains open.

Also make sure that you are not closing the socket anywhere in your code (Timers or callbacks around your project).
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Try a Sleep(100) or longer before AStreams.Initialize, I have a hazy recollection of seeing something like this a while ago - though it could be a false memory.
The problem was initially caused by the target flooding the channel (a small iot WiFi module sending null messages continuously). Still does not explain the symptom, but fixing that bug made this issue go away.

Edit: it's entirely possible that the outgoing traffic from the IOT module made it unable to complete the connection. I could not get Wireshark to display useful data (probably due to my own limited experience with it) so I am going to call it fixed for now.
 
Last edited:
Upvote 0
Top