Here is how I am opening a TCP socket:
That works fine, unless the socket is already open by another client. In that case, the server does open the socket but closes it immediately after the SYN-ACK/ACK ("Connection is forcefully rejected"). I do get this in the log:
I tried to separately declare a Sub tcpSocket( Connected as Boolean ) event to catch the socket being closed right after it is opened but
the event does not fire and my application is left to believe the socket is open.
How can I trap the SocketException event?
Here is a simple example that shows the problem (there will be no point running it if you do not have the proper target running the proper code to talk to it).
B4X:
tcpSocket.Connect( IPAddress, EthernetPort, 5000 )
Wait For tcpSocket_Connected( Connected As Boolean )
If tcpSocket.Connected = True Then
Astream.Initialize( tcpSocket.InputStream, tcpSocket.OutputStream, "AStream" )
Log( "TCP socket is open" )
Else
Log( "TCP socket failed to open" )
End If
That works fine, unless the socket is already open by another client. In that case, the server does open the socket but closes it immediately after the SYN-ACK/ACK ("Connection is forcefully rejected"). I do get this in the log:
B4X:
TCP socket is open
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
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:216)
at java.lang.Thread.run(Thread.java:748)
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:143)
at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:355)
at java.lang.Thread.run(Thread.java:748)
Error: (SocketException) java.net.SocketException: Connection reset by peer: socket write error
Error: (SocketException) java.net.SocketException: Connection reset by peer: socket write error
TimeOut: 0
the event does not fire and my application is left to believe the socket is open.
How can I trap the SocketException event?
Here is a simple example that shows the problem (there will be no point running it if you do not have the proper target running the proper code to talk to it).
Attachments
Last edited: