2

如果我运行以下 java 类,则不会出现异常或错误消息,但我确定无法建立连接(GrailsApplication1 未运行,并且“url”未指向任何 bean):

public class ConnectNow {

private void connect() {
    ClientDolphin dClient = new ClientDolphin();

    dClient.setClientModelStore(new ClientModelStore(dClient));
    String url = "http://localhost:8080/GrailsApplication1/";
    ClientConnector connector = new HttpClientConnector(dClient, url);
    connector.setCodec(new JsonCodec());
    dClient.setClientConnector(connector);
    dClient.send("Hi from client");
}

public static void main(String[] args) {
    ConnectNow cn = new ConnectNow();
    cn.connect();
    System.out.println("End");
}

}

Soo,我如何识别是否建立了连接?我是客户端/服务器开发的新手,所以也许我遗漏了一些明显的东西。

感谢您的任何建议或文档参考!

4

1 回答 1

1

现在,我想通了...关键是我必须将代码作为应用程序运行,在本例中为 javafx 应用程序。然后,如果您尝试传输某些内容(dClient 发送“Hi... - 在这种情况下),应用程序会尝试发送一条消息,并且您会得到ConnectException: Connection refused,如果没有运行在url.

在我看来,这似乎是开放海豚服务器可访问性测试的最短版本:

import javafx.application.Application;
import javafx.stage.Stage;
import org.opendolphin.core.client.ClientDolphin;
import org.opendolphin.core.client.ClientModelStore;
import org.opendolphin.core.client.comm.ClientConnector;
import org.opendolphin.core.client.comm.HttpClientConnector;
import org.opendolphin.core.comm.JsonCodec;

/**
 *
 * @author Václav Hanton
 */
public class ConnectNow extends Application {

    private void connect() {
        ClientDolphin dClient = new ClientDolphin();

        ClientModelStore mStore = new ClientModelStore(dClient);
        dClient.setClientModelStore(mStore);
        String url = "http://localhost:8080/GrailsApplication1/";
        ClientConnector connector = new HttpClientConnector(dClient, url);
        connector.setCodec(new JsonCodec());

        dClient.setClientConnector(connector);
        dClient.send("Hi from client");
    }

    public static void main(String[] args) {
        ConnectNow cn = new ConnectNow();
        cn.connect();
        Application.launch();
    }

    @Override
    public void start(Stage stage) throws Exception {
        System.out.println("Started");
    }
}
于 2013-07-27T22:43:59.733 回答