如何修复:Anylogic 无法通过 Socket 连接到 Eclipse [英] How to fix: Anylogic does not connect to Eclipse over Socket

查看:83
本文介绍了如何修复:Anylogic 无法通过 Socket 连接到 Eclipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Anylogic 中的 Mojave 在我的 Macbook 上创建一个场景,这是使用许多不同工具的基于代理的模拟的一部分.我的想法是通过 Java 接口将 Anylogic 连接到 Eclipse.主要问题是,Anylogic 不知何故没有响应.

我尝试了许多不同的套接字代码,但找不到一个适用于 Anylogic 的代码.我正在使用 Anylogic 的免费版本并在我的主项目下创建了一个 Java 接口.要运行 Java 界面,请右键单击该文件并选择使用 Java 编辑器运行"

相比之下,我在 Eclipse 中创建了两个文件,一个是服务器,一个是客户端,它工作正常.

所以这是我的 Eclipse,我想应该没问题

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.

解决方案

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();

这篇关于如何修复:Anylogic 无法通过 Socket 连接到 Eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆