SSLSocket

elbarto

Member
Licensed User
Longtime User
Does anybody have a library for SSLSockets? Do you think it is difficult to make your own library for an inexperienced Java-Programmer to make a library using this class:

SSLSocket | Android Developers
 
Last edited:

elbarto

Member
Licensed User
Longtime User
That would be just what I need. No other special tricks. Would be fantastic if you could help me out with this in the Network Lib!
 
Upvote 0

elbarto

Member
Licensed User
Longtime User
Thank you very much! I think now it works. 'Think' because I get an error:

B4X:
LastException javax.net.ssl.SSLException: Not trusted server certificate

That should be a problem of the Server. Is there an easy way to avoid this in the Network Library?
 
Upvote 0

elbarto

Member
Licensed User
Longtime User
Do you get this error when you try to connect?

Is this server public?

You can try to manually install the certificate on the device.

I get the error after a successfull connect. Here:

B4X:
AsyncStreams1.Initialize(Socket1.InputStream, Socket1.OutputStream, "AsyncStreams1")

Yes, it is a public Server. The Admin sent me now an cacert.pem file. I will keep you informed if and how this worked.
 
Upvote 0

elbarto

Member
Licensed User
Longtime User
Until now I did not find a solution for my problem ("Not trusted server certificate"). Could you add a method "InitializeSSLAcceptAll" like the one that exists in "HttpClient"? First to see if then everything else works with the SSLSocket.
 
Upvote 0

elbarto

Member
Licensed User
Longtime User
I solved this with a new library (SocketSSL) that for now works with a method "trustAllCerts". Maybe later I will try to find a different solution for that. If somebody is interested in this library just let me know.
 
Upvote 0

elbarto

Member
Licensed User
Longtime User
OK, here it is. The usage is exactly the same as the Socket in the Network Library. I needed a slightly modified class for the AsyncStreams (AsyncStreamsSSL), because the Server was waiting for a "NewLine" (\n). Maybe this also can be done with the Standard AsyncStreams. Anyway, I was happy as they finally talked :icon_clap:.
 

Attachments

  • SocketSSL.zip
    26.7 KB · Views: 461
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Is this a completed library or just the source for it?

Edit: ooo, its both
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I am getting this error:

You are doing network operations on the main thread which is not allowed in later versions of Android because it slows the GUI responsiveness. Looking at the SocketSSL.jar the AsyncStreamsSSL.Write2 is directly writing to the output stream, for some reason that I cannot comprehend, and that is what is causing the error.
Code:
B4X:
aout.out.write("\n".getBytes());

Has it been fixed?
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Hi Elbarto,

I have convert your library to B4J.
Is it also possible to ask the library a certificate.
At the moment this library accept all certificates.
I'm not so good in Java and hoop, that you or anyone can help in this.

Many thanks for help,
Swissmade
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi Elbarto,

I probs being thick here, but when I do a basic project, I just getting lots of errors :

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form

    Dim AStreams As AsyncStreamsSSL
    Dim Socket1 As SocketSSL

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

    Socket1.Initialize("Socket1")

End Sub
Sub Socket1_Connected (Successful As Boolean)
    Log(Successful)
    If Successful = False Then
        Log(LastException.Message)
        Return
    End If
       
    AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
    ConnectToServer
End Sub
Public Sub ConnectToServer
    Socket1.Connect("ssl-eu.astraweb.com" , 443, 20000)
End Sub
Sub AStreams_NewData (buffer() As Byte)
Dim msg As String

    msg = BytesToString(buffer, 0, buffer.Length, "WINDOWS-1252")
    Log(msg)   

End Sub

Program started.
java.lang.RuntimeException: java.lang.NoClassDefFoundError: android/util/Log
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.start(main.java:35)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoClassDefFoundError: android/util/Log
at elbarto.android.SocketSSL.Initialize(SocketSSL.java:47)
at b4j.example.main._appstart(main.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:563)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:224)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
... 14 more
Caused by: java.lang.ClassNotFoundException: android.util.Log
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 28 more

I've added the SocketSSL.xml and SocketSSL.jar to the Libraries folder, any idea's.

Thanks for taking the time to convert this, think a lot of people will be happy using your conversion.

Regards

Potman
 
Upvote 0
Top