Android Question JCSF-NG smb AND jcifs.smb.SmbException: No more connections...

MrKim

Well-Known Member
Licensed User
Longtime User
Edit: I tried copying the files but same result. After 20 files i run out of connections.
I am trying to use the jcsf-ng SMB lib to stream a number of small files from the windows share. But I am getting
B4X:
jcifs.smb.SmbException: No more connections can be made to this remote computer at this time because there are already as many connections as the computer can accept.
After about 20 copies.
I am closing the connection after each on is finished:
Class Code:
B4X:
Sub Class_Globals
    Public smbClient As SMBClient
    Public glRes As SMBResource
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(cred As SMBCred)
    smbClient.Initialize("SMBClient", cred.Domain, cred.Username, cred.Password, cred.Share)
End Sub

Sub SMBClient_CopyProgress(totalBytes As Long, path As String, filename As String)
    Log($"SMBClient_CopyProgress(${totalBytes} : ${path}:${filename})"$)
End Sub

Sub SMBClient_Resource(success As Boolean, smbobjres As Object, smbobj As Object, info As String)
    Log($"SMBClient_Resource(${success},${info},${smbobjres},${smbobj})"$)
    If smbobjres <> Null And smbobj <> Null And info = "OK" Then
        glRes = smbobjres

        Dim InputStream1 As InputStream = glRes.openInputStream
        Dim Bmp As Bitmap
        Bmp.Initialize2(InputStream1)
        CallSub2(actSMBFiles, "SetImg", Bmp)
        InputStream1.Close
        glRes.close
    End If
End Sub
Activity Code:
B4X:
    Dim x As Int
    Dim Copy As SMBCopyHERE
    For x = 0 To lvFiles.Size - 1
        Main.credentials.Share = lvFiles.GetItem(x).As(SMBFile).CanonicalPath
        ToastMessageShow(lvFiles.GetItem(x).As(SMBFile).length, False)
    '    Main.credentials.Share = Value
        Copy.Initialize(Main.credentials)
        Sleep(30) 'allow the picture to display
    Next
but I notice this comment on the close command for the SMBResource object.
1744202943637.png

but I can find no reference to setting a 'strict' mode.
Any help appreciated.
 
Last edited:
Solution
This is the solution. You need to always check if the connection is already initialized and if it is just reuse it:
B4X:
If GlblSMBResource.IsInitialized Then
    #If Debug
        Log("smbResource is initialized. Proceeding...")
    #End If
    smbClient.GetResourceFromUrl(SMB2Cred.Share)
Else
    #If Debug
        Log("smbResource is not initialized. Reinitializing smbClient...")
    #End If
    smbClient.Initialize("SMBClient", SMB2Cred.Domain, SMB2Cred.Username, SMB2Cred.Password, SMB2Cred.Share)
End If
Sub SMBClient_Resource gets called exactly the same.
It also solved a speed problem I was having getting multiple files.

MrKim

Well-Known Member
Licensed User
Longtime User
This is the solution. You need to always check if the connection is already initialized and if it is just reuse it:
B4X:
If GlblSMBResource.IsInitialized Then
    #If Debug
        Log("smbResource is initialized. Proceeding...")
    #End If
    smbClient.GetResourceFromUrl(SMB2Cred.Share)
Else
    #If Debug
        Log("smbResource is not initialized. Reinitializing smbClient...")
    #End If
    smbClient.Initialize("SMBClient", SMB2Cred.Domain, SMB2Cred.Username, SMB2Cred.Password, SMB2Cred.Share)
End If
Sub SMBClient_Resource gets called exactly the same.
It also solved a speed problem I was having getting multiple files.
 
Upvote 0
Solution
Top