{"id":25,"date":"2018-07-10T07:07:43","date_gmt":"2018-07-10T07:07:43","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=25"},"modified":"2019-05-15T06:51:20","modified_gmt":"2019-05-15T06:51:20","slug":"introduction-to-networking-and-socket-api","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/introduction-to-networking-and-socket-api\/","title":{"rendered":"Introduction to Networking and Socket API"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/ZKz6LiRrU8Y\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div class=\"toolbar\">\r\n<div id=\"toolbarContainer\">\r\n<div id=\"toolbarViewer\">\r\n<div id=\"toolbarViewerLeft\">\r\n<div><\/div>\r\n<div class=\"splitToolbarButton\" style=\"text-align: center;\"><strong style=\"text-align: initial; font-size: 1em;\">Networking in Java<\/strong><\/div>\r\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">What \u00a0is \u00a0networking?<\/strong><\/p>\r\n&nbsp;\r\n\r\nConnecting more than one machines. Machines are hardware. The purpose of networking is to have communication \u00a0between \u00a0two \u00a0processes \u00a0which \u00a0may \u00a0be on different \u00a0machines.\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">What is required for Networking?<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Hardware \u00a0like \u00a0connectors, \u00a0cables, \u00a0hub, \u00a0NIC \u00a0etc. - Physical \u00a0Layer<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">What is the role of NIC?<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify;\"><span style=\"font-size: 1em;\">It converts signals to digital data and \u00a0data \u00a0to \u00a0signals \u00a0which \u00a0is \u00a0transmitted \u00a0on the cable or any other media like wireless, etc.<\/span><\/li>\r\n \t<li style=\"text-align: justify;\">The code for converting data to signals and vice versa is in the ROM of NIC - Firmware - DataLink Layer Each NIC has a 48-bit MAC-address by which it is identified. This MAC address is divided into 2 parts. Initial 24-bits identify the Manufacturer and the rest 24- bits are the serial number assigned by the manufacturer. From user's perspective, the user does not identify the NIC card. The user will identify \u00a0the host \u00a0Machines.<\/li>\r\n \t<li style=\"text-align: justify;\">Some way of identifying hosts which form a network - Network Layer This would be managed by the OS. Various options\/protocols are available for Network layer. The most common being IP protocol.<\/li>\r\n<\/ol>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">There are various version of the IP protocol. The commonly used versions are IPv4 and IPv6. In the IPv4 protocol, each machine is identified by a 32-bit (128-bit in case of IPv6) value assigned to each node participating in the network. This 32-bit value is known as the IP-address of the machine. It is denoted in decimal-dotted notation by writing the values of each byte separated by dot('.') e.g. 192.168.88.1. The 32-bits of an IP-address are divided into 2 parts, Some initial most-significant bits are used to identify the network and rest of the bits are used to identify the host within the network. In the host bits there are two reserved values. All zeros for host is reserved for specifying the network address and all ones is reserved for the broadcast address. The number of bits used for the network is specified by using a subnet-mask. E.g. if in the above IP-address we want to specify 10-bits for network, then the subnet-mask would be 255.192.0.0. The network address for above IP-address would be 192.128.0.0. The broadcast address will be 192.191.255.255.<\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<div id=\"viewerContainer\">\r\n<div id=\"viewer\" class=\"pdfViewer\">\r\n<div id=\"pageContainer1\" class=\"page\">\r\n<div class=\"textLayer\">\r\n<p style=\"text-align: justify;\">The purpose of networking is to have communication between 2 processes.<\/p>\r\n\r\n<ol start=\"4\">\r\n \t<li>Some means of exchanging bytes between processes on different machines - \u00a0Transport \u00a0Layer<\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\nHere there are 2 commonly used protocols: TCP(Transmission Control Protocol) and UDP(User Datagram Protocol). TCP is a stream based protocol whereas UDP is a packet based protocol. In case of UDP the sender prepares a packet containing bytes for the receiver. In case of TCP there is a stream available on which bytes can be written or read from. For a process to start using any of the above protocols, it has to obtain port number (which is a resource with the OS). The port number is a 16-bit value. A valid port number is in the range of 1 to 65535.\r\n<ol start=\"5\">\r\n \t<li style=\"text-align: justify;\">The transport layer provides a means for sending and receiving bytes between 2 processes, but that is not sufficient for communication between processes. The communication between 2 processes would require that the bytes of data exchanged between 2 processes can have a meaning for the 2 processes. The bytes of data are according to some format and convey some meaningful data which may be interpreted and used by the receiver. There has to be a pre-decided format according to which these bytes would be exchanged. The layer which defines some kinds of rules and format for the bytes being exchanged between 2 processes \u2013 Application Layer<\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">There are many protocols at the application layer which use TCP or UDP at the transport layer. The most common protocols are the HTTP, SMTP, POP3, FTP, DNS, tFTP etc.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">DNS and tFTP protocols use UDP for \u00a0communication \u00a0between \u00a0the \u00a0client \u00a0and the server, whereas all the other protocols mentioned use TCP.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The HTTP protocol is the most commonly used protocol for communication between the browser and the server. The default port number used by \u00a0the HTTP protocol is 80.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">Protocol Stack<\/strong><\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<\/div>\r\n<div id=\"pageContainer6\" class=\"page\">\r\n<div class=\"loadingIcon\"><img class=\"size-full wp-image-26 aligncenter\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic.jpg\" alt=\"\" width=\"800\" height=\"600\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<div><\/div>\r\n<div style=\"text-align: center;\"><strong style=\"text-align: initial; font-size: 1em;\">JAVA API for networking<\/strong><\/div>\r\n<p style=\"text-align: left;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Inet Address class.<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">In Java, the classes related to networking are in the java.net package. The class InetAddress is used for representing the Host in any IP based protocol. An instance of InetAddress encapsulates information about an IP address and its \u00a0host \u00a0name. No public constructor to create any instance of this class are\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">available. Instead, we use the public and static factory methods to get instances of this class.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">Methods<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public static InetAddress getByName(String name)<\/span><\/li>\r\n \t<li style=\"text-align: justify;\">public static InetAddress getByAddress(byte[] address)<\/li>\r\n \t<li style=\"text-align: justify;\">public static InetAddress getByAddress(String host, byte[] address)<\/li>\r\n \t<li style=\"text-align: justify;\">public static InetAddress getLocalHost()<\/li>\r\n \t<li style=\"text-align: justify;\">public static InetAddress[] getAllByName()<\/li>\r\n \t<li style=\"text-align: justify;\">public byte[] getAddress()<\/li>\r\n \t<li style=\"text-align: justify;\"><span style=\"text-align: initial; text-indent: 1em; font-size: 1em;\">public String getHostAddress()<\/span><\/li>\r\n \t<li style=\"text-align: justify;\">public String getCanonicalHostName()<\/li>\r\n \t<li style=\"text-align: justify;\">public String getHostName()<\/li>\r\n<\/ol>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\">The <\/strong><span style=\"text-align: justify; text-indent: -1em; font-size: 1em;\">getByName()<\/span><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\"> method may be used to resolve an IP address from the name, e.g. we may use the method as given below to resolve the IP address of any <\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\">host:<\/strong>InetAddress add = InetAddress.getAddressByName(\"mail.yahoo.com\");\r\n\r\nSystem.out.println(Arrays.toString(add.getAddress()));\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong>The <\/strong>getByAddress() <strong>method may be used to resolve the name of the host by using its IP address, e.g. we may use the method as given below to resolve the name by using its IP address:<\/strong><\/p>\r\n&nbsp;\r\n\r\nbyte[] ipaddress = byte[]202,164,27,54;\r\n\r\nInetAddress add = InetAddress.getByAddress(ipaddress);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The getByAddress() with two parameters creates an instance of the InetAddress with the specified host name and IP address. This method does not carry out any check with the name server and does not attempt any kind of name or address resolution. The getLocalHost() creates an instance of the InetAddress class corresponding to the local host. The last static method getAllbyName() returns an array containing the InetAddress instances corresponding to all the IP addresses configured on the specified host name. An instance of InetAddress encapsulates the values of the IP address and the name of the host. The methods getAddress() and getHostName() return the encapsulated values of the IP address and the host name. The method getCanonicalHostName() returns the full domain name of the host whose IP address it represents. The getHostAddress() returns in String from the IP address value. For IPv4 this would be the four byte values in decimal format separated by dots.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">UsingSocketandServerSocketforClient\u2013ServerCommunicationSequence of steps:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">In Java, The Socket and the <\/strong><span style=\"font-size: 1em;\">ServerSocket <\/span><strong style=\"font-size: 1em;\">classes are used to have a TCP\/IP communication between a client and a Server. The following are the steps required to establishing the connection between the \u00a0client\u00a0 and \u00a0a \u00a0server \u00a0using the \u00a0TCP\/IP \u00a0protocol.<\/strong><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Server-Side<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">The steps on the server side are as follows:<\/span><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">The Server process reserves a port for itself. This is done by calling the constructor of <\/strong><span style=\"text-align: initial; font-size: 1em;\">ServerSocket.\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">e.g. ServerSocket ss = new ServerSocket(portno);<\/span><\/li>\r\n \t<li style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">The Server starts listening for connection from client. This is done by calling the <\/strong><span style=\"font-size: 1em;\">accept()<\/span><strong style=\"font-size: 1em;\"> method on the<\/strong><span style=\"font-size: 1em;\"> ServerSocket. accept() <\/span><strong style=\"font-size: 1em;\">is a blocking method, it would wait till a client connects. As soon as the client connects the <\/strong><span style=\"font-size: 1em;\">accept()<\/span><strong style=\"font-size: 1em;\"> method returns an instance of Socket, which is a connection to the client who\u00a0 has connected.<\/strong><\/li>\r\n<\/ol>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">e.g.\u00a0 \u00a0Socket\u00a0\u00a0 socket\u00a0\u00a0 = ss.accept() \/\/ the server is waiting<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Client-side<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The Steps on the client side is as follows:<\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify;\"><strong>The client connects\u00a0\u00a0 to \u00a0the \u00a0server \u00a0by \u00a0calling\u00a0 \u00a0the \u00a0constructor of\u00a0\u00a0<\/strong>Socket <strong>class, where he specifies the host and the <\/strong>portno <strong>of the server.<\/strong><\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\ne.g. Socket socket = new Socket(ipaddress, portno)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong>This would create a Socket on the client side, as well as on the server-side the accept method returns the socket. So two connected sockets are created simultaneously, one on the client side by using a constructor, and other on the server side with the accept method.<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The connected socket has an InputStream and OutputStream, is used for communicating with the remote \u00a0process.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><strong>Diagram<\/strong><\/p>\r\n\r\n<div id=\"viewerContainer\">\r\n<div id=\"viewer\" class=\"pdfViewer\">\r\n<div><\/div>\r\n<div id=\"pageContainer7\" class=\"page\"><img class=\"aligncenter wp-image-27 size-large\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-1024x777.jpg\" alt=\"\" width=\"1024\" height=\"777\" \/><\/div>\r\n&nbsp;\r\n<p class=\"page hanging-indent\"><strong style=\"font-size: 1em;\">Methods of the Socket class<\/strong><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<div>\r\n<ol>\r\n \t<li>public InputStream getInputStream()<\/li>\r\n \t<li>public OutputStream getOutputStream()<\/li>\r\n \t<li>public InetAddress getHost()<\/li>\r\n \t<li>public int getPort()<\/li>\r\n \t<li>public InetAddress getLocalHost()<\/li>\r\n \t<li>public int getLocalPort()<\/li>\r\n<\/ol>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong>The <\/strong>getInputStream()<strong> and the<\/strong> getOutputStream() <strong>methods are used to obtain the <\/strong>InputStream <strong>and the <\/strong>OutputStream <strong>of the Socket instance, which are connected to the remote Socket. These are used for sending to and receiving bytes from the connected Socket.<\/strong><\/p>\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Introduction to Networking and Socket API<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/ZKz6LiRrU8Y\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson\u00a0<\/span>Education.<\/li>\r\n \t<li style=\"text-align: justify;\">The class of JAVA by Pravin Jain, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.<\/li>\r\n \t<li style=\"text-align: justify;\">https:\/\/docs.oracle.com\/javase\/tutorial\/networking\/overview\/index.html<\/li>\r\n<\/ol>\r\n<\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/ZKz6LiRrU8Y\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div class=\"toolbar\">\n<div id=\"toolbarContainer\">\n<div id=\"toolbarViewer\">\n<div id=\"toolbarViewerLeft\">\n<div><\/div>\n<div class=\"splitToolbarButton\" style=\"text-align: center;\"><strong style=\"text-align: initial; font-size: 1em;\">Networking in Java<\/strong><\/div>\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">What \u00a0is \u00a0networking?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Connecting more than one machines. Machines are hardware. The purpose of networking is to have communication \u00a0between \u00a0two \u00a0processes \u00a0which \u00a0may \u00a0be on different \u00a0machines.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">What is required for Networking?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Hardware \u00a0like \u00a0connectors, \u00a0cables, \u00a0hub, \u00a0NIC \u00a0etc. &#8211; Physical \u00a0Layer<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">What is the role of NIC?<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify;\"><span style=\"font-size: 1em;\">It converts signals to digital data and \u00a0data \u00a0to \u00a0signals \u00a0which \u00a0is \u00a0transmitted \u00a0on the cable or any other media like wireless, etc.<\/span><\/li>\n<li style=\"text-align: justify;\">The code for converting data to signals and vice versa is in the ROM of NIC &#8211; Firmware &#8211; DataLink Layer Each NIC has a 48-bit MAC-address by which it is identified. This MAC address is divided into 2 parts. Initial 24-bits identify the Manufacturer and the rest 24- bits are the serial number assigned by the manufacturer. From user&#8217;s perspective, the user does not identify the NIC card. The user will identify \u00a0the host \u00a0Machines.<\/li>\n<li style=\"text-align: justify;\">Some way of identifying hosts which form a network &#8211; Network Layer This would be managed by the OS. Various options\/protocols are available for Network layer. The most common being IP protocol.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">There are various version of the IP protocol. The commonly used versions are IPv4 and IPv6. In the IPv4 protocol, each machine is identified by a 32-bit (128-bit in case of IPv6) value assigned to each node participating in the network. This 32-bit value is known as the IP-address of the machine. It is denoted in decimal-dotted notation by writing the values of each byte separated by dot(&#8216;.&#8217;) e.g. 192.168.88.1. The 32-bits of an IP-address are divided into 2 parts, Some initial most-significant bits are used to identify the network and rest of the bits are used to identify the host within the network. In the host bits there are two reserved values. All zeros for host is reserved for specifying the network address and all ones is reserved for the broadcast address. The number of bits used for the network is specified by using a subnet-mask. E.g. if in the above IP-address we want to specify 10-bits for network, then the subnet-mask would be 255.192.0.0. The network address for above IP-address would be 192.128.0.0. The broadcast address will be 192.191.255.255.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"viewerContainer\">\n<div id=\"viewer\" class=\"pdfViewer\">\n<div id=\"pageContainer1\" class=\"page\">\n<div class=\"textLayer\">\n<p style=\"text-align: justify;\">The purpose of networking is to have communication between 2 processes.<\/p>\n<ol start=\"4\">\n<li>Some means of exchanging bytes between processes on different machines &#8211; \u00a0Transport \u00a0Layer<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Here there are 2 commonly used protocols: TCP(Transmission Control Protocol) and UDP(User Datagram Protocol). TCP is a stream based protocol whereas UDP is a packet based protocol. In case of UDP the sender prepares a packet containing bytes for the receiver. In case of TCP there is a stream available on which bytes can be written or read from. For a process to start using any of the above protocols, it has to obtain port number (which is a resource with the OS). The port number is a 16-bit value. A valid port number is in the range of 1 to 65535.<\/p>\n<ol start=\"5\">\n<li style=\"text-align: justify;\">The transport layer provides a means for sending and receiving bytes between 2 processes, but that is not sufficient for communication between processes. The communication between 2 processes would require that the bytes of data exchanged between 2 processes can have a meaning for the 2 processes. The bytes of data are according to some format and convey some meaningful data which may be interpreted and used by the receiver. There has to be a pre-decided format according to which these bytes would be exchanged. The layer which defines some kinds of rules and format for the bytes being exchanged between 2 processes \u2013 Application Layer<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">There are many protocols at the application layer which use TCP or UDP at the transport layer. The most common protocols are the HTTP, SMTP, POP3, FTP, DNS, tFTP etc.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">DNS and tFTP protocols use UDP for \u00a0communication \u00a0between \u00a0the \u00a0client \u00a0and the server, whereas all the other protocols mentioned use TCP.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The HTTP protocol is the most commonly used protocol for communication between the browser and the server. The default port number used by \u00a0the HTTP protocol is 80.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">Protocol Stack<\/strong><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<div id=\"pageContainer6\" class=\"page\">\n<div class=\"loadingIcon\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-26 aligncenter\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic.jpg\" alt=\"\" width=\"800\" height=\"600\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic.jpg 800w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic-300x225.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic-768x576.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic-65x49.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic-225x169.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic-350x263.jpg 350w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div style=\"text-align: center;\"><strong style=\"text-align: initial; font-size: 1em;\">JAVA API for networking<\/strong><\/div>\n<p style=\"text-align: left;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Inet Address class.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">In Java, the classes related to networking are in the java.net package. The class InetAddress is used for representing the Host in any IP based protocol. An instance of InetAddress encapsulates information about an IP address and its \u00a0host \u00a0name. No public constructor to create any instance of this class are\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">available. Instead, we use the public and static factory methods to get instances of this class.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">Methods<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public static InetAddress getByName(String name)<\/span><\/li>\n<li style=\"text-align: justify;\">public static InetAddress getByAddress(byte[] address)<\/li>\n<li style=\"text-align: justify;\">public static InetAddress getByAddress(String host, byte[] address)<\/li>\n<li style=\"text-align: justify;\">public static InetAddress getLocalHost()<\/li>\n<li style=\"text-align: justify;\">public static InetAddress[] getAllByName()<\/li>\n<li style=\"text-align: justify;\">public byte[] getAddress()<\/li>\n<li style=\"text-align: justify;\"><span style=\"text-align: initial; text-indent: 1em; font-size: 1em;\">public String getHostAddress()<\/span><\/li>\n<li style=\"text-align: justify;\">public String getCanonicalHostName()<\/li>\n<li style=\"text-align: justify;\">public String getHostName()<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\">The <\/strong><span style=\"text-align: justify; text-indent: -1em; font-size: 1em;\">getByName()<\/span><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\"> method may be used to resolve an IP address from the name, e.g. we may use the method as given below to resolve the IP address of any <\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\">host:<\/strong>InetAddress add = InetAddress.getAddressByName(&#8220;mail.yahoo.com&#8221;);<\/p>\n<p>System.out.println(Arrays.toString(add.getAddress()));<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>The <\/strong>getByAddress() <strong>method may be used to resolve the name of the host by using its IP address, e.g. we may use the method as given below to resolve the name by using its IP address:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>byte[] ipaddress = byte[]202,164,27,54;<\/p>\n<p>InetAddress add = InetAddress.getByAddress(ipaddress);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The getByAddress() with two parameters creates an instance of the InetAddress with the specified host name and IP address. This method does not carry out any check with the name server and does not attempt any kind of name or address resolution. The getLocalHost() creates an instance of the InetAddress class corresponding to the local host. The last static method getAllbyName() returns an array containing the InetAddress instances corresponding to all the IP addresses configured on the specified host name. An instance of InetAddress encapsulates the values of the IP address and the name of the host. The methods getAddress() and getHostName() return the encapsulated values of the IP address and the host name. The method getCanonicalHostName() returns the full domain name of the host whose IP address it represents. The getHostAddress() returns in String from the IP address value. For IPv4 this would be the four byte values in decimal format separated by dots.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">UsingSocketandServerSocketforClient\u2013ServerCommunicationSequence of steps:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">In Java, The Socket and the <\/strong><span style=\"font-size: 1em;\">ServerSocket <\/span><strong style=\"font-size: 1em;\">classes are used to have a TCP\/IP communication between a client and a Server. The following are the steps required to establishing the connection between the \u00a0client\u00a0 and \u00a0a \u00a0server \u00a0using the \u00a0TCP\/IP \u00a0protocol.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Server-Side<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">The steps on the server side are as follows:<\/span><\/p>\n<ol>\n<li style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">The Server process reserves a port for itself. This is done by calling the constructor of <\/strong><span style=\"text-align: initial; font-size: 1em;\">ServerSocket.\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">e.g. ServerSocket ss = new ServerSocket(portno);<\/span><\/li>\n<li style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">The Server starts listening for connection from client. This is done by calling the <\/strong><span style=\"font-size: 1em;\">accept()<\/span><strong style=\"font-size: 1em;\"> method on the<\/strong><span style=\"font-size: 1em;\"> ServerSocket. accept() <\/span><strong style=\"font-size: 1em;\">is a blocking method, it would wait till a client connects. As soon as the client connects the <\/strong><span style=\"font-size: 1em;\">accept()<\/span><strong style=\"font-size: 1em;\"> method returns an instance of Socket, which is a connection to the client who\u00a0 has connected.<\/strong><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">e.g.\u00a0 \u00a0Socket\u00a0\u00a0 socket\u00a0\u00a0 = ss.accept() \/\/ the server is waiting<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Client-side<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The Steps on the client side is as follows:<\/p>\n<ol>\n<li style=\"text-align: justify;\"><strong>The client connects\u00a0\u00a0 to \u00a0the \u00a0server \u00a0by \u00a0calling\u00a0 \u00a0the \u00a0constructor of\u00a0\u00a0<\/strong>Socket <strong>class, where he specifies the host and the <\/strong>portno <strong>of the server.<\/strong><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>e.g. Socket socket = new Socket(ipaddress, portno)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>This would create a Socket on the client side, as well as on the server-side the accept method returns the socket. So two connected sockets are created simultaneously, one on the client side by using a constructor, and other on the server side with the accept method.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The connected socket has an InputStream and OutputStream, is used for communicating with the remote \u00a0process.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>Diagram<\/strong><\/p>\n<div>\n<div class=\"pdfViewer\">\n<div><\/div>\n<div id=\"pageContainer7\" class=\"page\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-27 size-large\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-1024x777.jpg\" alt=\"\" width=\"1024\" height=\"777\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-1024x777.jpg 1024w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-300x228.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-768x583.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-65x49.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-225x171.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2-350x266.jpg 350w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M02Pic2.jpg 1028w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div>\n<p>&nbsp;<\/p>\n<p class=\"page hanging-indent\"><strong style=\"font-size: 1em;\">Methods of the Socket class<\/strong><\/p>\n<\/div>\n<\/div>\n<div>\n<ol>\n<li>public InputStream getInputStream()<\/li>\n<li>public OutputStream getOutputStream()<\/li>\n<li>public InetAddress getHost()<\/li>\n<li>public int getPort()<\/li>\n<li>public InetAddress getLocalHost()<\/li>\n<li>public int getLocalPort()<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>The <\/strong>getInputStream()<strong> and the<\/strong> getOutputStream() <strong>methods are used to obtain the <\/strong>InputStream <strong>and the <\/strong>OutputStream <strong>of the Socket instance, which are connected to the remote Socket. These are used for sending to and receiving bytes from the connected Socket.<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Introduction to Networking and Socket API<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/ZKz6LiRrU8Y\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson\u00a0<\/span>Education.<\/li>\n<li style=\"text-align: justify;\">The class of JAVA by Pravin Jain, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.<\/li>\n<li style=\"text-align: justify;\">https:\/\/docs.oracle.com\/javase\/tutorial\/networking\/overview\/index.html<\/li>\n<\/ol>\n<\/div>\n","protected":false},"author":4,"menu_order":2,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-25","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":40,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/25\/revisions"}],"predecessor-version":[{"id":385,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/25\/revisions\/385"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/25\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=25"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=25"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}