iOS Question How to properly close and reopen a ServerSocket

jefflynn1974

Member
Licensed User
Longtime User
I'm just asking because the obvious solution would be
B4X:
ServerSocket1.Initialize(ServerPort,"ServerSocket1")
ServerSocket1.Listen
.
.
.
ServerSocket1.Close
.
. 'do some other stuff
.
ServerSocket1.Initialize(ServerPort,"ServerSocket1") 'is this line necessary here? Anyway, doesn't work either
ServerSocket1.Listen
but this doesn't work. After the second ServerSocket1.Listen, the socket cannot be connected to.
 

jefflynn1974

Member
Licensed User
Longtime User
I tried to simplify my question, I might have simplified it to the extend that I missed the point. Now I'll explain it in detail. I'm converting an Android application to iOS, which uses ServerSocket, and a Windows client program connects to it. The problem is the following:
0. I start the app on my iPad
1. the PC client connects to it
2. Do some stuff
3. I press the home button on iPad (app goes to background)
4. I let the device sleep (or I press the power button - same effect)
5. I wake up the device and start the app again
6. the PC client won't connect , and I'm stuck here

I think I have tried everything but nothing helps. I tried closing the socket in B4XPage_Background and reopening in B4XPage_Foreground. I have tried several combinations of closing and repoening the socket and issuing ServerSocket1.Listen at different points of my app, nothing works. It seems that once the socket has been closed, or the app has gone to background (the os may close the socket in this case?) it cannot be opened again.
 
Upvote 0

jefflynn1974

Member
Licensed User
Longtime User
Yes, it works. I tried to use it as a template but it is way too complicated.
Why do you think the following example doesn't work? It is working on the first start, but after sending it to background and starting it again it fails.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    
    Private Page1 As Page

    Dim hd As HUD
    
    Dim ServerSocket1 As ServerSocket
    Dim AStreams As AsyncStreamsText

    Dim ServerPort=51041 As Int
End Sub

Sub ToastMessageShow(message As String, longTime As Boolean)
    hd.ToastMessageShow(message,longTime)
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)

    ServerSocket1.Initialize(ServerPort,"ServerSocket1")
    ServerSocket1.Listen
End Sub

Sub Application_Foreground
    If ServerSocket1.IsInitialized=False Then
        ServerSocket1.Initialize(ServerPort,"ServerSocket1")
        ServerSocket1.Listen
    End If
End Sub

Sub Application_Background
    AStreams.Close
    ServerSocket1.Close
End Sub

Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        ToastMessageShow("ServerSocket1_NewConnection", False)

        AStreams.Initialize(Me, "AStreams", NewSocket.InputStream, NewSocket.OutputStream)
        AStreams.Write("Hello" & Chr(13) & Chr(10))
    Else
        ToastMessageShow("ServerSocket1_NewConnection failed", False)
    End If

End Sub

Sub AStreams_Terminated
    ToastMessageShow("AStreams_Terminated", False)
    
    AStreams.Close
    ServerSocket1.Listen
End Sub
 
Upvote 0

Similar Threads

Top