Secure Sockets

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am trying to create a app that will connect to a secure socket (SSL) and send a ASCII message to it but having a small issue in doing it with a username and password.

I am using this library: SocketSSL.zip

I am able to connect to my device with the secure SSL connection using this library.

However, the product I am connecting to allows you to set a username and password.

When I set a username and password in the product, then try and connect to it, it then requires a username and password to be able to connect to this secure socket.

If I try and connect to it without any username and password and I 'log' the reply from this connection it logs:

Username:

How can I include the username and password when I connect to this device?

Incase there is another way to do what I am trying to do, this is what I am trying to do:

Connect to a IP:port (with a username and password) send a ASCII message to it and listen for a reply from this device.
Then, keep the connection open for any other reply's that it will automatically send without notice. (all ASCII based messages).
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I am using AsyncStreams to send the ASCII messages between the device and product.

I have attached code in a zip file for you to take a look at.

When I run this code on my device it logs the following:

Installing file.
~i:** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Connected - SSL Socket
incoming data -
Username:
~i:** Activity (main) Resume **

If I remove the username and password in my product then connect to it, then it allows me to send data without any problems.

Based on my code in the example attached is there something I am missing to allow username and passwords as I am not sure how to do it?

You can connect to my device online if you want but I can't publish the IP or port in the forum but am happy to send it to your in a email or PM.
 

Attachments

  • ssl_socket_example.zip
    17.5 KB · Views: 210
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Did you copy these logs from the IDE? The ~i: tokens should have been removed.

I copied the log from the IDE but I was using the version before the version that just come out.


I still don't understand something. What is your server expecting?

I am not quite sure what you mean by this.

I will have to keep playing around to see how I can do the username and password in the SSL socket.


Edit: Using version 2.70 of B4A it doesn't show the ~i tokens.
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I have found out when I get a reply from my device I need to send a ASCII message to the device..

So for example, when I connect to the device it will automatically send a message 'Username:' then I need to send a ASCII message back to it for example 'aaron'.

However when I do send that message back I get a error in B4A.
Does anyone know what this following error means?

Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Connected - SSL Socket
incoming data -

Username:
main_senddata (B4A line: 61)
AStreams_ssl.Write(Buffer)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1126)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:686)
at java.io_OutputStream.write(OutputStream.java:82)
at elbarto.android.AsyncStreamsSSL.Write2(AsyncStreamsSSL.java:67)
at elbarto.android.AsyncStreamsSSL.Write(AsyncStreamsSSL.java:60)
at b4a.example.main._senddata(main.java:453)
at b4a.example.main._button1_click(main.java:335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
at android.view.View.performClick(View.java:4147)
at android.view.View$PerformClick.run(View.java:17161)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4786)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
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.
B4X:
aout.out.write("\n".getBytes());
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
So how do we fix this? Move it to a service?

EDIT: Nope, didn't work.

Oh, it's in the java code, not the B4A code. So only the guy who made the library can fix it.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
So how do we fix this? Move it to a service?

EDIT: Nope, didn't work.

Oh, it's in the java code, not the B4A code. So only the guy who made the library can fix it.

It's now working for me fine. (without modifying the Java Code, and I don't have the Java code only have the B4A Library)
I have used it in a Service Module in B4A and I have also tested it on Android 2.3 and Android 4.2.2 and works fine for both devices.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@swissmade When you are talking about B4J then you have written in the wrong forum. This is the B4A-Part of the forum
 
Upvote 0
Top