how can i download a file from sshd server to jsch client properly mobile to mobile

253 Views Asked by At

i have a sshd server running on my mobile without error i am trying to download a file from my mobile internal storage using jsch as client . getting this errors

** CLIENT **

    public void startClient(){

   String localFile = "/data/user/0/com.open.jsch/databases/au.au_1";
    String remoteFile = "/data/user/0/com.open.androidsshd/databases/au.au_1";

    Session jschSession = null;

    try {

    JSch jsch = new JSch();
    jsch.setKnownHosts("/home/mkyong/.ssh/known_hosts");
    jschSession = jsch.getSession(USERNAME, REMOTE_HOST, REMOTE_PORT);

    // authenticate using private key
    // jsch.addIdentity("/home/mkyong/.ssh/id_rsa");

    // authenticate using password
    jschSession.setPassword(PASSWORD);

    //Missing code
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    jschSession.setConfig(config);
    //
    System.out.println("Establishing connection");
    // 10 seconds session timeout
    jschSession.connect(SESSION_TIMEOUT);

    Channel sftp = jschSession.openChannel("sftp");

    // 5 seconds timeout
    sftp.connect(CHANNEL_TIMEOUT);

    ChannelSftp channelSftp = (ChannelSftp) sftp;

    // transfer file from local to remote server
    //channelSftp.put(localFile, remoteFile);

    // download file from remote server to local
    channelSftp.get(remoteFile, localFile);
    System.out.println("got the file");
    channelSftp.exit();

    } catch (JSchException | SftpException e) {

    e.printStackTrace();

    } finally {
    if (jschSession != null) {
        jschSession.disconnect();
    }
    }


   }

Client Error

    2022-10-30 21:55:59.228 15159-15202/com.allah.jsch W/System.err: com.jcraft.jsch.JSchException:     Session.connect: java.net.SocketException: Connection reset
    2022-10-30 21:55:59.228 15159-15202/com.allah.jsch W/System.err:     at  com.jcraft.jsch.Session.connect(Session.java:565)
    2022-10-30 21:55:59.228 15159-15202/com.allah.jsch W/System.err:     at com.allah.jsch.Client.startClient(Client.java:71)
    2022-10-30 21:55:59.228 15159-15202/com.allah.jsch W/System.err:     at com.allah.jsch.MainActivity$2.run(MainActivity.java:78)
    2022-10-30 21:55:59.229 15159-15202/com.allah.jsch W/System.err:     at java.lang.Thread.run(Thread.java:764)

SERVER

int port = 8888;
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(port);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(
    "src/test/resources/hostkey.ser"));
sshd.setSubsystemFactories(Arrays
    .<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
sshd.setShellFactory(new EchoShellFactory()); // necessary if you want to type commands over ssh
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

@Override
public boolean authenticate(String u, String p, ServerSession s) {
    return ("sftptest".equals(u) && "sftptest".equals(p));


}


});


try {
sshd.start();
} catch (IOException e) {
e.printStackTrace();
}

for client side testing. i am running on my android studio emulator .for server i am running on mobile. for the first time i run the code there is a lots of error but i fixed it by adding dependencies and internet permission whatever i can find it online now the errors i cant fixit there is no answers on the google or stack i an understanding i need help

**permission **

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

**gradle on client **

// https://mvnrepository.com/artifact/org.apache.sshd/sshd-core
implementation group: 'org.apache.sshd', name: 'sshd-core', version: '0.6.0'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.2'

// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk16
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
    implementation files('libs\\jsch-0.1.55.jar')

i am expecting to download a file from SSHD server to JSCH client/ SSHD Client . i think i am missing some code on server side i need some help with this . i know you guys are very busy please a humble request give me a correct working solution thanks for advance

1

There are 1 best solutions below

13
blackapps On

To make a long story short:

Even if you would manage to determine the public internet ip of your Android device that ip would be useless as most mobile providers block incoming connections.

Hence it makes no sense trying your mobile phone to act as a server.

Only possible on wifi.