Cannot connect to Host Exception while running Appium on remote server

I have appium installed on remote server. Tests are defined on local machine and I am trying to run those tests on Appium installed on server. But I am getting back connection failed exception. I am starting appium programmatically from Java code.

public  void startAppiumServer() throws IOException, InterruptedException
CommandLine command = new CommandLine("/Applications/");
        command.addArgument("/Applications/", false);
        command.addArgument("--address", false);
        command.addArgument("--port", false);
        command.addArgument("--no-reset", false);
        command.addArgument("--native-instruments-lib", false);
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        DefaultExecutor executor = new DefaultExecutor();
        executor.execute(command, resultHandler);

This is the code to start the driver,

public void startAppiumDriver() throws Exception {
        File appDir = new File(this.root_directory);
        File app = new File(appDir, this.application_name);

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("app", app.getAbsolutePath());

        String url = "http://"+this.server_ip+":"+this.port+"/wd/hub";

        driver = new IOSDriver(new URL(url),capabilities);
        wait = new WebDriverWait(driver, this.driver_timeout);

This works perfectly when I run this locally, ie when I start appium on my local machine. Issue is when I start appium on remote server via code.

Also if I manually start appium UI and click the launch button on server, and I simply connect to the same port where appium is running then connection works perfectly. Issue is with programatically starting appium on remote server.

That’s not how computers and operating systems work normally. Your Java process runs locally, so it can not spawn a process on a remote machine. If computers did this normally, there would be massive security issues all across the world.

When you enter your local machine’s IP address, everything is fine because Appium will start on your local machine and bind to your local’s IP address.

When you enter your remote machine’s IP address, nothing is fine because Appium will spawn locally and try to bind to an IP address that does not belong to the machine Appium is running on; Appium will fail in this case and then terminate. When your test then tries to connect to the remote IP, it will try to form a connection with your remote machine rather than Appium, which would fail if there is no Appium process listening on the specified remote machine’s socket.

I’ve thought it over, and you really only have a few realistic, practical options for your set-up if you want to run remotely:

  1. Start Appium manually on your remote machine, or create and install an init script so that Appium starts at boot. Connect to Appium remotely without trying to start Appium programmatically. You can’t easily programmatically start and stop Appium and run remotely at the same time.
  2. Similar to option 1, but use Selenium Grid.

I agree with @afwang, you can not trigger process on remote machine using appium service builder. But I guess u can do with SSH key exchange and later you can invoke appium server. But in this case you may face permission related errors if remote user does not have permissions. I think you should not introduce such complications to your automation.