B4J Question [solved]socket service interruption,event trigger??

eric19740521

Member
Licensed User
Longtime User
B4J socket service interruption,event trigger?? AStreams_Error or AStreams_Terminated,
but B4J doesn't...

Same experiment, I'm sure B4A has,it will execute the event (AStreams_Terminated,)

Maybe I is wrong. But I checked..

B4A and B4J are using the same code... so I can only help

B4J V9.3
JNetwork V1.2
jRandomAccessFile V2.34

B4A V11.2
Network V1.52
RandomAccessFile V2.33


B4J socket:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
 
    Private server As ServerSocket
    Private client As Socket
    Private astream As AsyncStreams
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
 
    '服務器 初始化.並寫聆聽.....如果是單純的client,可以不加入server功能
    server.Initialize(51042, "server")
 
    'GetMyIP=server.GetMyIP
    Try
        server.Listen
    Catch
        'WifiStatus = "Error listening: " & LastException
        Log("server 出錯了???"& LastException)
    End Try
End Sub

Sub Button1_Click
    'xui.MsgboxAsync("Hello World!", "B4X")
 
    client.Initialize("client")
    client.Connect("192.168.1.180",51042,50000)
End Sub



'server accepted client
Private Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("server_NewConnection==>")
 
    If Successful Then
        client = NewSocket
  
        astream.InitializePrefix( client.InputStream, False,  client.OutputStream, "astreams")
  
   
        Log("socket1_Connected ok")
   
    Else
   
        Log("server_NewConnection連線失敗: " & LastException)
    End If
 
 
    server.Listen    '處裡完使用者的連線.要啟動聆聽
End Sub



Sub client_connected(Successful As Boolean)
    Log("socket1_Connected==>")

    If Successful Then
     
        astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astreams")
  
        Log("socket1_Connected ok")

    Else
  
        Log("socket1_Connected fail: " & LastException)
    End If
 
 
End Sub


Sub AStreams_Error
    'ToastMessageShow(LastException.Message, True)
    Log("AStreams_Error")
 
 
End Sub

Sub AStreams_Terminated
    Log("AStreams_Terminated中斷了")
End Sub

B4A socket:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #BridgeLogger: 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.
    Private xui As XUI
 
    Private server As ServerSocket
    Private client As Socket
    Private astream As AsyncStreams
 
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
 
    '服務器 初始化.並寫聆聽.....如果是單純的client,可以不加入server功能
    server.Initialize(51042, "server")
 
    'GetMyIP=server.GetMyIP
    Try
        server.Listen
    Catch
        'WifiStatus = "Error listening: " & LastException
        Log("server 出錯了???"& LastException)
    End Try
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
 
    client.Initialize("client")
    client.Connect("192.168.1.162",51042,50000)
End Sub


'server accepted client
Private Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("server_NewConnection==>")
 
    If Successful Then
        client = NewSocket
  
        astream.InitializePrefix( client.InputStream, False,  client.OutputStream, "astreams")
  
   
        Log("socket1_Connected ok")
   
    Else
   
        Log("server_NewConnection連線失敗: " & LastException)
    End If
 
 
    server.Listen    '處裡完使用者的連線.要啟動聆聽
End Sub



Sub client_connected(Successful As Boolean)
    Log("socket1_Connected==>")

    If Successful Then
     
        astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astreams")
  
        Log("socket1_Connected ok")

    Else
  
        Log("socket1_Connected fail: " & LastException)
    End If
 
 
End Sub


'錯誤時 觸發
Sub astreams_Error
    Log("Astream_Error==>")
    Log("Error: " & LastException)
 
    'astream.Close
End Sub

'連線中斷時 觸發
Sub astreams_Terminated
    Log("Astream_Terminated==>")
 
    'astream.Close
 
End Sub
 

Attachments

  • 2.png
    2.png
    92.4 KB · Views: 107
  • 1.png
    1.png
    114.1 KB · Views: 107
Last edited:

eric19740521

Member
Licensed User
Longtime User
I searched the forums. Understand that this problem exists.

If I want to solve this problem, I need to practice the heartbeat mechanism by myself

The description below, from the article on the Internet (link )

For example, if some communication software is not used for a long time, if you want to know whether its status is online or offline, you need heartbeat packets, and send and receive packets regularly. Contractor: It can be the client or the server, whichever is convenient and reasonable to implement, usually the client. The server can also send heartbeats periodically. Generally speaking, for the sake of efficiency, the client actively sends packets to the server, rather than the server sending packets to the client. The client sends a packet every once in a while, using TCP, using send, using UDP, and using sendto. After the server receives it, it knows that the current client is still in the "alive" state. Otherwise, if such a packet is not received after a certain period of time, the server considers that the client has been disconnected and performs the corresponding client disconnection. logical processing.

I hope everyone has to help
 
Upvote 0
Top