B4A Library Network v1.50 - Adds support for SSL sockets

This update adds support for SSL sockets (client sockets).

Sub Process_Globals
   Private so As Socket
   Private astream As AsyncStreams
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   so.InitializeSSL("so", Null, "")
   so.Connect("bing.com", 443, 0)
End Sub

Sub so_Connected (Successful As Boolean)
   If Successful Then
     astream.Initialize(so.InputStream, so.OutputStream, "astream")
     Dim s As String = _
$"GET / HTTP/1.1
Host: www.bing.com

   s = s.Replace(CRLF, Chr(13) & Chr(10))
   End If
End Sub

Sub astream_NewData (Buffer() As Byte)
   Log(BytesToString(Buffer, 0, Buffer.Length, "utf8"))
End Sub

Sub astream_Error
End Sub

Sub astream_Terminated
End Sub

You can use a custom keystore if needed. The first section in this blog explains how to create the keystore: https://assylias.wordpress.com/2012...er-from-android-with-self-signed-certificate/
You should then pass an input stream to InitializeSSL:
Dim in As InputStream = File.OpenInput(File.DirAssets, "test.keystore")
so.InitializeSSL("so", in, "123456")

You can also use InitializeSSLAcceptAll method to accept all certificates automatically. This method is less secure and should be mainly used for testing.

Installation instructions:
- Download the attached zip file and copy the files to the internal libraries folder.


  • Network.zip
    18 KB · Views: 788


Well-Known Member
Licensed User
Longtime User
Is there a way to select what version SSL you are going to connect with?

When I try and connect I am getting: (SSLHandshakeException) javax.net.ssl.SSLHandshakeException: Connection closed by peer

The product I am connecting to only supports TSLv1 and since Lollipop come out Android now defaults it's self to a newer version which means I had to force the connect to version TLSv1 since the product I am connecting to uses TLSv1.

socket.setEnabledProtocols(new String[] { "TLSv1", "SSLv3" });

Can the same thing be added to this Library? Maybe a option to select what version of SSL TLS you want to use ?


Well-Known Member
Licensed User
Longtime User
You can use this code to set the protocols:
Dim r As Reflector
r.Target = so 'socket variable
r.Target = r.GetField("socket")
r.RunMethod4("setEnabledProtocols", Array(Array As String("TLSv1", "SSLv3")), _
     Array As String("[Ljava.lang.String;"))
Perfect, seems to of done the job nicely.
Thanks heaps for providing SSL sockets to the network Library.


Well-Known Member
Licensed User
Longtime User
Using socket object, Is there a way (event) to intercept a unexpectedly broken connection ? (ex. wifi is out of range or the target IP device is unexpectedly powered off).

For example in my test I powered off the connected IP device but when I check the connection using "Starter.so.Connected" the property says true (but the connected device isn't powered on).

I defined the _Error and _Terminated events but they are never fired (when I power off the IP device).
These events is fired when I go out out WIFI range.
All the code is defined within the Starter service.
Last edited: