Java:如何与机器通信?

发布于 2024-11-09 07:16:23 字数 1515 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま柒月 2024-11-16 07:16:23

我不知道您正在与哪种设备交谈,但如果您通常使用 telnet 向其输入命令,那么您可能会向其发送换行符,并且可能需要这些换行符作为命令终结者。因此,也许

dataOutputStream.writeUTF("}Rv!\n");

or

dataOutputStream.writeUTF("}Rv!\r\n");

(以及取消对 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 perhaps

dataOutputStream.writeUTF("}Rv!\n");

or

dataOutputStream.writeUTF("}Rv!\r\n");

(along with uncommenting that call to flush()) would work better.

梦幻之岛 2024-11-16 07:16:23

您真正想向设备发送什么? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文