如何使用套接字 ping IP 并通过它发送数据?

发布于 2024-11-05 06:19:01 字数 36 浏览 0 评论 0原文

如何使用套接字程序 ping IP 地址并通过它发送数据?

How can I ping an IP address using a socket program and send data through it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

蓝天 2024-11-12 06:19:01

你不能在 Java 中执行 ping - ping 在 ICMP 级别工作,它在 IP 之上工作,而 Java 提供对 UDP(位于 IP 之上)和 TCP(同样在 IP 之上)的支持。它基本上是一个不同的(更高级别)协议,您需要为其编写自己的(本机)库才能访问 IP 堆栈。

You can't do ping in Java -- ping works at ICMP level which works on top of IP, whereas Java offers support for UDP (which sits on top of IP) and TCP (again on top of IP). It's basically a different (higher level) protocol for which you will need your own (native) library written in order to gain access to the IP stack.

东走西顾 2024-11-12 06:19:01

Ping 是一种特定的 ICMP 协议。您无法使用纯 Java 发送 ICMP 数据包。

但是,您可以打开到特定端口的 TCP 套接字并向其发送一些数据。有数百万个关于如何执行此操作的教程示例。

我建议您查看这些

http://www.google.co.uk /search?q=java+socket+tutorial 600 万条结果

http://www.google.co.uk/search?q=java+socket+example 1160 万个结果。

要只发送一个字符,您可以这样做

Socket s = new Socket(hostname, port);
s.getOutputStream().write((byte) '\n');
int ch = s.getInputStream().read();
s.close();
if (ch == '\n') // its all good.

Ping is a specific ICMP protocol. You cannot send ICMP packets in pure Java.

However, you can open a TCP Socket to a specific port and send it some data. There are millions of example of tutorials on how to do this.

I suggest you look at these

http://www.google.co.uk/search?q=java+socket+tutorial 6 million results

http://www.google.co.uk/search?q=java+socket+example 11.6 million results.

To send just one character you can do

Socket s = new Socket(hostname, port);
s.getOutputStream().write((byte) '\n');
int ch = s.getInputStream().read();
s.close();
if (ch == '\n') // its all good.
北城挽邺 2024-11-12 06:19:01

Ping 使用 java 中不可用的 ICMP 协议。这是在 java 中 ping 服务器的更好方法是:

       try{
        String s = null;
        List<String> commands = new ArrayList<String>();
        commands.add("ping");
        commands.add("192.168.2.154");
        ProcessBuilder processbuilder = new ProcessBuilder(commands);
        Process process = processbuilder.start();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
         System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null)
            {
              System.out.println(s);
            }

    }catch (Exception e) {
 System.out.println("This is sad ");

}

另一种方法可能是使用纯 java 套接字。

Ping uses ICMP protocol that is not available in java. This can be a better way to ping a server in java is to :

       try{
        String s = null;
        List<String> commands = new ArrayList<String>();
        commands.add("ping");
        commands.add("192.168.2.154");
        ProcessBuilder processbuilder = new ProcessBuilder(commands);
        Process process = processbuilder.start();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
         System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null)
            {
              System.out.println(s);
            }

    }catch (Exception e) {
 System.out.println("This is sad ");

}

Also another way could be is to work with pure java sockets.

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