Domanda

I know how to access samba file by jcifs, and use it to implement list file, upload, download.....
But I want to search samba server in my LAN automatic.
How can I do that? Please help me . Thanks a lot

È stato utile?

Soluzione

You can access LAN ip and check is it server.like 192.168.0.0 to 192.168.0.255. if you check ip in use then you can check it's port. Samba port is 445 so that you can check is 192.168.0.0:455 available, if it is available so that it's a samba server. When you finish check 0 ~ 255, that you can search samba server in LAN.

Altri suggerimenti

to complicate :-/ here is the better solution :

private class NetworkUsers  extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        // your code, thats runs before "doInBackground" action
    }

    @Override
    protected void onPostExecute(String result) {
        // your code, thats runs after "doInBackground" action

        if(result != null){ 
            // handle "result" String
        } else {

        }
    }

    @Override
    protected String doInBackground(String... params) {
        SmbFile[] domains = null;
        String result = null;
        String url = params[0];
        SmbFile path = null;
        try {
            path = new SmbFile(url);
            try {
                if(path.exists()){
                    result = ""; // set result variable to empty
                    domains = (new SmbFile(url)).listFiles();
                    for (int i = 0; i < domains.length; i++) {
                        // here you can add custom validations
                        // for example if(domains[i].isHidden()){} | example if(domains[i].canRead()){} , etc.
                        result +=  domains[i].toString() + "\n";
                    }   
                }
            } catch (SmbException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return result;
    }       
}

How to use :

List all network devices on the root:

(new NetworkUsers()).execute("smb://");

List device content:

(new NetworkUsers()).execute("smb://my-device/shared/"); //my-device is the network device name
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top