Android Question Incoming Data Issue

aaronk

Well-Known Member
Licensed User
Hello,

I am using the SSLSocket Library (http://www.basic4ppc.com/android/forum/attachments/socketssl-zip.13485/) and I am using it with AsyncStreamsSSL.

I am able to connect to the device I want to connect to, and seem to get new data from this device. (so far so good)

However when I connect I need to send a username to the device. The device I am connecting to will automatically send the word 'Username: ' (without the quote symbols).

Now the tricky part.. I want to capture the incoming data and then make my app automatically send the reply to the device automatically.

Here is what I have done so far..
When the app starts it will connect to my device..

B4X:
Sub Class_Globals
	Dim my_ssl_socket As SocketSSL
	Dim AStreams_ssl As AsyncStreamsSSL
End Sub

Sub Activity_Create(FirstTime As Boolean)
	my_ssl_socket.Initialize("my_ssl_socket")
	my_ssl_socket.Connect("192.168.0.100","3055",5000) 'connects to my device
End Sub

Sub my_ssl_socket_Connected (Successful As Boolean)
	If Successful Then
		Log("Connected") 'Log it saying the connection was made
		AStreams_ssl.Initialize(my_ssl_socket.InputStream ,my_ssl_socket.OutputStream ,"AStreams")
	  Else
		Log("Connection Failed") 'Couldn't connect to the device so Log it
	End If
End Sub

So far the above will connect to the device and will log if it was connected or not.

Once it's connected the following sub is fired when any incoming data is sent to my app from this connection.
B4X:
Sub AStreams_NewData (Buffer() As Byte)

' Incoming data from connection
 	Dim msg As String	
    	msg = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
	
	Log("incoming data - " & msg)
	
	If msg = "Username: " Then 'Yes there is meant to be a space at the end
		SendData("aaron" & Chr(13))
	End If
	
End Sub

The following sub is how I am sending the data to my device..
B4X:
Sub SendData(msg As String)

	Dim Buffer() As Byte 
		Buffer = msg.GetBytes("UTF8")

		AStreams_ssl.Write(Buffer)
		AStreams_ssl.Write(Array As Byte(254))
		Log("outgoing data - " & msg)
		
End Sub

Now when I run my app it will connect to my device, and will receive all the incoming data from it.
As you will see in the following screenshot it seems to log the 'Username: ' on a separate line and it doesn't log anything on the first line.

login_error1.PNG


However it doesn't send the outgoing data.

Now I split up the incoming data as it seems to log it on different lines (must have a CRLF between the commands) so my incoming AStreams_NewData sub now looks like the following:

B4X:
Sub AStreams_NewData (Buffer() As Byte)

' Incoming data from connection
 	Dim msg As String	
    	msg = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
	
	Log("incoming data - " & msg)
	
	Dim strBeforeSplit As String 
		strBeforeSplit=msg
		Dim strSplit() As String
		strSplit=Regex.Split(CRLF,strBeforeSplit)
		
		For x = 0 To strSplit.Length - 1
		
			If strSplit(x) = "Username: " Then 'Yes there is meant to be a space at the end
				SendData("aaron" & Chr(13))
			End If
			
		Next
	
End Sub

Now when I run my app it captures the 'Username: ' part and now sends the data like it should of in the first place.

However, now I am forced with another issue..
When I view the log it seems to Log the reply on separate lines where it should be all on one line. (The device I am connected to will reply back the username I send and should be all one message.)

login_error.PNG



Is there something I am doing wrong to why it's splitting up the incoming data into separate messages or is it the SSLSocket Library I am using (Is there a new version available - I am using version 1.10 of the SSLSocket Library)?
I know that device I am connecting to is sending the data out correctly as I am able to use a software package that connects in the same way and is working fine with this same device.

Anyone got any ideas ?
 

aaronk

Well-Known Member
Licensed User
Can't you use AsyncStreams with this library?

You can then use AsyncStreamsText class. It will manage the text for you.
Tried doing what you suggested but had no luck :(

Replaced the following from Class_Globals:
Dim AStreams_ssl As AsyncStreamsSSL
with
Dim AStreams_ssl As AsyncStreams

And also added the following to Class_Globals
Dim ast As AsyncSteamsText

Loaded the AsyncSteamsText.bas file to my project from http://www.basic4ppc.com/android/fo...n-working-with-streams-of-text.27002/#content

Changed the my_ssl_socket_Connected sub to support the AsyncSteamsText which I just added:
B4X:
Sub my_ssl_socket_Connected (Successful As Boolean)
	If Successful Then
		Log("Connected")
		'AStreams_ssl.Initialize(ssl_socket.InputStream ,ssl_socket.OutputStream ,"AStreams")
		
		If ast.IsInitialized Then ast.Close
      ast.Initialize(Me, "ast", ssl_socket.InputStream, ssl_socket.OutputStream)
	  Else
	  	Log("Connection Failed")
	End If
End Sub

Added the sub:
B4X:
Sub ast_NewText(Text As String)
   Log("Text: " & Text)
   Log(Text.Length)
End Sub

But now it Logs the following (all on different lines):
Connected
Text:
0

Have I done I correctly or have I missed something ?
 
Upvote 0
Top