Socket Connection

pablovidal

Member
Licensed User
Longtime User
Hello,
B4A is an extraordinary tool, but lacks examples, This, because not as sockets connections implement the examples I've seen and nothing. Basically what I need is a working example for sending and receiving from an Android device to a server socket that I have currently working with other tools. thanks
 

pablovidal

Member
Licensed User
Longtime User
Really, speaking, this example does nothing, I have everything connected to the socket, but when these fail data sent to the server.

B4X:
Sub oSocket_Connected(Connected As Boolean)As Boolean 
    If Connected = True Then
     ToastMessageShow("Conectado",True)
     oAStreams.Initialize(oSocket.InputStream,oSocket.OutputStream,"oAstreams")
    Else
     ToastMessageShow("Servidor no disponible",True)
    End If
    
End Sub


Sub SendtoSocket( cOp As String, cTrama As String )

Dim cString As String
 
 cString = "#INI|20121202~"& CRLF
 
 If oAStreams.IsInitialized = False Then Return

    If cString.Length > 0 Then
     Dim buffer() As Byte
     buffer = cString.GetBytes( "UTF8" ) 
     oAStreams.Write( buffer )  ' Aqui no envia el buffer / no send buffers
     ToastMessageShow( cString, True )     
    End If
 
End Sub
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Can you post the server code? Does it expect a Windows end of line? If yes then you should send chr(13) & chr(10) instead of CRLF.

What exactly is the difference between CRLF and Chr(13) & Chr(10)? I meant to post an FYI thread and never did, but not too long ago I spent probably a good 16 hours over the course of several days trying to figure out why several lines of post headers and XML data sent to a raw socket was not working despite every other bit of sample code floating around on the web saying I had it right. By dumb luck I discovered that I needed to terminate each line with Chr(13) & Chr(10), not CRLF as I was doing. :BangHead:
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
What exactly is the difference between CRLF and Chr(13) & Chr(10)? I meant to post an FYI thread and never did, but not too long ago I spent probably a good 16 hours over the course of several days trying to figure out why several lines of post headers and XML data sent to a raw socket was not working despite every other bit of sample code floating around on the web saying I had it right. By dumb luck I discovered that I needed to terminate each line with Chr(13) & Chr(10), not CRLF as I was doing. :BangHead:

I think crlf.length and asc(crlf) will give a short answer :) I didn't know exactly, assuming that it was identical to chr(13) & chr(10).
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I think crlf.length and asc(crlf) will give a short answer :) I didn't know exactly, assuming that it was identical to chr(13) & chr(10).

That's why it took me so long to try it, because I thought they were the same thing, but certainly not when sending data to sockets. I still can't believe how much time I wasted on such a silly thing. :eek:
 
Upvote 0
Top