Android Question Write and read on ip address

roby128128

Member
Hello I found this code posted by Babo in 2019 and it works perfectly for me, but now I need to listen to the messages sent to me by the printer such as paper end etc, how can I receive the result of the writing? . Thanks



Read and write on ip:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A ip writer
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim Socket1 As Socket
    Dim Printer1 As TextWriter
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    
    Activity.LoadLayout("1")
    
    Socket1.Initialize("Socket1")
    Socket1.Connect("192.168.100.202", 9100, 20000)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox("Error", "")
        Return
    End If
    
    Printer1.Initialize(Socket1.OutputStream)

    Msgbox("Connected", "")
End Sub

Sub Button1_Click
    Printer1.WriteLine("Testrow")
        
    Msgbox("Print", "")
End Sub
 

roby128128

Member
Thanks Erel for answering me, in fact the code is not beautiful but it is simple only to send print commands to a network printer, but without knowing if the printing was successful. I saw your example is clearly complete to do what I need I see to solve with your example thanks always for your availability.
 
Upvote 0

roby128128

Member
Hi Erel I used the client example, it works but I don't get any error message when there is no paper in AStreams_NewData while with test VB6 "windows" the printer responds with the error code how can I intercept the response? Thanks
Read and write on ip:
Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Test Client Socket
    #VersionCode: 2
    #VersionName: 1.1
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim CltSock As Socket
    Dim Astreams As AsyncStreams
    ' If you install the "SocketServer" on local machine - change this ip to "127.0.0.1"
    Dim ip As String : ip = "192.168.0.100"
    
    Dim port As Int: port = 9100
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim btn_client As Button
    Dim lbl_status As Label
    Dim EditText1 As EditText
    Dim txt_out As EditText
    Private send As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("ClientForm")
    EditText1.Text = ip
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  
    lbl_status.Text = msg
  
End Sub

Sub btn_client_Click
    CltSock.Initialize("Client")
    CltSock.Connect(ip,port,20000)
    
    
    
End Sub

Sub Client_Connected(ConStatus As Boolean)
    If ConStatus = True Then
        Msgbox("Connection Successful","")
        Astreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
    Else
         Msgbox(LastException.Message, "Error connecting")
        
    End If   



End Sub

Sub EditText1_EnterPressed
    ip = EditText1.Text
End Sub



Private Sub send_Click
    If Astreams.IsInitialized = False Then Return
 
    Dim sNewLine As String
    sNewLine = "Ciao" & CRLF
    Dim buffer() As Byte
    buffer = sNewLine.GetBytes("UTF8")
    Astreams.Write(buffer)
    Log(sNewLine)
    ToastMessageShow("Sending:" & sNewLine,False)

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1:

1632288366556.png


2:

There are still many mistakes in this code. First one is using an activity to manage a network connection. Switch to B4XPages: https://www.b4x.com/android/forum/t...-asyncstreams-b4xserializator.119011/#content

3:

Code with Msgbox = broken code.

4:
Add Log(msg) to AStreams_NewData.
 
Upvote 0
Top