Unit-4 Networking
Unit-4 Networking
• It separates the client system and web server from the global network.
• In other words, we can say that the proxy server allows us to access any
websites with a different IP address.
Authenticator The class Authenticator represents an object that knows how to obtain
authentication for a network connection.
CacheRequest Represents channels for storing resources in the ResponseCache.
ContentHandler The abstract class ContentHandler is the superclass of all classes that read
an Object from a URLConnection.
CookieHandler A CookieHandler object provides a callback mechanism to hook up a HTTP state
management policy implementation into the HTTP protocol handler.
CookieManager CookieManager provides a concrete implementation of CookieHandler, which
separates the storage of cookies from the policy surrounding accepting and
rejecting cookies.
DatagramPacket This class represents a datagram packet.
DatagramSocket This class represents a socket for sending and receiving datagram packets.
HttpCookie An HttpCookie object represents an HTTP cookie, which carries state information
between server and user agent.
HttpURLConnection A URLConnection with support for HTTP-specific features.
InetSocketAddress This class implements an IP Socket Address (IP address + port number) It can
also be a pair (hostname + port number), in which case an attempt will be
made to resolve the hostname.
InterfaceAddress This class represents a Network Interface address.
JarURLConnection A URL Connection to a Java ARchive (JAR) file or an entry in a JAR file.
MulticastSocket The multicast datagram socket class is useful for sending and receiving IP
multicast packets.
NetPermission This class is for various network permissions.
NetworkInterface This class represents a Network Interface made up of a name, and a list of IP
addresses assigned to this interface.
PasswordAuthentication The class PasswordAuthentication is a data holder that is used by
Authenticator.
Proxy This class represents a proxy setting, typically a type (http, socks) and a socket
address.
ProxySelector Selects the proxy server to use, if any, when connecting to the network
resource referenced by a URL.
ResponseCache Represents implementations of URLConnection caches.
SecureCacheResponse Represents a cache response originally retrieved through secure means, such
as TLS.
ServerSocket This class implements server sockets.
Socket This class implements client sockets (also called just "sockets").
SocketAddress This class represents a Socket Address with no protocol
attachment.
SocketImpl The abstract class SocketImpl is a common superclass of all classes that
actually implement sockets.
SocketPermission This class represents access to a network via sockets.
URLConnection The abstract class URLConnection is the superclass of all classes that
represent a communications link between the application and a URL.
• InetAddress
• The InetAddress class is used to encapsulate both the numerical IP
address and the domain name for that address.
• We interact with this class by using the name of an IP host, which is more
convenient and understandable than its IP address.
•
• InetAddressClass
static methods you can use to create new
InetAddress objects.
• –getByName(String host)
• –getAllByName(String host)
• –getLocalHost()
• InetAddress x = InetAddress.getByName( “msbte.com”);
• Throws UnknownHostException
• Factory Methods
• byte[ ] getAddress( )
• –Returns the IP address in byte format.
• String getHostAddress( )
• –Returns the IP address in dotted decimal format.
• String getHostName( )
• –Returns the hostname of the InetAddress.
• boolean isMulticastAddress( )
• –Returns true if the InetAddress is a multicast address (class D address).
• String toString()
• –Converts this IP address to a String.
class InetAddressTest1
{
public static void main(String args[])throws UnknownHostException
{
InetAddress Address = InetAddress.getByName("www.google.com");
System.out.println(Address.getHostAddress());
System.out.println(Address.getHostName());
if(Address.isMulticastAddress())
System.out.println("It is multicast address");
}
}
216.58.203.4
www.google.com
Server socket
• The constructors used to server socket are given below. All of them throw IO
Exception
• ServerSocketChannel getChannel ()
– Returns the unique ServerSocketChannel object associated with this socket, if any.
• InetAddress getInetAddress()
– Returns the local address of this server socket.
• int getLocalPort() -Returns the port number on which this socket is listening.
• SocketAddress getLocalSocketAddress()
– Returns the address of the endpoint this socket is bound to.
• int getReceiveBufferSize()
– Gets the value of the SO_RCVBUF option for this ServerSocket, that is the proposed buffer size that
will be used for Sockets accepted from this ServerSocket.
Client socket
• The constructors used to serve socket are given below. All of them throw IO
Exception
• InetAddress getLocalAddress()
– Gets the local address to which the socket is bound.
• int getLocalPort()
• Returns the local port number to which this socket is bound.
• SocketAddress getLocalSocketAddress()
– Returns the address of the endpoint this socket is bound to.
• boolean getOOBInline ()---Tests if SO_OOBINLINE is enabled.
• OutputStream getOutputStream ()----Returns an output stream for this socket.
• int getPort()--Returns the remote port number to which this socket is connected.
• int getReceiveBufferSize()Gets the value of the SO_RCVBUF option for
this Socket, that is the buffer size used by the platform for input on this Socket.
• SocketAddress getRemoteSocketAddress()Returns the address of the endpoint
this socket is connected to, or null if it is unconnected.
• boolean getReuseAddress()-----Tests if SO_REUSEADDR is enabled.
• int getSendBufferSize()
– Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform
for output on this Socket.
• int getSoLinger()----Returns setting for SO_LINGER.
• int getSoTimeout()----Returns setting for SO_TIMEOUT.
• boolean getTcpNoDelay()----Tests if TCP_NODELAY is enabled.
• int getTrafficClass()Gets traffic class or type-of-service in the IP header for packets
sent from this Socket
• booleanisBound()Returns the binding state of the socket.
• void setReceiveBufferSize(int size)Sets the SO_RCVBUF option to the specified value for this Socket.
• void setReuseAddress(boolean on)Enable/disable the SO_REUSEADDR socket option.
• void setSendBufferSize(int size)Sets the SO_SNDBUF option to the specified value for this Socket.
• static void setSocketImplFactory(SocketImplFactory fac)
– Sets the client socket implementation factory for the application.
• void setSoLinger(boolean on, int linger)
– Enable/disable SO_LINGER with the specified linger time in seconds.
• void setSoTimeout(int timeout)Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
• void setTcpNoDelay(boolean on)Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
• void setTrafficClass(int tc)Sets traffic class or type-of-service octet in the IP header for packets sent from
this Socket.
• void shutdownInput()Places the input stream for this socket at "end of stream".
• void shutdownOutput()Disables the output stream for this socket.
• StringtoString()Converts this socket to a String.
Programming TCP Client-Server in Java
Socket MyClient;
try {
MyClient = new Socket("Machine name", PortNumber);
} catch (IOException e) {
System.out.println(e);
}
File: MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try {
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
Programs
URL class
Constructors of URL class
•The abstract class URLConnection is the superclass of all classes that represent a
communications link between the application and a URL.
Instances of this class can be used both to read from and to write to the resource
referenced by the URL.
• Java implements datagrams on top of the UDP (User Datagram Protocol) protocol
by using two classes:
• DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port) :
– It transmits packets beginning at the specified offset into the data.
import java.net.*;
}
}
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.
• The end