0

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.

Razil
  • 5
  • 4
  • Where exactly do you have the option "run with Java Editor"? In AnyLogic I am not aware of such a thing, you can only let the code run with the model itself. Another thing I am wondering is how exactly looks the code you put in AnyLogic? And why did you put it as an interface? I just tried around myself a little bit, it should be possible with the commands you use above... Maybe the integration with a Java class in AnyLogic is not done right...please explain a bit further about how you tried this. – Florian Jun 11 '19 at 10:13
  • Hi Florian, thanks for your response. I used the code in the Java interface because I thought it would be the right thing to pick if you want to create a connection between two programs with a socket. So in my menu, if I right-click the Java interface and go on "open with" it shows me "Java Editor". But if it's not possible to run a code by itself, I guess I will try to run it with the model. Do you have any tips on how to get a simple conversation between my two programs while my model is running? – Razil Jun 12 '19 at 20:16
  • The code itself is ok. But in AnyLogic you can't use it like that, with a separate main. You'll have to put that code in a AnyLogic Function, AnyLogic Java Class, or AnyLogic Agent. Also you might have to use Threads in AnyLogic, in order to avoid that the model stops just because you are waiting to receive a message. However these are too many things to answer in one question. – Florian Jun 12 '19 at 20:25
  • Sorry, since you do not share any information how you implemented that in AnyLogic, I cant tell why it is not working as expected. – Florian Jun 12 '19 at 20:29
  • Maybe you can share screenshots, the exact code, logs, the model source.... – Florian Jun 12 '19 at 20:30
  • Ok i will try to upload some screenshots for you – Razil Jun 12 '19 at 20:37
  • so this is my Eclipse, which I guess should be fine – Razil Jun 12 '19 at 20:38
  • this is my main model in Anylogic, which is the Warehouse tutorial from Anylogic itself – Razil Jun 12 '19 at 20:40
  • Sorry to interrupt, please put all this additional explanation directly in the question by editing it, this way everybody else can also easily see it. – Florian Jun 12 '19 at 20:41
  • and there is the Java interface with the server code in it. – Razil Jun 12 '19 at 20:41
  • Ok, as I wrote before, you cant run Java code like that in AnyLogic. It is a bit different then Eclipse. And when you click "Open with Java Editor" in AnyLogic, you can only view the code not run it. I posted an answer which does work, at least it creates a connection (message: "client connected"). – Florian Jun 12 '19 at 22:15

1 Answers1

0

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

Florian
  • 962
  • 1
  • 5
  • 17