| |

VerySource

 Forgot password?
 Register
Search
View: 923|Reply: 2

How to use the java class (dnsjava package) to implement the ping method

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-22 09:00:03
| Show all posts |Read mode
Not allowed to call commands in java can only call existing java classes, how to implement, please master
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-23 21:00:02
| Show all posts
Following, I also want to know...
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 09:00:02
| Show all posts
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.DatagramPacket;
import java.io.IOException;

public class MyPing
{
    static final int echoPort = 7;
    static final int maxPingTime = 3000; // Milliseconds
    static final int pingPollInterval = 100; // Milliseconds

    public void ping(InetAddress dest)
    {
        String message = "test ping";
        byte[] outmsg = message.getBytes();
        int length = message.getBytes().length;
        byte[] inmsg = new byte[length];
        DatagramPacket outPacket = new DatagramPacket(outmsg, length);
        DatagramPacket inPacket = new DatagramPacket(inmsg, length);

        try
        {
            DatagramSocket socket = new DatagramSocket(echoPort, dest);
            outPacket.setAddress(dest);
            outPacket.setPort(echoPort);
            socket.send(outPacket);
            socket.setSoTimeout(5000);
            socket.receive(inPacket);
            System.out.println(new String(inmsg));
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}


import java.net.InetAddress;
import java.net.UnknownHostException;

class Tester
{
    public static void main(String[] a)
    {
        try
        {
            InetAddress addr = InetAddress.getLocalHost();
            new MyPing().ping(addr);
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list