我在MATLAB中有一个程序需要通过套接字获取CSV文件。我试图让它真正接受任何数据都无济于事。 MATLAB文档说在打开TCP套接字之后,你可以调用fscanf(socket)来获取数据,但这总是超时。
这是我发送文件的java代码:
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket s = new Socket("192.168.1.8", 3000);
BufferedReader r = new BufferedReader(new FileReader("./channel1.csv"));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
int bytes = 0;
while ((line = r.readLine()) != null)
{
System.out.println("Sending:" + line);
out.write(line);
bytes += line.length();
}
System.out.println("Wrote " + bytes + " bytes");
out.close();
s.close();
r.close();
}
运行此命令会打印CSV文件以及:“写入2085字节”
以下是试图收到文件的MATLAB代码:
t=tcpip('192.168.1.6',3000,'NetworkRole','server');
set(t,'InputBufferSize',10000);
fopen(t) %blocks here for connection
data = fscanf(t); %times out here
如果我使用netcat使用以下命令连接到MATLAB脚本:
nc 192.168.1.8 3000
一切正常,但是如果我使用netcat尝试发送文件:
cat channel1.csv | nc -v 192.168.1.8 3000
它与Java程序相同。
如何在MATLAB中接收ASCII文件?
答案 0 :(得分:0)
我明白了。上面的MATLAB代码工作,我在Java中添加了一行来在每次写入后刷新缓冲区并且它工作正常!所以我的Java代码如下。我不确定为什么冲洗缓冲区有帮助。它似乎是关于MATLAB一次解析多行Java的东西吗?
public static void main(String[] args) throws UnknownHostException, IOException
{
log("Hello World!");
Socket s = new Socket("192.168.1.8", 3000);
BufferedReader r = new BufferedReader(new FileReader("./channel1.csv"));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
int bytes = 0;
int i = 0;
while (true)
{
line = ++i + "," + Math.sin(Math.toRadians((double) i));
System.out.println("Sending:" + line);
out.write(line + "\n");
out.flush();
bytes += line.length(
}
}
感谢Andrew Janke的帮助!