How does Java handle ICMP packet sending and receiving?

Source: Internet
Author: User

Preface

Java is a cross-platform language. Generally, network operations are performed at or above the IP layer, that is, only tcp/udp operations can be performed. Of course, you can also set some options for tcp/udp, if you want to perform operations on the IP layer or data link layer, you can do nothing. You must rely on jni to use the socket interfaces of the local OS. Fortunately, I know that someone is developing jpcap when I know winpcap. This package allows you to conveniently operate the underlying network application protocol. The following is a detailed description.

Procedure

Download the required package quota.

You can use any ide tool you like, but you must add jpcap. jar to classpath; otherwise, it cannot be compiled. Icmp has many types. Currently, only the echo type is used as an example, that is, the network behavior generated by using the ping tool. The following code is detailed.



    1. Import java.net. InetAddress;
    2. Import jpcap. JpcapCaptor;
    3. Import jpcap. JpcapSender;
    4. Import jpcap. NetworkInterface;
    5. Import jpcap. packet. EthernetPacket;
    6. Import jpcap. packet. ICMPPacket;
    7. Import jpcap. packet. IPPacket;
    8. Class ICMP
    9. {
    10. Public static void main (String [] args) throws java. io. IOException {
    11. NetworkInterface [] devices = JpcapCaptor. getDeviceList ();
    12. If (args. length <1 ){
    13. System. out. println ("Usage: java ICMP ");
    14. For (int I = 0; I
    15. System. out. println (I + ":" + devices [I]. name + "(" + devices [I]. description + ")");
    16. System. exit (0 );
    17. }
    18. Int index = Integer. parseInt (args [0]);
    19. // Enable the network device
    20. JpcapCaptor captor = JpcapCaptor. openDevice (devices [index], 2000, false, 3000 );
    21. // Set to filter only icmp Packets
    22. Captor. setFilter ("icmp", true );
    23. JpcapSender sender = captor. getJpcapSenderInstance ();
    24. ICMPPacket p = new ICMPPacket ();
    25. P. type = ICMPPacket. ICMP_ECHO;
    26. P. seq = (short) 0x0005;
    27. P. id = (short) 0x0006;
    28. P. setIPv4Parameter (0, false, 0, false, 0,10101, 100, IPPacket. IPPROTO_ICMP,
    29. InetAddress. getByName ("192.168.1.30"), InetAddress. getByName ("192.168.1.1 "));
    30. P. data = "abcdefghijklmnopqrstuvwabcdehghi". getBytes ();
    31. EthernetPacket ether = new EthernetPacket ();
    32. Ether. frametype = EthernetPacket. ETHERTYPE_IP;
    33. // Enter the mac address of the user and the other user, which must be filled in correctly. If an error occurs, the user cannot receive the response packet.
    34. Ether. dst_mac = new byte [] {(byte) 0x00, (byte) 0x03, (byte) 0x2d, (byte) 0x02, (byte) 0xd1, (byte) 0x69 };
    35. Ether. src_mac = new byte [] {(byte) 0x08, (byte) 0x00, (byte) 0x46, (byte) 0xad, (byte) 0x3c, (byte) 0x12 };
    36. P. datalink = ether;
    37. Sender. sendPacket (p );
    38. System. out. println ("send ...");
    39. ICMPPacket rp = null;
    40. While (true ){
    41. Rp = (ICMPPacket) captor. getPacket ();
    42. If (rp = null ){
    43. Throw new IllegalArgumentException ("no rcv icmp echo reply ");
    44. } Else
    45. {
    46. System. out. println ("rcv icmp echo reply ");
    47. Return;
    48. }
    49. }
    50. }
    51. }
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.