Networking Basic
0 Java networking supports the concept of Socket.
0 Socket
0 Identifies an endpoint in a n/w.
0 Allows a single computer to serve many different type of
information to many different client as well as server at
once.
0 This is accomplished through port(numbered socket on
a particular machine).
Networking Basic
0 Socket
0 Socket communication takes place via protocol
0IP
0 Low-level routing protocol
0 Breaks data into small packets and send them to an address across n/w
0 No guarantee to delivered.
0TCP
0 Higher level protocol
0 Manage packet robustly
0 Sort and retransmit them as necessary
0 Reliably transmit data
0UDP
0 Used directly to support fast ,connectionless , unreliable transport
packets.
InetAddress
0 Encapsulate both the numerical IP Address and the
domain name for the address.
0 Can handle IPv4 and IPv6
0 Factory Method
0 InetAddress class does not have visible constructor
0 To create an InetAddress object, you have to use one of
the available factory methods.
InetAddress
0 Factory Methods
0 static InetAddress getLocalHost( )throws
UnknownHostException
0 static InetAddress getByName(String hostName) throws
UnknownHostException
0 static InetAddress[ ] getAllByName(String hostName)
throws UnknownHostException
TCP/IP Sockets
0 Used to implement reliable, bidirectional, persistent,
point-to-point stream based connection between
hosts on the Internet.
0 There are two types of TCP sockets in Java; one is for
servers and other is for clients.
0 The ServerSocket class is for server and Socket is for
client.
0 The ServetSocket class is designed to be a “listener”,
which waits for clients to connect before do anything.
TCP/IP Sockets
0 The Socket class is designed to connect to server
sockets.
0 The creation of a Socket object implicitly established
connection between client and server.
0 Note: There are no methods or constructors that
explicitly expose the details of establishing that
connection.
TCP/IP Sockets
0 There are two constructor used to create client
socket:
0 Socket(String hostName, int port) throws
UnknownHostException, IOException
Creates a socket connected to the named host and
port.
0 Socket(InetAddress ipAddress, int port) throws
IOException
Creates a socket using a pre existing InetAddress
object and a port.
0 There are three constructor used to create server
socket:
0 ServerSocket(int port) throws IOException
0 Creates server socket on the specified port with a queue
length of 50.
0 ServerSocket(int port, int maxQueue) throws
IOException
0 Creates a server socket on the specified port with a
maximum queue length of maxQueue.
0 ServerSocket(int port, int maxQueue, InetAddress
localAddress) throws IOException
0 Creates a server socket on the specified port with a
maximum queue length of maxQueue. On a multihomed
host, localAddress specifies the IP address to which this
socket binds.
URL
0 Uniform Resource Locator
0 Provide reasonably intelligible form to uniquely
identify or address information on the Internet.
0 URLs are being every where at once every browser
uses them to identify information on the Web.
0 Within Java’s network class library, the URL class
provides a simple, concise (brief important
information) API to access information across the
Internet using URLs.
URL
0 A URL specification format is based on four
components.
0 Here are two examples: http://www.osborne.com/
and http://www.osborne.com:80/index.htm.
0 The first is the protocol to use, separated from the rest
of the locator by a colon (:).
0 Common protocols are HTTP, FTP, gopher.
0 The second component is the host name or IP address
of the host to use.
0 The third component, the port number, is an optional
parameter.
URL
0 The fourth part is the actual file path.
0Most HTTP servers will append a file named index.html or
index.htm to URLs that refer directly to a directory resource.
0 Constructor:
0 URL(String urlSpecifier) throws
MalformedURLException
0 URL(String protocolName, String hostName, int port,
String path) throws MalformedURLException
0 URL(String protocolName, String hostName, String path)
throws MalformedURLException
0 URL(URL urlObj, String urlSpecifier) throws
MalformedURLException
URLConnection
0 URLConnection is a general-purpose class for
accessing the attributes of a remote resource.
0 Once you make a connection to a remote server, you
can use URLConnection to inspect the properties of
the remote object before actually transporting it
locally
HttpURLConnection
0 Java provides a subclass of URLConnection that
provides support for HTTP connections.
0 We obtain an HttpURLConnection by calling
openConnection( ) on a URL object, but we must cast
the result to HttpURLConnection.
Datagrams
0 Datagram is basically an information but there is no
guarantee of packet delivery.
0 Java implements datagrams on top of the UDP
protocol by using two classes:
0 DatagramPacket
0 Is a message that can be sent or received.
0 If you send multiple packet, it may arrive in any order.
0 DatagramSocket
0 is the mechanism used to send or receive the
DatagramPackets.
DatagramSocket
0 Represents a connection-less socket for sending and
receiving datagram packet.
0 DatagramSocket defines four public constructors:
0 DatagramSocket( ) throws SocketException
0 DatagramSocket(int port) throws SocketException
0 DatagramSocket(int port, InetAddress ipAddress) throws
SocketException
0 DatagramSocket(SocketAddress address) throws
SocketException
DatagramSocket
0 Methods
0 void send(DatagramPacket packet) throws IOException
0 void receive(DatagramPacket packet) throws
IOException
DatagramPacket
0 Constructor:
0 DatagramPacket(byte data[ ], int size)
0 DatagramPacket(byte data[ ], int size, InetAddress
ipAddress, int port)

Unit-6-java basic for java programing.pptx

  • 2.
    Networking Basic 0 Javanetworking supports the concept of Socket. 0 Socket 0 Identifies an endpoint in a n/w. 0 Allows a single computer to serve many different type of information to many different client as well as server at once. 0 This is accomplished through port(numbered socket on a particular machine).
  • 3.
    Networking Basic 0 Socket 0Socket communication takes place via protocol 0IP 0 Low-level routing protocol 0 Breaks data into small packets and send them to an address across n/w 0 No guarantee to delivered. 0TCP 0 Higher level protocol 0 Manage packet robustly 0 Sort and retransmit them as necessary 0 Reliably transmit data 0UDP 0 Used directly to support fast ,connectionless , unreliable transport packets.
  • 4.
    InetAddress 0 Encapsulate boththe numerical IP Address and the domain name for the address. 0 Can handle IPv4 and IPv6 0 Factory Method 0 InetAddress class does not have visible constructor 0 To create an InetAddress object, you have to use one of the available factory methods.
  • 5.
    InetAddress 0 Factory Methods 0static InetAddress getLocalHost( )throws UnknownHostException 0 static InetAddress getByName(String hostName) throws UnknownHostException 0 static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
  • 6.
    TCP/IP Sockets 0 Usedto implement reliable, bidirectional, persistent, point-to-point stream based connection between hosts on the Internet. 0 There are two types of TCP sockets in Java; one is for servers and other is for clients. 0 The ServerSocket class is for server and Socket is for client. 0 The ServetSocket class is designed to be a “listener”, which waits for clients to connect before do anything.
  • 7.
    TCP/IP Sockets 0 TheSocket class is designed to connect to server sockets. 0 The creation of a Socket object implicitly established connection between client and server. 0 Note: There are no methods or constructors that explicitly expose the details of establishing that connection.
  • 8.
    TCP/IP Sockets 0 Thereare two constructor used to create client socket: 0 Socket(String hostName, int port) throws UnknownHostException, IOException Creates a socket connected to the named host and port. 0 Socket(InetAddress ipAddress, int port) throws IOException Creates a socket using a pre existing InetAddress object and a port.
  • 9.
    0 There arethree constructor used to create server socket: 0 ServerSocket(int port) throws IOException 0 Creates server socket on the specified port with a queue length of 50. 0 ServerSocket(int port, int maxQueue) throws IOException 0 Creates a server socket on the specified port with a maximum queue length of maxQueue. 0 ServerSocket(int port, int maxQueue, InetAddress localAddress) throws IOException 0 Creates a server socket on the specified port with a maximum queue length of maxQueue. On a multihomed host, localAddress specifies the IP address to which this socket binds.
  • 10.
    URL 0 Uniform ResourceLocator 0 Provide reasonably intelligible form to uniquely identify or address information on the Internet. 0 URLs are being every where at once every browser uses them to identify information on the Web. 0 Within Java’s network class library, the URL class provides a simple, concise (brief important information) API to access information across the Internet using URLs.
  • 11.
    URL 0 A URLspecification format is based on four components. 0 Here are two examples: http://www.osborne.com/ and http://www.osborne.com:80/index.htm. 0 The first is the protocol to use, separated from the rest of the locator by a colon (:). 0 Common protocols are HTTP, FTP, gopher. 0 The second component is the host name or IP address of the host to use. 0 The third component, the port number, is an optional parameter.
  • 12.
    URL 0 The fourthpart is the actual file path. 0Most HTTP servers will append a file named index.html or index.htm to URLs that refer directly to a directory resource. 0 Constructor: 0 URL(String urlSpecifier) throws MalformedURLException 0 URL(String protocolName, String hostName, int port, String path) throws MalformedURLException 0 URL(String protocolName, String hostName, String path) throws MalformedURLException 0 URL(URL urlObj, String urlSpecifier) throws MalformedURLException
  • 13.
    URLConnection 0 URLConnection isa general-purpose class for accessing the attributes of a remote resource. 0 Once you make a connection to a remote server, you can use URLConnection to inspect the properties of the remote object before actually transporting it locally
  • 14.
    HttpURLConnection 0 Java providesa subclass of URLConnection that provides support for HTTP connections. 0 We obtain an HttpURLConnection by calling openConnection( ) on a URL object, but we must cast the result to HttpURLConnection.
  • 15.
    Datagrams 0 Datagram isbasically an information but there is no guarantee of packet delivery. 0 Java implements datagrams on top of the UDP protocol by using two classes: 0 DatagramPacket 0 Is a message that can be sent or received. 0 If you send multiple packet, it may arrive in any order. 0 DatagramSocket 0 is the mechanism used to send or receive the DatagramPackets.
  • 16.
    DatagramSocket 0 Represents aconnection-less socket for sending and receiving datagram packet. 0 DatagramSocket defines four public constructors: 0 DatagramSocket( ) throws SocketException 0 DatagramSocket(int port) throws SocketException 0 DatagramSocket(int port, InetAddress ipAddress) throws SocketException 0 DatagramSocket(SocketAddress address) throws SocketException
  • 17.
    DatagramSocket 0 Methods 0 voidsend(DatagramPacket packet) throws IOException 0 void receive(DatagramPacket packet) throws IOException
  • 18.
    DatagramPacket 0 Constructor: 0 DatagramPacket(bytedata[ ], int size) 0 DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port)