Java: Network editor (Inetaddress,inetsocketaddress,url)

Source: Internet
Author: User

Ineraddress:

/**IP Address: Uniquely identifies a computer on the network
* Port number: Indicates a different application on the computer
* Java.net.InetAddress class: This class represents an Internet Protocol (IP) address.
* Common methods:
* Getbyname (String host) determines the IP address of the host in the given host name.
*gethostname () Gets the host name of this IP address.
*gethostaddress () returns the IP address string (in textual representation).
*getlocalhost () returns the local host.
*getallbyname (String host) returns an array of its IP addresses, based on the name service configured on the system, given the host name.
*
*/

 Public classtestinetaddress { Public Static voidMain (string[] args) {Try {            //determines the IP address of the host in the given host name. //inetaddress inetaddress = Inetaddress.getbyname ("p-pc");InetAddress inetaddress = Inetaddress.getlocalhost ();//get Local HostSystem. out. println (inetaddress); String HostName=Inetaddress.gethostname (); System. out. println ("Host Name:"+hostName); String IP=inetaddress.gethostaddress (); System. out. println ("IP Address:"+IP); //get its IP address based on host name or domain nameinetaddress[] ids = Inetaddress.getallbyname ("www.baidu.com");  for(inetaddress inetaddress2:ids) {System. out. println (INETADDRESS2); }        } Catch(unknownhostexception e) {e.printstacktrace (); }            }}

Inetsocketaddress:

*java.net.inetsocketaddress class: This class implements an IP socket address (IP address + port number).
* Construction Method
*inetsocketaddress (inetaddress addr, int port) creates a socket address based on the IP address and port number.
*inetsocketaddress (String hostname, int port) creates a socket address based on host name and port number.
* Common methods:
*getaddress (): Gets inetaddress.
*getport () Gets the port number.
*tostring () Constructs a string representation of this inetsocketaddress. (Host name/IP: port number)
*gethostname () gets the host name.

 Public classtestinetsocketaddress { Public Static voidMain (string[] args) {//inetsocketaddress socketaddress = new Inetsocketaddress ("127.0.0.1", 3306);        Try{inetsocketaddress socketaddress=NewInetsocketaddress (Inetaddress.getlocalhost (),3306); System. out. println (socketaddress); InetAddress inetaddress=socketaddress.getaddress (); System. out. println ("Host Information:"+inetaddress); intPort =Socketaddress.getport (); System. out. println ("Port number:"+port); String HostName=Socketaddress.gethostname (); System. out. println ("Host Name:"+hostName); } Catch(unknownhostexception e) {e.printstacktrace (); }            }}

Url:

*url: Uniform Resource Locator
* Component: Protocol, hostname or IP, port number, resource path
*java.net.url class: Represents a Uniform Resource locator, which is a pointer to the Internet "resource"
* Common methods of construction
* URL (string spec) creates a URL object based on the string representation.
* URL (string protocol, string host, int port, string file) creates a URL object based on the specified protocol, host, port number, and file.
* Common methods:
* Getprotocol () Gets the protocol name for this URL.
* GetHost () Gets the hostname of this URL (if applicable).
* Getport () Gets the port number for this URL.
* GetFile () Gets the file name of this URL.
* Getdefaultport () Gets the default port number of the protocol associated with this URL.
* GetPath () Gets the path portion of this URL.
* ...

 Public classTesturl { Public Static voidMain (string[] args) {Try{URL URL=NewURL ("HTTP://WWW.BAIDU.COM/INDEX.HTML#AA?CANSU=BJSXT"); String Protocol=Url.getprotocol (); System. out. println ("protocol:"+protocol); String Host=Url.gethost (); System. out. println ("Host Name:"+host); intPort =Url.getport (); System. out. println ("Port number:"+port); intDefualtport =Url.getdefaultport (); System. out. println ("Default port:"+defualtport); String file=Url.getfile (); System. out. println ("Resource path:"+file); String Path=Url.getpath (); System. out. println ("Resource path:"+path); } Catch(malformedurlexception e) {e.printstacktrace (); }            }}

URL class
* InputStream OpenStream () opens a connection to this URL and returns a InputStream that is used to read from the connection.

Import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.ioexception;import Java.io.inputstream;import java.io.inputstreamreader;import java.net.malformedurlexception;import Java.net.URL; Public classWebspider { Public Static voidMain (string[] args) {Try{URL URL=NewURL ("https://channel.jd.com/men.html"); InputStream IPs=Url.openstream (); InputStreamReader ISR=NewInputStreamReader (IPS);//convert a byte stream to a character streamBufferedReader br =NewBufferedReader (ISR);            String str;  while((Str=br.readline ())! =NULL) {System. out. println (str);        } br.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

Java: Network editor (Inetaddress,inetsocketaddress,url)

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.