How to start appium on two ports programmatically?


I was using appium in test programmatically for single device.

I want to test multiple devices so i need to start appium programmatically on two ports.

But i don’t know how to get port and how to start appium on multiple ports by code.

I tried it by starting appium manually on two ports and TestNG file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="ParallelDemo" verbose="1" parallel="tests" thread-count="2">
  <test name="Samsung J7">
    <parameter name="deviceName" value="SamsungJ7"></parameter>
    <parameter name="udid" value="330033acecf394bd"></parameter>
    <parameter name="platformVersion" value="8.1"></parameter>
    <parameter name="url" value="//"></parameter>
      <class name="application.basics"/>
  </test> <!-- Default test -->
  <test name="Samsung A10">
    <parameter name="deviceName" value="SamsungA10"></parameter>
    <parameter name="udid" value="RZ8M422FZJA"></parameter>
    <parameter name="platformVersion" value="9"></parameter>
    <parameter name="url" value="//"></parameter>
      <class name="application.basics"/>

</suite> <!-- Default suite -->
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

AppiumServiceBuilder builder = new AppiumServiceBuilder();
AppiumDriverLocalService server = AppiumDriverLocalService.buildService(builder);

@cesarv : Thanks for reply. but i mentioned already that i do know code for single device but not for multiple device when im passing parameters from TestNG.xml file.