Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

sockets - Writing Java's pw.println(), etc. in 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 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

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Server.java

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...