简体   繁体   中英

Communication between java server and matlab client

I'd like to establish a server(Java)/client (Matlab) communication using socket. 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 .

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.

When I type a message in the Matlab command window, I can't pass it to the Java Server.

Jave code:

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

Matlab equivalent:

kkSocket = Socket('localhost', 3434);

Java code for client:

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

What would be a Matlab equivalent for this? 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. It automatically establishes a connection to a session of MATLAB. It uses Java's Remote Method Invocation under the hood which makes use of sockets. matlabcontrol is designed specifically to only enable communication on localhost; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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