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

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

问题描述

我正在尝试使用Anylogic中的Mojave在Macbook上创建一个方案,该方案是使用许多不同工具的基于代理的仿真的一部分.我的想法是通过Java接口将Anylogic连接到Eclipse. 主要的问题是,Anylogic无法响应.

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.

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

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'

与之相比,我在Eclipse中创建了两个文件,其中一个是服务器,一个是客户端,并且可以正常工作.

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

所以这是我的Eclipse,我想应该很好 https://www.minpic .de/i/7wbk/nv00b

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

这是我在Anylogic中的主要模型 https://www.minpic.de/i/7wbn/pzuut

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

,其中包含带有服务器代码的Java接口. https://www.minpic.de/i/7wbo/1mxsl4

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.

推荐答案

Java代码本身很好,至少可以用于创建简单的连接. 对于Eclipse中的服务器端,您可以像这样保留它.

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.

但是对于AnyLogic的客户端而言,存在一个问题: 您不能像这样直接运行代码,因为其中有main方法. AnyLogic不是像Eclipse这样的普通" Java IDE,它是一个非常特殊的IDE.它会自动为您自动创建一个项目,并将运行该项目所需的所有内容放入其中,包括一个主要方法.这意味着您不需要第二种主要方法.您宁愿使您的客户端成为AnyLogic为您构建的更大程序的一部分".单击仅显示代码的使用Java编辑器打开"时,您将无法在AnyLogic中运行任何类似的代码!

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. 在AnyLogic中创建Java类(一个简单的类,没有main方法)
  2. 在类中添加一个函数以启动客户端过程(在其自身的main方法触发之前)
  3. 从类Client
  4. 创建实例
  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;

}

创建实例并激活客户端可以使用以下代码完成,例如将其添加到按钮或AnyLogic主代理的OnStartup代码中:

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天全站免登陆