在MATLAB中编写Java的pw.println()等 [英] Writing Java's pw.println(), etc. in MATLAB
问题描述
我正在尝试在MATLAB中使用Java命令pw.println()和br.readLine(),因为我已经在MATLAB和要使用Java控制的命令行程序之间设置了一个套接字(input_socket2) BufferedReader和PrintWriter类.
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.
在下面的代码片段之前,我实现了两个计算机之间的另一个套接字.这很好用,而且我也知道下面的代码片段成功地打开了MATLAB与其他程序之间的通信线路.但是,MATLAB在pw.println('noop')处引发错误.我认为这与语法有关,但是我不确定如何用MATLAB语法编写命令:
Before the following snippet of code, I implemented another socket that goes between two 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>
Server.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
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的pw.println()等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!