Java:如何与机器通信?
我正在尝试通过 java 与我的设备进行通信。 我可以使用 telnet 与它进行通信,我知道因为我使用 PuTTY,所以我的配置是:
ip: 192.168.1.4 端口: 2001 通信类型:telnet
这有效,我的设备和网络工作正常。
所以我认为我可以通过java做同样的事情,然后我创建了这个类:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author Valter
*/
public class Middleware {
public static void main(String args[]) {
try {
Socket socket = new Socket("192.168.1.4", 2001);
// create a channel between to receive data
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
// create a channel between to send data
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF("}Rv!");
// dataOutputStream.flush();
String answer= dataInputStream.readUTF();
System.out.println("Answer:"+answer);
dataInputStream.close();
dataOutputStream.close();
socket.close();
} catch (UnknownHostException ex) {
System.out.println("EXCEÇÃO UNKNOW HOST EXCEPTION");
} catch (IOException ex) {
System.out.println("EXCEÇÃO IOEXCEPTION");
System.out.println(ex.getMessage());
}
}
}
但是当我尝试执行这个类时,什么也没有发生,没有异常,什么也没有。 我看起来就像是没有尽头的“一会儿”。
我应该在这里做什么? 我应该在这里使用客户端 telnet 到 java 吗?
i'm trying to communicate with my device through java.
I can communicate with it using telnet, i know that because i use PuTTY, so my configuration is:
ip: 192.168.1.4 port: 2001 communication type: telnet
This works, my device and network is working fine.
So i though that i could do the same through java, then i create this class:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author Valter
*/
public class Middleware {
public static void main(String args[]) {
try {
Socket socket = new Socket("192.168.1.4", 2001);
// create a channel between to receive data
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
// create a channel between to send data
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF("}Rv!");
// dataOutputStream.flush();
String answer= dataInputStream.readUTF();
System.out.println("Answer:"+answer);
dataInputStream.close();
dataOutputStream.close();
socket.close();
} catch (UnknownHostException ex) {
System.out.println("EXCEÇÃO UNKNOW HOST EXCEPTION");
} catch (IOException ex) {
System.out.println("EXCEÇÃO IOEXCEPTION");
System.out.println(ex.getMessage());
}
}
}
But when i try to execute this, nothing happens, no exceptions, no nothing.
I looks like a 'while' without end.
What should i do here?
I should use a client telnet to java here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道您正在与哪种设备交谈,但如果您通常使用 telnet 向其输入命令,那么您可能会向其发送换行符,并且可能需要这些换行符作为命令终结者。因此,也许
or
(以及取消对
flush()
调用的注释)会更好。I don't know what sort of device you're talking to, but if you normally type commands to it using
telnet
, you're presumably sending newlines to it, and perhaps those newlines are needed as command terminators. So perhapsor
(along with uncommenting that call to
flush()
) would work better.您真正想向设备发送什么? DataOutputStream#writeUTF 使用 Java 特定的字符串编码,我怀疑这是否是设备真正期望的。
除此之外,如果设备确实正确实现了 telnet 协议(而不仅仅是可以使用 telnet 客户端访问的协议),则必须使用 Java telnet 库来支持控制序列或自己实现。您不能像示例代码中那样只读取和写入套接字。
What do you really want to send to the device? DataOutputStream#writeUTF uses a Java specific string encoding and I doubt that this is what the device really expects.
Except for that, if the device is really implementing the telnet protocol properly (and not just something which can be accessed with a telnet client), you have to either use a Java telnet library to support control sequences or implement this yourself. You can't just read and write to the socket as in your example code.