Network Programming Introduction Based on Classes in the java.net package 9/13/2018 V.B.Sanghavi
JAVA Networking API series of layered abstractions basic networking capabilities are split between the java.net and the java.io defining custom protocols, using existing protocols, or building a higher-level API on top of the basic services 9/13/2018 V.B.Sanghavi
Low Level API Addresses, which are networking identifiers, like IP addresses. Sockets, which are basic bidirectional data communication mechanisms. Interfaces, which describe network interfaces. 9/13/2018 V.B.Sanghavi
High Level API URIs, which represent Universal Resource Identifiers. URLs, which represent Universal Resource Locators. Connections, which represents connections to the resource pointed to by URLs. 9/13/2018 V.B.Sanghavi
Network Terminology IP Address Connection-oriented (TCP) and connectionless protocol (UDP) Protocol Socket –allows a single comp. to serve diff.clients at once. Port Number –numbered socket Domain Naming Service(DNS) 9/13/2018 V.B.Sanghavi
Client-server Architecture 9/13/2018 V.B.Sanghavi
Networking Classes InetAddress Socket ServerSocket URL URLConnection DatagramSocket DatagramPacket 9/13/2018 V.B.Sanghavi
InetAddress Encapsulates the Numerical IP address and domain name for that address. Interact with this class by hostname as it is more convenient. Can handle IPv4 and IPv6 addresses. 9/13/2018 V.B.Sanghavi
Factory Methods static InetAddress getLocalHost() throws UnknownHostException static InetAddress getByName(String hostname) throws UnknownHostException Static InetAddress[] getAllByName() throws UnknownHostException 9/13/2018 V.B.Sanghavi
static InetAddress getByAddress() throws UnknownHostException 9/13/2018 V.B.Sanghavi
Instance Methods Boolean equals(Object o) Byte[] getAddress() String getHostAddress() String getHostName() Boolean isMulticastAddress() String toString() 9/13/2018 V.B.Sanghavi
Socket Programming for communication between the machines. The client in socket programming must know two information: IP address Port number 9/13/2018 V.B.Sanghavi
Socket class endpoint for communications between the machines. create a socket to connect to a server sockets and initiate protocol exchange. Constructor Socket (String host, int port) Socket ( InetAddress ip, int port) program 9/13/2018 V.B.Sanghavi
- inetAddress getInetAddress() int getPort() int getLocalPort() Methods : - inetAddress getInetAddress() int getPort() int getLocalPort() 9/13/2018 V.B.Sanghavi
- public InputStream getInputStream() Socket class Methods: - public InputStream getInputStream() - public OutputStream getOutputStream() - void close() 9/13/2018 V.B.Sanghavi
ServerSocket class to create a server socket that listen for local or remote client program. Constructor: ServerSocket (int port) ServerSocket (int port, int maxqueue) ServerSocket ( int port, int maxqueue, InetAddress i) program 9/13/2018 V.B.Sanghavi
- public Socket accept() - public InputStream getInputStream() Methods: - public Socket accept() - public InputStream getInputStream() - public OutputStream getOutputStream() - void close() 9/13/2018 V.B.Sanghavi
URL class represents a URL (Uniform Resource Locator) to uniquely identify or address info. On the internet. URLs can point to files, Web sites, ftp sites, newsgroups, email addresses, and other resources. 9/13/2018 V.B.Sanghavi
Format of URL http://www.javatpoint.com : 8080/abc / index.jsp telnet://localhost:8000/ 9/13/2018 V.B.Sanghavi
URL constructors URL (String urlspecifier) URL ( String protocol, String host, int port, String path) Exception : MalformedURLException 9/13/2018 V.B.Sanghavi
Methods of URL class public String getProtocol() public String getHost() public String getPort() public String getFile() Public String toExternalForm() Program 9/13/2018 V.B.Sanghavi
URLConnection represents a communication link between the URL and the application. can be used to read and write data to the specified resource reffered by the URL. 9/13/2018 V.B.Sanghavi
Creating a URLConnection URL url = new URL ( some_url ); URLConnection connection = url.openConnection(); 9/13/2018 V.B.Sanghavi
public String getContentType() public long getExpiration() public long getDate() public String getContentType() public long getExpiration() public long getLastModified() public int getContentLength() public InputStream getInputStream() public OutputStream getOutputStream() 9/13/2018 V.B.Sanghavi
Program for URLConnection class to read the content of specified resource 9/13/2018 V.B.Sanghavi
Datagrams Bundles of information passed between machines. When released- no assurance regarding arrival. When received- no assurance regarding data reliability. 9/13/2018 V.B.Sanghavi
DatagramSocket class represents a connection-less socket for sending and receiving datagram packets Constructors DatagramSocket() //Sending DatagramSocket(int port) //receiving DatagramSocket(int port, InetAddress address) 9/13/2018 V.B.Sanghavi
DatagramPacket class The DatagramPacket is message that can be sent or received 9/13/2018 V.B.Sanghavi
Constructors DatagramPacket(byte[] data, int length) (While receiving the data) DatagramPacket(byte[] data, int length, InetAddress address, int port) (while sending the data) 9/13/2018 V.B.Sanghavi
Methods: InetAddress getAddress() Int getPort() byte[] getData() Int getLength() 9/13/2018 V.B.Sanghavi
Content Handler and Protocol Handler Handling a protocol taking care of the interaction between a client and a server generating requests in the correct format acknowledging that the data has been received 9/13/2018 V.B.Sanghavi
Handling the content converting the raw data into a format Java understands 9/13/2018 V.B.Sanghavi
ContentHandler implemented as subclasses of the ContentHandler getContent() associated with a specific MIME type through the use of the ContentHandlerFactory interface createContentHandler() 9/13/2018 V.B.Sanghavi
ProtocolHandler implemented as subclasses of the URLStreamHandler class openConnection() toExternalForm() to implement a custom protocol needed to access Web objects identified by URLs 9/13/2018 V.B.Sanghavi
createURLStreamHandler() associated with a specific protocol through the use of the URLStreamHandlerFactory createURLStreamHandler() 9/13/2018 V.B.Sanghavi