简体   繁体   English

Java服务器与Matlab客户端之间的通信

[英]Communication between java server and matlab client

I'd like to establish a server(Java)/client (Matlab) communication using socket. 我想使用套接字建立服务器(Java)/客户端(Matlab)通信。 They can send messages to each other. 他们可以互相发送消息。 An example shows how to do this in Java server and Java client, http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html . 示例显示了如何在Java服务器和Java客户端http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html中执行此操作。

When I try to rewrite the client part in Matlab, I only can get the first message that the Java server sends and display it in the Matlab command window. 当我尝试在Matlab中重写客户端部分时,我只能得到Java服务器发送的第一条消息并将其显示在Matlab命令窗口中。

When I type a message in the Matlab command window, I can't pass it to the Java Server. 在Matlab命令窗口中键入消息时,无法将其传递给Java Server。

Jave code: Jave代码:

kkSocket = new Socket("localhost", 3434);

Matlab equivalent: 相当于Matlab:

kkSocket = Socket('localhost', 3434);

Java code for client: 客户端的Java代码:

    out = new PrintWriter(kkSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); 

What would be a Matlab equivalent for this? 与此等效的Matlab是什么? Thanks in advance. 提前致谢。

For the input stream: 对于输入流:

input_stream   = input_socket.getInputStream;
d_input_stream = DataInputStream(input_stream);

For the output stream: 对于输出流:

output_stream   = output_socket.getOutputStream;
d_output_stream = DataOutputStream(output_stream);

If you are trying to use MATLAB and the Java application on the same machine then matlabcontrol may do everything that you are looking for. 如果您尝试在同一台计算机上使用MATLAB和Java应用程序,则matlabcontrol可能会完成您要查找的所有操作。 It automatically establishes a connection to a session of MATLAB. 它会自动建立与MATLAB会话的连接。 It uses Java's Remote Method Invocation under the hood which makes use of sockets. 它在使用套接字的后台使用Java的Remote Method Invocation matlabcontrol is designed specifically to only enable communication on localhost; matlabcontrol专为仅在本地主机上启用通信而设计; the sockets it creates will not accept remote connections due to the security issues that could allow. 由于所允许的安全性问题,它创建的套接字将不接受远程连接。 However, if you need to allow remote connections you may find parts of matlabcontrol's source code to be useful. 但是,如果需要允许远程连接,则可能会发现matlabcontrol的部分源代码很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM