A small Java Socket Function Application

Source: Internet
Author: User

The launch of Sun's Java language is an epoch language revolution. This language can adapt to the application software and network development fields at the same time, and its excellent network development functions are even more amazing, using Java, you can easily implement functions that require a lot of code and time in other languages: You can easily use Java coding and flash to never do so ), you can use Java to develop Webserver and middleware (refer to www.apusic.com) for load balancing and other functions. You can also develop your mail system; java Socket components with very powerful functions can be generated and run in ASP, which is easier than VB, VC, and Borland C ++ builder to develop corresponding components and have many powerful functions. The various highlights of Java are not completely described by the author here. However, the Code provided by the author below does not exceed 20 lines, which makes you feel the real strength of Java .. If you can use Java Socket flexibly, I believe that you can do better than me! OK! Lets begain!

This article uses the Java Socket communication function to implement the following two functions:

1. You can query the specific usage of domain and ip addresses on the internet. See the following document );

2. You can query the corresponding ip address and the corresponding machine name through the machine name in the LAN;
Principle: The following Code uses socket to query the corresponding internet dns resolution and LAN dns resolution to obtain the corresponding data;

 
 
  1. // Code starts
  2. /*
  3. This Code only provides testing and does not provide fully practical functions. However, you can use the following ideas to develop your own corresponding programs, note that the socket communication query domain or ip address over the internet provided in this article is not completely correct, because the socket access method used in this article is not the corresponding domain database accessed, therefore, you cannot query the exact domain or ip address, but all domain or ip addresses that can be accessed on the internet can be queried to the corresponding ip address (or domain ), if necessary, you can use the socket mentioned in this article to access the corresponding domain database to obtain the correct domain query function! Before running the program, make sure that you have installed jdk1.3 and configured relevant parameters.
  4. */
  5. Import Java.net .*;
  6. Class search
  7. {
  8. Public static void main (String [] args)
  9. {
  10. Try
  11. {
  12. InetAddressAddress=InetAddress. GetByName (args [0]);
  13. // Obtain the ip address or domain you want to query
  14. System. out. println (address );
  15. // Output query results
  16. }
  17. Catch (Exception e)
  18. {
  19. // Error message
  20. System. out. println ("sorry, the site or IP address you queried has not been found by the program. The possible reasons are as follows ");
  21. System. out. println ("1, this site has not been registered or this site has not been enabled ");
  22. System. out. println ("2. You are not connected to the network ");
  23. System. out. println ("3. This IP address may have been resolved by DHCP, but it has not been directed to the corresponding site ");
  24. }
  25. }
  26. }
  27. // Code ended

Save the above Code as search. Java

After entering the dos mode, use "Javac search. Java" to compile search. Java as search. class. Then, we can start to use our code for testing!

1. If you want to query domains in the internet, we use www.sina.com as an example. You can enter "Java search www.sina.com" and you will get the following result: "www.sina.com/206.204.114.57 ", the data returned to you contains the IP address of www.sina.com. If you re-enter: "Java search 206.204.114.57", the returned result is "www.sina.com/206.204.114.57". The returned result also contains the domain of sina, however, if your ip address resolves multiple domain names, you may not be able to accurately reverse query the domain by ip address, and some sites use different ip addresses for access convenience, that is to say, a site has several ip addresses, So that you only return the first ip address. If there are other problems, you can leave them for your own detection. As mentioned above, this article only provides you with a new idea;

2. I believe this is very useful when querying ip addresses through machine names in a LAN, however, you can easily obtain the corresponding machine name and IP address! Of course, you can also use the ip address to reverse query the machine name, but it is also a bit not a problem: if the machine does not exist, it will not report an error, but the result will be returned in the form of an ip address in front and back, well, I will not talk about anything else. You can use it slowly!

The Code provided above may not be very good for friends who are not used to Java, but you can change it to an applet to run in the browser!

The Java functions described in this article are only a small part of the Java Network functions. The powerful Java functions provided in this Article can only reflect a small part, if your E file is good enough, we suggest you visit Java.sun.com to learn more about Java .!

Note: Win2000 professional + jdk1.3

  1. Analysis on CGI supported by jsp http Server
  2. Use JSP pages to generate PDF reports
  3. Step for customizing JSP labels
  4. Detailed test of JSP containers
  5. Describes the following features of jsp http Server

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.