我正在研究通过TCP连接与客户端 - 服务器套接字连接Matlab的JADE(Java)项目。在这里,JADE创建了一个服务器套接字,Matlab创建了一个客户端套接字。我正在从Matlab到Java(JADE)检索一些数据。以下是我的代码,我通过代理由JADE调用Matlab。 (1)问题是如果不重新启动程序,我无法重新运行它。我相信我需要一个具有多线程Matlab实例的多线程java实例,它可以相互连接和同步。但是,我发现Matlab是一个单线程。该程序抛出绑定错误。
WARNING: Error adding ICP jade.imtp.leap.JICP.JICPPeer@1dbb27d[Cannot bind server socket to localhost port 1099].
jade.core.AgentContainerImpl joinPlatform
SEVERE: Communication failure while joining agent platform: No ICP active
jade.core.IMTPException: No ICP active
我想多次运行它而无需手动重新启动。这是我的JADE代码(从https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html获得帮助):
public class MatlabComAgent extends Agent
{
ServerSocket srvr = null;
Socket skt = null;
BufferedReader in;
PrintWriter out;
String ip = "localhost";
String filePath;
int port = 1234;
protected void setup()
{
// Get arguments
Object[] args = getArguments();
filePath = (String) args[0];
// Create the TCP connection
try
{
// Create server and socket
srvr = new ServerSocket(port);
skt = srvr.accept();
// Create writer and reader to send and receive data
out = new PrintWriter(skt.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
}
catch (IOException e)
{
e.printStackTrace();
}
// Send a message to the tester to say its can start sending requests
sendMessage("Tester","","start-now",ACLMessage.INFORM);
// Run behavior
CommWithMatlab commWithMatlab = new CommWithMatlab();
addBehaviour(commWithMatlab);
} // End setup
Matlab连接代码:
% Create TCP/IP object 't'. Specify server machine and port number.
% Open the connection with the server
t = tcpip('localhost', 1234);
set(t, 'InputBufferSize', 30000);
set(t, 'OutputBufferSize', 30000);
pause(0.1)
fopen(t);
disp('Connection with JADE established')
我在“套接字服务器上找到了有趣的注释,允许通过线程和Java”Creating a socket server which allows multiple connections via threads and Java页面进行多个连接,但是,我无法完全按照这里所说的那样做。可能是我在这里遗漏了一些东西。 (2)如果我编辑用于多线程的Matlab代码和/或JADE代码,我会感到困惑。
这是我尝试过的代码:
protected void setup()
{
// Get arguments
Object[] args = getArguments();
filePath = (String) args[0];
// Create the TCP connection
try
{
srvr = new ServerSocket(port);
Runnable connectionHandler = new ConnectionHandler(skt);
new Thread(connectionHandler).start();
}
catch (IOException e)
{
e.printStackTrace();
}
这是新的ConnectionHandler类:
public class ConnectionHandler implements Runnable {
private Socket sk=null; //initialize in const'r
BufferedReader in;
PrintWriter out;
public ConnectionHandler(ServerSocket skt) throws IOException
{
sk = skt.accept();
out = new PrintWriter(sk.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
}
public void run() {
try
{
// Create writer and reader to send and receive data
out = new PrintWriter(sk.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
但是我得到了一些erorr“java.lang.NullPointerException”。有人可以帮我正确编码,我缺少什么。此外,(3) ConnectionHandler类中的此run()将自动调用?我很困惑所以我在Connectionhandler类和它的run()中创建了writer和reader。我可以简单地将我的MatlabComAgent类作为多线程而不添加任何新类。我可以把我的班级作为
public class MatlabComAgent extends Agent implements Runnable
{....
....
}
我是否还应将以下内容放在ConnectionHandler类中?
// Send a message to the tester to say its can start sending requests
sendMessage("Tester","","start-now",ACLMessage.INFORM);
// Run behavior
CommWithMatlab commWithMatlab = new CommWithMatlab();
addBehaviour(commWithMatlab);
这里,CommWithMatlab类扩展了SimpleBehavior,其中包含进一步将命令从Matlab传递到PowerWorld(使用另一个连接)所需的操作。一个例子是:
class CommWithMatlab extends SimpleBehaviour
{
private static final long serialVersionUID = 8966535884137111965L;
@Override
public void action()
{
// Wait for a message from another agent requesting something
ACLMessage msg = blockingReceive();
// If this is to open a case
if(msg.getConversationId().equals(OPEN_CASE))
{
openCase(msg.getContent());
}
}
我可以简单地将参数传递给addagent()并调用runJade()。以下是我使用代理的JADE运行函数:
//Runs JADE and starts the initial agents
public static void runJade() throws ControllerException
{
// Launch JADE platform
Runtime rt = Runtime.instance();
Profile p;
p = new ProfileImpl();
cController = rt.createMainContainer(p);
rt.setCloseVM(true);
// Launch Powerworld interface agent
addAgent(PWRWORLD_NAME, PWRWORLD_CLASS, null);
addAgent(PWRWORLD_TESTER_NAME, PWRWORLD_TESTER_CLASS, null);
//addAgent(PWRWORLD_TESTER_NAME2, PWRWORLD_TESTER_CLASS2, null);
}
private static void addAgent(String name, String type, String arg) throws ControllerException
{
Object[] argsObj = {arg};
AgentController ac = cController.createNewAgent(name, type, argsObj);
ac.start();
}
(4)我有一个不同的程序也可以创建相同的连接。当我尝试在其他程序运行时运行一个程序时,它再次抛出绑定错误。但是,这些程序是完全独立的。一个程序使用端口1234和其他1239.但是,系统总是将本地端口分配给1099到两个程序,因此在这种情况下抛出绑定错误。 任何帮助都很明显!
答案 0 :(得分:0)
不幸的是,无法在分布式网络上使用matlabcontrol。我查了一下。