IP of 3.Java network programming

Source: Internet
Author: User
Tags domain name server

in the previous two posts we have a brief overview of IP, ports, Protocols , and Two reference models , and we now look at this reference model from a procedural point of view.

If we are working on Web site development, then we should know that HTML is a Hypertext Markup Language (Hyper Text Markup Language), while ASP, PHP, JSP is the technology of dynamically generating HTML page, and HTTP is hypertext transmission, is based on www,html is Hypertext Transfer language, is based on HTTP. So we developed it in the application layer.

If we are doing network programming, we are developing at the transport and internetwork levels.

IP protocol:

Understand:

If we want to communicate with a computer on the Internet, first of all we need to consider how to establish a connection with each other, we intend to use wired fiber to connect? or through a landline or mobile phone? This is what the data link layer and the physical layer are going to do. Who do you intend to communicate with after establishing the connection? If it is a mobile phone, do you need to dial a mobile phone number? But if it's a computer, who do you want to talk to and how to be sure? At this point you need to use the Internet Layer IP protocol to distinguish which IP is who, so you can accurately and you want to chat with the person to chat.

It says so much about the concept crap, now we're going to do it. Write a small program in Java to get the host name and IP of the local computer.

The knowledge points we need to use are as follows:

Ipdemo.java source code is as follows:

 import  java.net.* class   Ipdemo { static  void  main (string[] args) throws   Exception {inetaddress i  =inetaddress        . Getlocalhost (); System.out.println (i.ToString ());  //         print local host name and local IP address  SYSTEM.OUT.PRINTLN ("Address:" +i.gethostaddress ()); //         print local IP address  System.out.println ("Name:" +i.gethostname ()); //  print local host name   

Program:

This is the host name and IP address of the local computer that was obtained, so can you enter the hostname of any computer to get information about that computer?

A method is provided under the java.net package.

Using this method can be achieved, but it is important to note that this implementation is not all possible, then why?

To add a point of knowledge here, we know that the IP address in the network distinguishes who is who and which computer it is. But this IP address is not good to remember, so the domain name was introduced later. When we enter in the browser address bar:

Http://www.baidu.com

At this time our computer will first access the local computer's C:\WINDOWS\system32\drivers\etc\host file for query,

# Copyright (c) 1993-1999 Microsoft corp.## This was a sample HOSTS file used by Microsoft TCP/IP for windows.## this F Ile contains the mappings of IP addresses to host names. each# entry should is kept on a individual line.  The IP address should# is placed in the first column followed by the corresponding host name.# the IP address and the host Name should is separated by at least one# space.## Additionally, comments (such as these) may is inserted on individual# Lines or following the machine name denoted by a ' # ' symbol.## For example:# # 102.54.94.97 rhino.acme.com # source server# 38.25.63.10X.acme.com # x Client Host

#127.0.0.1 localhost#203.208.46.146 www.google.com#203.208.46.146 dl.google.com#203.208.46.146 Dl-ssl.google.com

www.baidu.com is not found after the query, a request is made to the nearby DNS domain name server for the same query.

Although I have not read the DNS domain name server data storage format but presumably there should be at least one such document.

......

# 61.135.169.121 Www.baidu.com
# 61.135.169.125 Www.baidu.com
#111.13.100.91 www.baidu.com
# 221.176.187.238 www.sina.com.cn
......

When we query the www.baidu.com corresponding IP, after the DNS domain name resolves, the content in the browser becomes:

http://61.135.169.121

At this time the computer began to the IP corresponding to the server to make the request, the server to accept the request after the feedback to us a page, we saw the homepage of Baidu.

Therefore, only the host file or the DNS domain name server has the corresponding information to be found.

Once you know this, we'll write this program again.

All we have to do now is enter a domain name to get the IP for that domain name.

Ipdemo.java Source code is as follows:

import java.net.*; class Ipdemo  {    publicstaticvoid main (string[] args)throws  Exception    {        inetaddress i=inetaddress.getbyname ("www.baidu.com");        System.out.println ("Address:" +i.gethostaddress ()); // Print the host IP address        System.out.println ("Name:" +i.gethostname ()); // Print the host host name     }}

Note here is because Baidu to provide services to many users, so the corresponding IP more than one.

Program run:

Appendix:

Query results using webmaster Tools:

Query results using ip.cn Web site:

To query the results using ping:

IP of 3.Java network programming

Related Article

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.