0
$\begingroup$

Possible Duplicate:
Failed to connect remote Mathematica kernel using J/Link

I'm trying to connect to a remote Mathematica 8 kernel on windows server (IP :172.20.48.113 ) from a windows client machine. I wrote the following code where KernelLink gets created but there is some problem with establishing the connection. It gives an error "MathLinkException: 1: MathLink connection was lost."

public static void main(String[] args) {
        KernelLink ml = null;
        try {
            ml = MathLinkFactory.createKernelLink("-linkmode connect -linkprotocol tcp -linkname 1234@172.20.48.113");
        } catch (MathLinkException ex) {
            Logger.getLogger(MathematicaSample.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
        try {
            ml.connect(1000000); // Wait at most 10 seconds
        } catch (MathLinkException e) {
            // If the timeout expires, a MathLinkException will be thrown.
            e.printStackTrace();
            ml.close();
            return; // Or whatever is appropriate.
        }    
        try {
            // Get rid of the initial InputNamePacket the kernel will send
            // when it is launched.
            ml.discardAnswer();
            ml.evaluate("<<MyPackage.m");
            ml.discardAnswer();
            ml.evaluate("2+2");
            ml.waitForAnswer();
            int result = ml.getInteger();
            System.out.println("2 + 2 = " + result);            
        } catch (MathLinkException e) {
            System.out.println("MathLinkException occurred: " + e.getMessage());
        } finally {
            ml.close();
        }
    }
}

Does anyone know how to connect to the kernel? I'm really struggling with it.

$\endgroup$
  • 2
    $\begingroup$ Perhaps you'd better add this to your previous question instead of asking it a second time with a bit more details. $\endgroup$ – Sjoerd C. de Vries Nov 25 '12 at 13:30
  • $\begingroup$ As Sjoerd said, it's better to add related updates to your previous question instead of posting a new one. I've edited it in this time for you, but in future, please remember this :) $\endgroup$ – rm -rf Nov 26 '12 at 2:29