Android Question A question for fellow B4A socket programmers and the wonderful Erel

Simon Smith

Active Member
Licensed User
Dear guys,

I can do this in 1.2 seconds in VB, but I have tried the libraries here and they all seem to be based around protocols and fixed ports, http, ftp. Is there a "TCP (Don't need UDP) Socket Library" where I can pick a server ip or url, set a port and connect and pass data back and forth similar to the Winsock control and any TCP/IP Socket communication - not just the ports restricted to the libraries?

Yours
Simon
 

Simon Smith

Active Member
Licensed User
I reiterate, I understand I can append a port to a URL :88 (for example) but I must be in complete control of the communication and no HTTP headers can be sent. Direct connect.
 

Simon Smith

Active Member
Licensed User
Hi I understand. So there is currently no port, but you are alluding to perhaps using b4j to make a library and calling that?
 

giga

Well-Known Member
Licensed User

Simon Smith

Active Member
Licensed User
Hi all thanks for all that. Well I put my creative hat on. Got a socket connection, but then ran into the 4 integer issue, then introduced the text method, but it doesn't have a connect like the other. I'm just looking for some code that looks like this in vb.

'globals
dim winsock1 as winsock

sub Command1_click
if winsock1.state=sckconnected then Winsock1.close
Winsock1.remoteport=100
Winsock1.remotehost="111.222.333.444"
winsock1.connect
end sub

Events
Sub Winsock1_Connected
MsgBox("Cool we connected",app.title)
Winsock1.senddata "hello" & chr(13) & chr(10)
End Sub

The other side gets hello [enter]
The other side would send goodbye so i just want to see

Sub Winsock1_DataArrival()
Dim s as string
s=winsock1.getdata
Msgbox (s,app.title)
'AND THERE WE HAVE THEN WORD goodbye
end sub

Sub Winsock1_Closed
'Thanks i know its closed
end sub

Its hard going through 10 pages of code from 2 different samples to something I achieve here in min 6 lines of code? Is there not a better way?
It would make a powerful library, yet to me so simple - I can do it in raw java even, so even if there is an example of executing .java functions it might help - but then i have to figure out how to link it. Anyone got the B4A skill to make a tiny library for this? Even with win32API its no more than 20 lines. Hope someone can.
 

Jeffrey Cameron

Well-Known Member
Licensed User
I have a service that handles connecting to, or hosting, multiple TCP connections that I posted in this thread.

It uses bracketed mode which is fine if you're connecting to another B4A device. If not, you will need to code your other end to prefix the bytes sent with the byte count.
 

Simon Smith

Active Member
Licensed User
Spot on! I have no control of the other end. I now understand the framing etc. My question is, if there is an ftp library, surely the raw tcp functionality is there, just change the code and enter different commands.
I am actually doing this for telnet. But if it can do ftp, it can do any. I understand Websockets protocol is not the same as TCP. I actually thought of trying to find a html5 telnet but no luck. Has to be client side only. Is there source to the ftp libary?
 

Simon Smith

Active Member
Licensed User
The server needs to expect a Websocket. It just terminates. Prefix mode seemed mandatory when I tried (4 byte integer) but even then I don't think Websocket in either HTML5 or the library works. The simplest solution would be a html5 example. If it can do VNC it somehow must be able to do Telnet or anything. Same with the FTP library. The protocol of FTP is initiated by text. My server expects a connection, and on success a text string and enter in a second or it says byebye.
 

Simon Smith

Active Member
Licensed User
I have tried the most simplest read/write to a time server on port 13. All I want is the one line it returns of the telnet. The time servers should work just the same. The sample one doesn't work; I proved this SMTP test text server to return a string on port 13;

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Socket1.Initialize("Socket1")
    Socket1.Connect("alt4.gmail-smtp-in.l.google.com",25,40000) '"whois-check.ausregistry.net.au" , 43, 20000)
End Sub

Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    Dim tr As TextReader
    Dim tw As TextWriter
    tr.Initialize2(Socket1.InputStream,"UTF8")
    tw.Initialize2(Socket1.OutputStream,"UTF8")
    Do Until tr.IsInitialized
    DoEvents
    Loop
   
    DoEvents
    Do Until tw.IsInitialized
    DoEvents
    Loop

    DoEvents
     Dim sb As StringBuilder
    sb.Initialize
    Do Until sb.IsInitialized
    DoEvents
    Loop
    Do Until tr.Ready
    DoEvents
    Loop
   
    sb.Append(CRLF).Append(tr.Readall)
    Do While tr.Ready
        sb.Append(CRLF).Append(tr.Readall)
    Loop
    Msgbox (sb.ToString ,"")
    Socket1.Close
End Sub
 

Erel

Administrator
Staff member
Licensed User
I think that you are confusing several different technologies. WebSockets is not related to standard TCP text based connection.

You shouldn't try to hold the main thread in a loop. It will not work.
You shouldn't use TextReader and TextWriter with a network connection.

You should use AsyncStreams events.
B4X:
Sub Process_Globals
   Private socket1 As Socket
   Private astream As AsyncStreams
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     socket1.Initialize("socket1")
     socket1.Connect("alt4.gmail-smtp-in.l.google.com",25,40000)
   End If
End Sub

Sub Socket1_Connected (Successful As Boolean)
   If Successful Then
     Log("Connected!")
     astream.Initialize(socket1.InputStream, socket1.OutputStream, "astream")
   Else
     Log("error connecting: " & LastException)
   End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Dim str As String = BytesToString(buffer, 0, buffer.Length, "utf8")
   Log(str)
End Sub

Sub AStream_Error
   Log("Connection error")
End Sub

Sub AStream_Terminated
   Log("Disconneted")
End Sub
For a string based communication it is easier to use AsyncStreamsText class which will take care of building the messages.
 

Simon Smith

Active Member
Licensed User
Thanks heaps. Lots of experimenting on my part. Websockets must be some kind of protected new protocol. I like raw! Thank you.
 
Top