{"id":174,"date":"2018-07-26T11:29:30","date_gmt":"2018-07-26T11:29:30","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=174"},"modified":"2018-08-02T06:57:48","modified_gmt":"2018-08-02T06:57:48","slug":"java-networking-introduction","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/java-networking-introduction\/","title":{"rendered":"Java Networking &#8211; Introduction"},"content":{"raw":"<div>\r\n\r\n<strong>Java for Networking<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Java is practically a synonym for Internet Programming. Using Java we can generate secure, cross-platform and portable code. Java has complete network package to support networking using java.net.* package. I\/O stream library in Java also works quite well for TCP\/IP. Threading is also very useful for networking and is relatively easy in Java.<\/p>\r\n&nbsp;\r\n\r\n<strong>Networking Basics<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Internet protocol (IP) addresses<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Every host on Internet has a unique IP address. For example an IP address would look like 143.89.40.46, 203.184.197.198 203.184.197.196, 203.184.197.197, 127.0.0 and soon<\/p>\r\n<p style=\"text-align: justify\">2.Every host on the Internet can also be identified more conveniently by using hostname string like cse.ust.hk, tom.com, localhost.<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">3.Even one hostname can correspond to multiple internet addresses. For example,www.yahoo.com corresponds to many addresses like\u00a0\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">66.218.70.49; 66.218.70.50; 66.218.71.80; 66.218.71.84; \u2026<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">4. Domain Naming Service (DNS) maps these hostnames to numbers.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 <\/strong>\r\n\r\n<strong>Ports<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Many different services can be running on the host. A port identifies a service within a host. Many standard port numbers are pre-assigned. They are:<\/p>\r\n&nbsp;\r\n\r\ntime of day <strong>13<\/strong>, FTP <strong>21<\/strong>, TELNET <strong>23<\/strong>, SMTP <strong>25<\/strong>, HTTP <strong>80<\/strong>.\r\n\r\n&nbsp;\r\n\r\nWe can say that <strong>IP address + port number = \"phone number\u201c<\/strong> for service.\r\n\r\n&nbsp;\r\n\r\n<strong>Protocols<\/strong>\r\n\r\n&nbsp;\r\n\r\nProtocols are the rules that facilitate communications between machines.\r\n\r\nSome examples of different protocols are,\r\n\r\n&nbsp;\r\n\r\n1. HTTP - HyperText Transfer Protocol\r\n\r\n2. FTP - File Transfer Protocol\r\n\r\n3. SMTP - Simple Message Transfer Protocol\r\n\r\n4. TCP -Transmission Control Protocol\r\n<p style=\"text-align: justify\">5.\u00a0 UDP - User Datagram Protocol (e.g., video delivery)<\/p>\r\n<p style=\"text-align: justify\">Protocols are standardized and documented and so machines can reliably work with one another.<\/p>\r\n&nbsp;\r\n\r\n<strong>Client-Server interaction<\/strong>\r\n\r\n&nbsp;\r\n\r\nCommunication between hosts is two-way, but usually the two hosts take different roles.\r\n\r\n&nbsp;\r\n\r\n<strong>1.\u00a0 Server waits for client to make request<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Server registered on a known port with the host (\"public phone number\") . It usually runs in an endless loop and listens for incoming client connections.<\/p>\r\n&nbsp;\r\n\r\n<strong>2.\u00a0 Client \"calls\" server to start a conversation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Client making calls using hostname\/IP address and port number. Then it sends the request and waits for response.<\/p>\r\n&nbsp;\r\n\r\n<strong>3.\u00a0 Standard services always running<\/strong>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Server offers shared resource such as information, database, files, printer and compute power etc. to clients. Server running on host using expected port like ftp, http, smtp, etc.<\/span><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong> Networking Basics<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can try out some services of servers using telnet. Telnet assumes that whether you want to connect to port 23 on the receiving host (port 23 is where the telnet server is listening). However there is an optional argument after the hostname that allows you to connect to a different port. Many servers now refuse telnet connections due to security reasons.<\/p>\r\n&nbsp;\r\n\r\n<strong>Networking with Java<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Networking package uses the classes in java.net.*. Java programs can use TCP or UDP to communicate over the Internet. The URL and URLConnection classes in the package are used to work with Web applications. The Socket and ServerSocket (client-server applications) classes uses TCP to communicate over the network. The DatagramPacket, DatagramSocket, and MulticastSocket classes are used to work with UDP communication.<\/p>\r\n&nbsp;\r\n\r\n<strong>Java Networking<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe following are the classes used with the networking package.\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0\u00a0 InetAddress\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0\u00a0 URL\r\n\r\n3.\u00a0\u00a0\u00a0\u00a0\u00a0 URLConnection\r\n\r\n4.\u00a0\u00a0\u00a0\u00a0\u00a0 Using Sockets\r\n\r\n&nbsp;\r\n\r\n<strong>\u00a0 InetAddress<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The <strong>InetAddress<\/strong> class is used to encapsulate both the numerical IP address and the domain name for that address.\u00a0<strong>java.net.InetAddress <\/strong>class converts between hostnames and Internet addresses. We can interact with this class by using the name of an IP host, which is more convenient and understandable than its IP address. The <strong>InetAddress<\/strong> class hides the number inside. <strong>InetAddress<\/strong> can handle both IPv4 and IPv6 addresses.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong>Testing without a Network<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are three ways to test the local machine,\r\n\r\na) \u201clocalhost\u201d: It is the \u201clocal loopback\u201d IP address for testing without a network.InetAddress addr = InetAddress.getByName(null);\r\n\r\nb)\u00a0 Equivalently:InetAddress.getByName(\"localhost\");\r\n\r\nc) Or using the reserved IP number for the loopback:InetAddress.getByName(\"127.0.0.1\");\r\n\r\n&nbsp;\r\n\r\n<strong>InetAddress<\/strong>\r\n\r\n&nbsp;\r\n\r\nInetAddress class is used to dentify a machine either with the hostname or with the IP address.\r\n\r\n&nbsp;\r\n\r\n<strong>a)\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><strong>Identifying a Machine - IP (Internet Protocol) address<\/strong><strong>\u00a0<\/strong>\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0\u00a0 DNS (Domain Name System) : www.auist.net\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0\u00a0 Dotted Quad Form : 123.255.28.120\r\n\r\nInetAddress ia=InetAddress.getByName(\u2026);\r\n\r\n&nbsp;\r\n\r\n<strong>b)\u00a0 Local Host (for testing)<\/strong>\r\n\r\n&nbsp;\r\n\r\nAll these forms connect to local host,\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getByName(null) ;\r\n\r\n2.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getByName(\u201clocalhost\u201d);\r\n\r\n3.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getByName(\u201c127.0.0.1\u201d);\r\n\r\n4.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getLocalHost()\r\n\r\n&nbsp;\r\n\r\n<strong>Identifying a Machine<\/strong>\r\n\r\n&nbsp;\r\n\r\nA simple program to identify a machine using the hostname is given below,\r\n\r\nimport java.net.*;\r\n\r\npublic class WhoAmI {\r\n\r\npublic static void main(String[] args) throws Exception\r\n\r\n{\r\n\r\nif(args.length != 1)\r\n\r\n{\r\n\r\nSystem.err.println(\"Usage: WhoAmI MachineName\"); System.exit(1);\r\n\r\n}\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">InetAddress a = InetAddress.getByName(args[0]); System.out.println(a);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">To run:<\/strong>\r\n\r\n<\/div>\r\njava WhoAmI Vijim\r\n\r\nVijim\/127.0.0.1\r\n\r\n&nbsp;\r\n\r\n<strong>Factory Methods<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Factory methods are merely a convention whereby static methods in a class return an instance of that class. Three commonly used <strong>InetAddress<\/strong> factory methods are shown here:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">a) static InetAddress getLocalHost( ) throws UnknownHostException<\/p>\r\n<p style=\"text-align: justify\">b) static InetAddress getByName(String hostName) throws UnknownHostException<\/p>\r\n<p style=\"text-align: justify\">c) static InetAddress[ ] getAllByName(String hostName) throwsUnknownHostException<\/p>\r\n&nbsp;\r\n\r\n<strong>\u00a0 \u00a0Factory Method Description<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u00a0a) getLocalHost():<\/p>\r\n<p style=\"text-align: justify\">The <strong>getLocalHost( )<\/strong> method simply returns the <strong>InetAddress<\/strong> object that represents the local host.<\/p>\r\n<p style=\"text-align: justify\">b) getByName():<\/p>\r\n<p style=\"text-align: justify\">The <strong>getByName( )<\/strong> method returns an <strong>InetAddress<\/strong> for a host name passed to it. If these methods are unable to resolve the host name, they throw an <strong>UnknownHostException<\/strong>.<\/p>\r\n<p style=\"text-align: justify\">c) getAllByName():<\/p>\r\n<p style=\"text-align: justify\">The <strong>getAllByName( )<\/strong> factory method returns an array of <strong>InetAddress<\/strong>es that represent all of the addresses that a particular name resolves to. It will also throw an <strong>UnknownHostException<\/strong> if it can\u2019t resolve the name to at least one address.<\/p>\r\n&nbsp;\r\n\r\nTable 20.1 describes the instance methods of the InetAddress class.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-175 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100.png\" alt=\"\" width=\"618\" height=\"401\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>Example of InetAddress<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport java.net.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class IPAddress\r\n\r\n{\r\n\r\npublic static void main(String[] args)\r\n\r\n{\r\n\r\ntry {\r\n\r\n\/\/\u00a0 Get hostname by textual representation of IP address InetAddress addr = InetAddress.getByName(\"java -tips.org\");\r\n\r\nSystem.out.println(\" Address\" + addr);\r\n\r\n\/\/\u00a0 Get hostname by a byte array containing the IP address byte[] ipAddr = new byte[]{127, 0, 0, 1};\r\n\r\nInetAddress addr1 = InetAddress.getByAddress(ipAddr); System.out.println(\" Address1\" + addr1);\r\n\r\n\/\/\u00a0 Get the host name\r\n\r\nString hostname = addr1.getHostName();\r\n\r\nSystem.out.println(\" hostname\" + hostname);\r\n\r\n\/\/ Get canonical host name\r\n\r\nString canonicalhostname = addr1.getCanonicalHostName();\r\n\r\nSystem.out.println(\" canonical host name\" + canonicalhostname);\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">} catch (UnknownHostException e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">The example code uses the method, getByName() that gets the hostname by textual representation of IP address and returns the InetAddress object. It also uses the method getByAddress() that gets the hostname by a byte array containing the IP address and returns the InetAddress object. The code demonstrates getHostName() that gets the host name and returns the InetAddress object. The code also demonstrates getCanonicalHostName() that gets the canonical host name and returns the InetAddress object.<\/p>\r\nThe output of the above code is shown in Figure 20.1.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Output of InetAddress Program<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-176 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101.png\" alt=\"\" width=\"614\" height=\"304\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Example of IPAddress<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport java.net.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class IPAddress1\r\n\r\n{\r\n\r\npublic static void main(String[] args)\r\n\r\n{\r\n\r\ntry\r\n\r\n{\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0InetAddress addr = InetAddress.getByName(\"java -tips.org\"); byte[] ipAddr = addr.getAddress(); System.out.println(\"ByteAdress:\" + ipAddr );<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">String HostAddress = addr.getHostAddress(); System.out.println(\"HostAdress:\" + HostAddress ); String HostName = addr.getHostName(); System.out.println(\"HostName:\" + HostName );<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">String ipAddrStr = \"\";<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">for (int i=0; i&lt;ipAddr.length; i++)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">if (i &gt; 0) {\u00a0\u00a0\u00a0\u00a0\u00a0 ipAddrStr += \".\";\u00a0\u00a0 }<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">ipAddrStr += ipAddr[i]&amp;0xFF;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">System.out.println(\"IPAdress:\" + ipAddrStr);<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">InetAddress localhostIP = InetAddress.getByAddress(\"127.0.0.1\",<\/span><span style=\"text-align: initial;font-size: 1em\">new byte[]{127, 0, 0, 1});<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">System.out.println(\"get by Ip Address:\" + localhostIP); String HostName1 = localhostIP.getHostName(); System.out.println(\"HostName:\" + HostName1 );<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">String hostname2 = InetAddress.getByName(\"127.0.0.1\").getHostName(); System.out.println(\"HostName:\" + hostname2 );<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">} catch (UnknownHostException e) {<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example code is to demonstrate the instance methods of InetAddress object. It retrieves the InetAddress object using the method <em>getByName()<\/em> by passing the host name as \"java-tips.org\". The <em>getAddress() <\/em>method returns the object\u2019s IP address as byte array in network byte order from the InetAddress object. The method <em>getHostAddress()<\/em> returns a string that represents the host address associated with the InetAddress object and the method <em>getHostName()<\/em> returns a string that represents the host name associated with the InetAddress object. Then there exists a for loop which retrieves evry single byte from the IP addr and prints it.<span style=\"text-align: initial;font-size: 1em\">The output of this code is shown in Figure 20.2.<\/span><\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Output of IPAddress<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-177 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102.png\" alt=\"\" width=\"598\" height=\"356\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>Inet4Address and Inet6Address<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Beginning with version 1.4, Java has included support for IPv6 addresses. Two subclasses of <strong>InetAddress<\/strong> were created, they are: <strong>Inet4Address<\/strong> and <strong>Inet6Address<\/strong>. <strong>Inet4Address<\/strong> represents a traditional-style IPv4 address. <strong>Inet6Address<\/strong> encapsulates a new-style IPv6 address.<\/p>\r\n&nbsp;\r\n\r\n<strong>URL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The java.ne t.URL class can be used to identify the files on the Internet. URL (Uniform Resource Locator) is a pointer to a \"resource\" on the World Wide Web. A resource can be something as simple as a file or a directory. We can create a URL object using the following constructor:public URL(String spec) throws MalformedURLException<\/p>\r\n&nbsp;\r\n\r\nThere exists two classes for URL processing in Java.\r\n<ul>\r\n \t<li>java.net.URL which represents an URL resource<\/li>\r\n \t<li>java.net.URLConnection which is used to open a connection to a URL resource.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0We can create an absolute URL object using an constructor of the URL class as follows,<\/span>\r\n<div>\r\n<ul>\r\n \t<li>\u00a0 By Constructor : URL(String)<\/li>\r\n \t<li>\u00a0 URL myurl = new URL(\u201chttp:\/\/cs.biu.ac.il\/\u201d) ;<\/li>\r\n<\/ul>\r\nWe can create a URL relative to another URL. This is used for relative hyperlinks in an HTML page.\r\n\r\nGiven a URL, &lt;A HREF=\u201cMyPres.html\u201d&gt;Presentations&lt;\/A&gt;\r\n\r\n&nbsp;\r\n\r\nBy Constructor : URL(URL, String)\r\n\r\nURL myurl = new URL(\u201chttp:\/\/cs.biu.ac.il\/~freskom1\/\u201d) ;\r\n\r\nURL mypres = new URL(myurl, \u201cMyPres.html\u201d) ;\r\n\r\nOther URL Constructors that exists are,\r\n\r\n&nbsp;\r\n\r\n(i)\u00a0\u00a0\u00a0 URL(String protocol, String host, int port, String file)\r\n\r\n(ii)\u00a0\u00a0 URL(String protocol, String host, String file)\r\n\r\n(iii)\u00a0 URL(String spec)\r\n\r\n(iv)\u00a0 URL(URL context, String spec)\r\n\r\n&nbsp;\r\n\r\nAll constructors throw MalformedURLExce ption\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Parsing a URL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Once a URL object is created, we can parse the URL and retrieve the individual components in the URL like what protocol is mentioned, the name of the host, port,file mentioned in the URL, and reference if present in the URL.<\/p>\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0 String getProtocol()\r\n\r\n2.\u00a0\u00a0\u00a0 String getHost()\r\n\r\n3.\u00a0\u00a0\u00a0 int getPort()\r\n\r\n4.\u00a0\u00a0\u00a0 String getFile()\r\n\r\n5.\u00a0\u00a0\u00a0 String getRef()\r\n\r\n&nbsp;\r\n\r\n<strong>Example of ParseURL<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport java.net.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class ParseURL\r\n\r\n{\r\n\r\npublic static void main(String args[]) throws Exception {\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">URL u = new URL(\"http:\/\/java.sun.com\/docs\/books\/tutorial\/#down \");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"protocol =\" + u.getProtocol());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"host =\" + u.getHost());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"filename =\" + u.getFile());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"port =\" + u.getPort());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"ref =\"+\u00a0 u.getRef());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">This example demonstrates about the instance methods of URL. The method <em>getProtocol()<\/em> retrieves the protocol from the URL. The method <em>getHost()<\/em> retrieves the hostname from the URL. The method <em>getFile() <\/em>retrieves the filename, the method<em> getPort() <\/em>retrieves the port no if exists in the URL and<em> getRef() <\/em>retrieves the reference from the URL.<\/p>\r\n&nbsp;\r\n\r\nThe output of this code is shown in Figure 20.3.\r\n\r\n&nbsp;\r\n\r\n<strong>Output of ParseURL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-178 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103.png\" alt=\"\" width=\"609\" height=\"355\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module discusses about the basics for Internet Programming. This module also explores about working with InetAddress class for obtaining the Internet Address and also gives an idea about URL class and the URLConnection class.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n<ul>\r\n \t<li>Herbert Schildt, \" <em>Java: The Complete Reference\", 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\r\n<\/ul>","rendered":"<div>\n<p><strong>Java for Networking<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Java is practically a synonym for Internet Programming. Using Java we can generate secure, cross-platform and portable code. Java has complete network package to support networking using java.net.* package. I\/O stream library in Java also works quite well for TCP\/IP. Threading is also very useful for networking and is relatively easy in Java.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Networking Basics<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Internet protocol (IP) addresses<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Every host on Internet has a unique IP address. For example an IP address would look like 143.89.40.46, 203.184.197.198 203.184.197.196, 203.184.197.197, 127.0.0 and soon<\/p>\n<p style=\"text-align: justify\">2.Every host on the Internet can also be identified more conveniently by using hostname string like cse.ust.hk, tom.com, localhost.<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">3.Even one hostname can correspond to multiple internet addresses. For example,www.yahoo.com corresponds to many addresses like\u00a0\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">66.218.70.49; 66.218.70.50; 66.218.71.80; 66.218.71.84; \u2026<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">4. Domain Naming Service (DNS) maps these hostnames to numbers.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 <\/strong><\/p>\n<p><strong>Ports<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Many different services can be running on the host. A port identifies a service within a host. Many standard port numbers are pre-assigned. They are:<\/p>\n<p>&nbsp;<\/p>\n<p>time of day <strong>13<\/strong>, FTP <strong>21<\/strong>, TELNET <strong>23<\/strong>, SMTP <strong>25<\/strong>, HTTP <strong>80<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>We can say that <strong>IP address + port number = &#8220;phone number\u201c<\/strong> for service.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Protocols<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Protocols are the rules that facilitate communications between machines.<\/p>\n<p>Some examples of different protocols are,<\/p>\n<p>&nbsp;<\/p>\n<p>1. HTTP &#8211; HyperText Transfer Protocol<\/p>\n<p>2. FTP &#8211; File Transfer Protocol<\/p>\n<p>3. SMTP &#8211; Simple Message Transfer Protocol<\/p>\n<p>4. TCP -Transmission Control Protocol<\/p>\n<p style=\"text-align: justify\">5.\u00a0 UDP &#8211; User Datagram Protocol (e.g., video delivery)<\/p>\n<p style=\"text-align: justify\">Protocols are standardized and documented and so machines can reliably work with one another.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Client-Server interaction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Communication between hosts is two-way, but usually the two hosts take different roles.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.\u00a0 Server waits for client to make request<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Server registered on a known port with the host (&#8220;public phone number&#8221;) . It usually runs in an endless loop and listens for incoming client connections.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.\u00a0 Client &#8220;calls&#8221; server to start a conversation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Client making calls using hostname\/IP address and port number. Then it sends the request and waits for response.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.\u00a0 Standard services always running<\/strong><\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Server offers shared resource such as information, database, files, printer and compute power etc. to clients. Server running on host using expected port like ftp, http, smtp, etc.<\/span><\/p>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong> Networking Basics<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can try out some services of servers using telnet. Telnet assumes that whether you want to connect to port 23 on the receiving host (port 23 is where the telnet server is listening). However there is an optional argument after the hostname that allows you to connect to a different port. Many servers now refuse telnet connections due to security reasons.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Networking with Java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Networking package uses the classes in java.net.*. Java programs can use TCP or UDP to communicate over the Internet. The URL and URLConnection classes in the package are used to work with Web applications. The Socket and ServerSocket (client-server applications) classes uses TCP to communicate over the network. The DatagramPacket, DatagramSocket, and MulticastSocket classes are used to work with UDP communication.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Java Networking<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The following are the classes used with the networking package.<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 InetAddress<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 URL<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0 URLConnection<\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Using Sockets<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0 InetAddress<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The <strong>InetAddress<\/strong> class is used to encapsulate both the numerical IP address and the domain name for that address.\u00a0<strong>java.net.InetAddress <\/strong>class converts between hostnames and Internet addresses. We can interact with this class by using the name of an IP host, which is more convenient and understandable than its IP address. The <strong>InetAddress<\/strong> class hides the number inside. <strong>InetAddress<\/strong> can handle both IPv4 and IPv6 addresses.<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong>Testing without a Network<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are three ways to test the local machine,<\/p>\n<p>a) \u201clocalhost\u201d: It is the \u201clocal loopback\u201d IP address for testing without a network.InetAddress addr = InetAddress.getByName(null);<\/p>\n<p>b)\u00a0 Equivalently:InetAddress.getByName(&#8220;localhost&#8221;);<\/p>\n<p>c) Or using the reserved IP number for the loopback:InetAddress.getByName(&#8220;127.0.0.1&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>InetAddress<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>InetAddress class is used to dentify a machine either with the hostname or with the IP address.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>a)\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><strong>Identifying a Machine &#8211; IP (Internet Protocol) address<\/strong><strong>\u00a0<\/strong><\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 DNS (Domain Name System) : www.auist.net<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Dotted Quad Form : 123.255.28.120<\/p>\n<p>InetAddress ia=InetAddress.getByName(\u2026);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>b)\u00a0 Local Host (for testing)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>All these forms connect to local host,<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getByName(null) ;<\/p>\n<p>2.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getByName(\u201clocalhost\u201d);<\/p>\n<p>3.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getByName(\u201c127.0.0.1\u201d);<\/p>\n<p>4.\u00a0\u00a0\u00a0 InetAddress ia=InetAddress.getLocalHost()<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Identifying a Machine<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A simple program to identify a machine using the hostname is given below,<\/p>\n<p>import java.net.*;<\/p>\n<p>public class WhoAmI {<\/p>\n<p>public static void main(String[] args) throws Exception<\/p>\n<p>{<\/p>\n<p>if(args.length != 1)<\/p>\n<p>{<\/p>\n<p>System.err.println(&#8220;Usage: WhoAmI MachineName&#8221;); System.exit(1);<\/p>\n<p>}<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">InetAddress a = InetAddress.getByName(args[0]); System.out.println(a);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">To run:<\/strong><\/p>\n<\/div>\n<p>java WhoAmI Vijim<\/p>\n<p>Vijim\/127.0.0.1<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Factory Methods<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Factory methods are merely a convention whereby static methods in a class return an instance of that class. Three commonly used <strong>InetAddress<\/strong> factory methods are shown here:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">a) static InetAddress getLocalHost( ) throws UnknownHostException<\/p>\n<p style=\"text-align: justify\">b) static InetAddress getByName(String hostName) throws UnknownHostException<\/p>\n<p style=\"text-align: justify\">c) static InetAddress[ ] getAllByName(String hostName) throwsUnknownHostException<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0 \u00a0Factory Method Description<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u00a0a) getLocalHost():<\/p>\n<p style=\"text-align: justify\">The <strong>getLocalHost( )<\/strong> method simply returns the <strong>InetAddress<\/strong> object that represents the local host.<\/p>\n<p style=\"text-align: justify\">b) getByName():<\/p>\n<p style=\"text-align: justify\">The <strong>getByName( )<\/strong> method returns an <strong>InetAddress<\/strong> for a host name passed to it. If these methods are unable to resolve the host name, they throw an <strong>UnknownHostException<\/strong>.<\/p>\n<p style=\"text-align: justify\">c) getAllByName():<\/p>\n<p style=\"text-align: justify\">The <strong>getAllByName( )<\/strong> factory method returns an array of <strong>InetAddress<\/strong>es that represent all of the addresses that a particular name resolves to. It will also throw an <strong>UnknownHostException<\/strong> if it can\u2019t resolve the name to at least one address.<\/p>\n<p>&nbsp;<\/p>\n<p>Table 20.1 describes the instance methods of the InetAddress class.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-175 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100.png\" alt=\"\" width=\"618\" height=\"401\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100.png 618w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100-300x195.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100-225x146.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-100-350x227.png 350w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>Example of InetAddress<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import java.net.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class IPAddress<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args)<\/p>\n<p>{<\/p>\n<p>try {<\/p>\n<p>\/\/\u00a0 Get hostname by textual representation of IP address InetAddress addr = InetAddress.getByName(&#8220;java -tips.org&#8221;);<\/p>\n<p>System.out.println(&#8221; Address&#8221; + addr);<\/p>\n<p>\/\/\u00a0 Get hostname by a byte array containing the IP address byte[] ipAddr = new byte[]{127, 0, 0, 1};<\/p>\n<p>InetAddress addr1 = InetAddress.getByAddress(ipAddr); System.out.println(&#8221; Address1&#8243; + addr1);<\/p>\n<p>\/\/\u00a0 Get the host name<\/p>\n<p>String hostname = addr1.getHostName();<\/p>\n<p>System.out.println(&#8221; hostname&#8221; + hostname);<\/p>\n<p>\/\/ Get canonical host name<\/p>\n<p>String canonicalhostname = addr1.getCanonicalHostName();<\/p>\n<p>System.out.println(&#8221; canonical host name&#8221; + canonicalhostname);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">} catch (UnknownHostException e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">The example code uses the method, getByName() that gets the hostname by textual representation of IP address and returns the InetAddress object. It also uses the method getByAddress() that gets the hostname by a byte array containing the IP address and returns the InetAddress object. The code demonstrates getHostName() that gets the host name and returns the InetAddress object. The code also demonstrates getCanonicalHostName() that gets the canonical host name and returns the InetAddress object.<\/p>\n<p>The output of the above code is shown in Figure 20.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Output of InetAddress Program<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-176 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101.png\" alt=\"\" width=\"614\" height=\"304\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101.png 614w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101-300x149.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-101-350x173.png 350w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 Example of IPAddress<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import java.net.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class IPAddress1<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args)<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0InetAddress addr = InetAddress.getByName(&#8220;java -tips.org&#8221;); byte[] ipAddr = addr.getAddress(); System.out.println(&#8220;ByteAdress:&#8221; + ipAddr );<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">String HostAddress = addr.getHostAddress(); System.out.println(&#8220;HostAdress:&#8221; + HostAddress ); String HostName = addr.getHostName(); System.out.println(&#8220;HostName:&#8221; + HostName );<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">String ipAddrStr = &#8220;&#8221;;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">for (int i=0; i&lt;ipAddr.length; i++)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">if (i &gt; 0) {\u00a0\u00a0\u00a0\u00a0\u00a0 ipAddrStr += &#8220;.&#8221;;\u00a0\u00a0 }<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">ipAddrStr += ipAddr[i]&amp;0xFF;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;IPAdress:&#8221; + ipAddrStr);<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">InetAddress localhostIP = InetAddress.getByAddress(&#8220;127.0.0.1&#8221;,<\/span><span style=\"text-align: initial;font-size: 1em\">new byte[]{127, 0, 0, 1});<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;get by Ip Address:&#8221; + localhostIP); String HostName1 = localhostIP.getHostName(); System.out.println(&#8220;HostName:&#8221; + HostName1 );<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">String hostname2 = InetAddress.getByName(&#8220;127.0.0.1&#8221;).getHostName(); System.out.println(&#8220;HostName:&#8221; + hostname2 );<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">} catch (UnknownHostException e) {<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example code is to demonstrate the instance methods of InetAddress object. It retrieves the InetAddress object using the method <em>getByName()<\/em> by passing the host name as &#8220;java-tips.org&#8221;. The <em>getAddress() <\/em>method returns the object\u2019s IP address as byte array in network byte order from the InetAddress object. The method <em>getHostAddress()<\/em> returns a string that represents the host address associated with the InetAddress object and the method <em>getHostName()<\/em> returns a string that represents the host name associated with the InetAddress object. Then there exists a for loop which retrieves evry single byte from the IP addr and prints it.<span style=\"text-align: initial;font-size: 1em\">The output of this code is shown in Figure 20.2.<\/span><\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Output of IPAddress<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-177 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102.png\" alt=\"\" width=\"598\" height=\"356\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102.png 598w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-102-350x208.png 350w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/p>\n<div>\n<p><strong>Inet4Address and Inet6Address<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Beginning with version 1.4, Java has included support for IPv6 addresses. Two subclasses of <strong>InetAddress<\/strong> were created, they are: <strong>Inet4Address<\/strong> and <strong>Inet6Address<\/strong>. <strong>Inet4Address<\/strong> represents a traditional-style IPv4 address. <strong>Inet6Address<\/strong> encapsulates a new-style IPv6 address.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>URL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The java.ne t.URL class can be used to identify the files on the Internet. URL (Uniform Resource Locator) is a pointer to a &#8220;resource&#8221; on the World Wide Web. A resource can be something as simple as a file or a directory. We can create a URL object using the following constructor:public URL(String spec) throws MalformedURLException<\/p>\n<p>&nbsp;<\/p>\n<p>There exists two classes for URL processing in Java.<\/p>\n<ul>\n<li>java.net.URL which represents an URL resource<\/li>\n<li>java.net.URLConnection which is used to open a connection to a URL resource.<\/li>\n<\/ul>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0We can create an absolute URL object using an constructor of the URL class as follows,<\/span><\/p>\n<div>\n<ul>\n<li>\u00a0 By Constructor : URL(String)<\/li>\n<li>\u00a0 URL myurl = new URL(\u201chttp:\/\/cs.biu.ac.il\/\u201d) ;<\/li>\n<\/ul>\n<p>We can create a URL relative to another URL. This is used for relative hyperlinks in an HTML page.<\/p>\n<p>Given a URL, &lt;A HREF=\u201cMyPres.html\u201d&gt;Presentations&lt;\/A&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>By Constructor : URL(URL, String)<\/p>\n<p>URL myurl = new URL(\u201chttp:\/\/cs.biu.ac.il\/~freskom1\/\u201d) ;<\/p>\n<p>URL mypres = new URL(myurl, \u201cMyPres.html\u201d) ;<\/p>\n<p>Other URL Constructors that exists are,<\/p>\n<p>&nbsp;<\/p>\n<p>(i)\u00a0\u00a0\u00a0 URL(String protocol, String host, int port, String file)<\/p>\n<p>(ii)\u00a0\u00a0 URL(String protocol, String host, String file)<\/p>\n<p>(iii)\u00a0 URL(String spec)<\/p>\n<p>(iv)\u00a0 URL(URL context, String spec)<\/p>\n<p>&nbsp;<\/p>\n<p>All constructors throw MalformedURLExce ption<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Parsing a URL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Once a URL object is created, we can parse the URL and retrieve the individual components in the URL like what protocol is mentioned, the name of the host, port,file mentioned in the URL, and reference if present in the URL.<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0 String getProtocol()<\/p>\n<p>2.\u00a0\u00a0\u00a0 String getHost()<\/p>\n<p>3.\u00a0\u00a0\u00a0 int getPort()<\/p>\n<p>4.\u00a0\u00a0\u00a0 String getFile()<\/p>\n<p>5.\u00a0\u00a0\u00a0 String getRef()<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example of ParseURL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import java.net.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class ParseURL<\/p>\n<p>{<\/p>\n<p>public static void main(String args[]) throws Exception {<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">URL u = new URL(&#8220;http:\/\/java.sun.com\/docs\/books\/tutorial\/#down &#8220;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;protocol =&#8221; + u.getProtocol());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;host =&#8221; + u.getHost());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;filename =&#8221; + u.getFile());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;port =&#8221; + u.getPort());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;ref =&#8221;+\u00a0 u.getRef());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">This example demonstrates about the instance methods of URL. The method <em>getProtocol()<\/em> retrieves the protocol from the URL. The method <em>getHost()<\/em> retrieves the hostname from the URL. The method <em>getFile() <\/em>retrieves the filename, the method<em> getPort() <\/em>retrieves the port no if exists in the URL and<em> getRef() <\/em>retrieves the reference from the URL.<\/p>\n<p>&nbsp;<\/p>\n<p>The output of this code is shown in Figure 20.3.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output of ParseURL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-178 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103.png\" alt=\"\" width=\"609\" height=\"355\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103.png 609w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103-300x175.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103-225x131.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-103-350x204.png 350w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module discusses about the basics for Internet Programming. This module also explores about working with InetAddress class for obtaining the Internet Address and also gives an idea about URL class and the URLConnection class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<ul>\n<li>Herbert Schildt, &#8221; <em>Java: The Complete Reference&#8221;, 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\n<\/ul>\n","protected":false},"author":4,"menu_order":18,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-174","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":10,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/174\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/174\/revisions\/553"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/174\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=174"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=174"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}