在Matlab中编写Java代码? [英] Writing Java code in Matlab?

查看:133
本文介绍了在Matlab中编写Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Matlab中使用Java命令pw.println()和br.readLine(),因为我在Matlab和我想要使用Java类控制的命令行程序之间设置了一个套接字(input_socket2) BufferedReader和PrintWriter。在下面的代码片段之前,我在两台计算机之间实现了另一个套接字。这很好用,我也知道下面的代码片段成功地打开了Matlab和其他程序之间的通信线路。但是,Matlab在pw.println('noop')处抛出错误。我认为这与语法有关,但我不确定如何用Matlab语法编写命令:

I'm trying to use the Java commands pw.println() and br.readLine() in Matlab because I have set up a socket (input_socket2) between Matlab and a command-line program I want to control using Java classes BufferedReader and PrintWriter. Before the following snippet of code, I implemented another socket that goes between 2 computers. This works great and I also know that the following snippet of code successfully opens up a communication line between Matlab and the other program. However, Matlab throws an error at pw.println('noop'). I think it has something to do with syntax, but I'm not sure how to write the command in Matlab syntax then:

try
    input_socket2 = Socket(host2,port2);
    input_stream2   = input_socket2.getInputStream;
    d_input_stream2 = DataInputStream(input_stream2);
    br = BufferedReader(InputStreamReader(input_stream2));
    pw = PrintWriter(input_socket2.getOutputStream,true);
    pw.println('noop')
    br.read
end

任何想法?

推荐答案

由于您未提供实际错误,因此很难确定问题。

Since you didn't provide the actual error, it is difficult to pinpoint the problem.

无论如何,这是一个简单的实现来展示这个概念(经过测试和正常工作!):<​​/ p>

服务器。 java



Anyways, here's a simple implementation to show the concept (tested and working just fine!):

import java.net.*;
import java.io.*;

public class Server
{
    public static void main(String[] args) throws IOException
    {
        System.out.println("Listening on port...");
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
            System.out.println("Received connection!");
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()) );
        String inputLine;

        while ( (inputLine = in.readLine()) != null ) {
            System.out.println("Client says: " + inputLine);
            out.println(inputLine);
        }

        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}



Client.m



Client.m

import java.io.*;
import java.net.*;

%# connect to server
try
    sock = Socket('localhost',4444);
    in = BufferedReader(InputStreamReader(sock.getInputStream));
    out = PrintWriter(sock.getOutputStream,true);
catch ME
    error(ME.identifier, 'Connection Error: %s', ME.message)
end

%# get input from user, and send to server
userInput = input('? ', 's');
out.println(userInput);

%# get response from server
str = in.readLine();
disp(['Server says: ' char(str)])

%# cleanup
out.close();
in.close();
sock.close();

这篇关于在Matlab中编写Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆