How to fix: Anylogic does not connect to Eclipse o

2019-08-20 03:05发布

I'm trying to create a scenario on my Macbook with Mojave in Anylogic, which is part of agent-based simulation with many different tools. My idea is to connect Anylogic via Java Interface to Eclipse. The main problem is, that Anylogic somehow does not respond.

I have tried many different codes with sockets, but could not find one, which worked for Anylogic. I'm using the free version of Anylogic and created a Java Interface under my Main Project. To run the Java Interface I press right-click on the file and select 'run with Java Editor'

In comparison to that I create two files in Eclipse, with one being the Server and one the Client and it worked.

so this is my Eclipse, which I guess should be fine https://www.minpic.de/i/7wbk/nv00b

this is my main model in Anylogic https://www.minpic.de/i/7wbn/pzuut

and there is the Java interface with the server code in it. https://www.minpic.de/i/7wbo/1mxsl4

Im pretty new to coding so hopefully you guys can help me.


public class server{
    public static void main(String[] args) throws IOException {
    ServerSocket ss = new ServerSocket(4995);
    Socket s = ss.accept();

    System.out.println("Client connected");
    DataInputStream dout = new DataInputStream(s.getInputStream());
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    while(true) {
        String yoo = dout.readUTF();
        System.out.println("client" + yoo);
        if(yoo.equalsIgnoreCase("exit"));
        break;
    }
    ss.close();

    }   
}


public class client{
    public static void main(String[] args) throws IOException {
    Socket s = new Socket("localhost",4995);
    DataOutputStream dout = new DataOutputStream(s.getOutputStream());
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    while (true)
    {
        String so= br.readLine();
        dout.writeUTF(so);
        System.out.println("client" + so);
        if(so.equalsIgnoreCase("exit"));
        break;
    }
    s.close();
        }
    }

I was expecting the consoles of both programs to show me messages I have send via the console, but neither of the programs show me the messages of what I wrote in the other program.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-20 03:21

The Java code itself is fine, at least for creating a simple connection. For the server side in Eclipse, you can leave it like that.

For the client side in AnyLogic however, there is an issue: You can't run the code directly like this, because you have a main method in there. AnyLogic is no "normal" Java IDE like Eclipse, it is a very specific IDE. It automatically creates you exactly one project and puts everything in there that is needed to run it, including one main method. This means you don't need a second main method. You rather have to make your client become "part" of the bigger programm that AnyLogic is building for you. When you clicked on "Open with Java Editor" that only showed you the code, you cannot run any code like that in AnyLogic!

Therefore we do the following steps:

  1. Create of a Java class (a simple one, without main method) Client in AnyLogic
  2. Add a function to the class to start the client procedure (before it was triggered by it's own main method)
  3. Create an instance from the Class Client

The class code, already including the function is the following:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Client implements Serializable {

    public Client() {
    }

    public void activate() {
        try {
        Socket s = new Socket("localhost",4995);
        DataOutputStream dout = new DataOutputStream(s.getOutputStream());
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        while (true)
        {
            String so= br.readLine();
            dout.writeUTF(so);
            System.out.println("client" + so);
            if(so.equalsIgnoreCase("exit"));
            break;
        }
        s.close();
        }
        catch(IOException e) {
            System.out.println(e);
        }
    }

    /**
     * This number is here for model snapshot storing purpose<br>
     * It needs to be changed when this class gets changed
     */ 
    private static final long serialVersionUID = 1L;

}

Creating the instance and activating the client can be done with this code, add it for example in a button or the OnStartup code of the AnyLogic Main Agent:

Client client = new Client();
client.activate();

Button

查看更多
登录 后发表回答