NETWORKING IN JAVA
By,
S.Subhalakshmi.
M.Sc(Computer Science),
Nadar saraswathi college of arts and science.


 Networking
 Networking classes and Interfaces
 InetAddress
 Inet4Address and Inet6Address
 TCP/IP Server Sockets
 Datagrams

Java networking is a concept of connecting two or more
computing devices together so that we can share resources.
Java socket programming provides facility to share data between
different computing devices.
Java program communicates over the network at application
layer.
In the grand scheme of things, though, direct computer-to-
computer conversing almost never happens. Most of the conversing is between
a server and a client.
All the java networking classes and interfaces use
java.net.package.
These classes and interfaces provide the functionality to develop
system independent network communication.
Java.net.package encapsulates large number of classes and
interfaces that provides an easy-to use means to access network resources.
Java supports the both TCP and UDP protocol families.
TCP is used for reliable stream-based I/O across the network.
UDP supports a simpler, hence faster, point-to-point datagram-
oriented model.
The InetAddress is used to encapsulate both the numerical IP
address and the domain name for that address.
InetAddress class has no visible constructor.To create an Inet
Address object, you have to use Factory methods.
Three commonly used Inet Address factory methods are:
1.static InetAddress getLocalHost() throws
UnknownHostException.
2.static InetAddress getByName(String hostname) throws
UnknownHostException.
3.static InetAddress [] getAllByName(String hostname) throws
UnknownHostException.
InetAddress can handle both IPv4 and IPv6address.
The Inet4Address represents a traditional-style IPv4address.
The Inet6Address encapsulates a new-style IPv6address.
Because they are subclasses of InetAddress, an InetAddress
reference can refer to either.
This is one way that java was able to add IPv6 functionality
without breaking existing code or adding many more classes.
For the most part, you can simply use InetAddress when
working with IP addresses because it can accommodate both styles.
A socket is one endpoint of a two-way communication link
between two programs running on the network.
A socket is bound to a port number so that the TCP layer can
identify the application that data is destined to be sent to.
An endpoint is a combination of an IP address and a port number.
Every TCP connection can be uniquely identified by its two
endpoints. That way you can have multiple connections between your host and
the server.
A datagram is a basic transfer unit associated with a packet-
switched network.
Datagrams are typically structured in header and payload
sections.
Datagrams provide a connectionless communication service
across a packet-switched network.
The delivery, arrival time, and order of arrival of datagrams need
not be guaranteed by the network.
A datagram needs to be self-contained without reliance on earlier
exchanges because there is no connection of fixed duration between the two
communicating points as there is, for example, in most voice telephone
conversations.
Java implements datagrams on top of the UDP protocol by using
two classes:
Java datagramsocket class represents a connection-less socket
for sending and receiving datagram packets.
A datagram is basically an information but there is no
guarantee of its content, arrival or arrival time.
DatagramSocket() throws SocketException:it creates a
datagram socket binds it with the available port number on the localhost
machine.
DatagramSocket(int port) throws SocketException:it creates a
datagram socket and binds it with the given port number.
DatagramSocket(int port, InetAddress address) throws
SocketException:it creates a datagram socket and binds it with the specified
port number and host address.
Java DatagramPacket is a message that can be send or
received.If you send multiple packet,it may arrive in any
order.Additionally,packetdelivery is not guaranteed.
DatagramPacket(byte[] barr, int length):it creates a datagram
packet.This constructor is used to receive the packets.
DatagramPacket(byte[] barr,int length,InetAddress address,int
port):it creates a datagram packet.This constructor is used to send the
packets.
Java

Java

  • 1.
    NETWORKING IN JAVA By, S.Subhalakshmi. M.Sc(ComputerScience), Nadar saraswathi college of arts and science.
  • 2.
       Networking  Networkingclasses and Interfaces  InetAddress  Inet4Address and Inet6Address  TCP/IP Server Sockets  Datagrams 
  • 3.
    Java networking isa concept of connecting two or more computing devices together so that we can share resources. Java socket programming provides facility to share data between different computing devices. Java program communicates over the network at application layer. In the grand scheme of things, though, direct computer-to- computer conversing almost never happens. Most of the conversing is between a server and a client.
  • 4.
    All the javanetworking classes and interfaces use java.net.package. These classes and interfaces provide the functionality to develop system independent network communication. Java.net.package encapsulates large number of classes and interfaces that provides an easy-to use means to access network resources. Java supports the both TCP and UDP protocol families. TCP is used for reliable stream-based I/O across the network. UDP supports a simpler, hence faster, point-to-point datagram- oriented model.
  • 6.
    The InetAddress isused to encapsulate both the numerical IP address and the domain name for that address. InetAddress class has no visible constructor.To create an Inet Address object, you have to use Factory methods. Three commonly used Inet Address factory methods are: 1.static InetAddress getLocalHost() throws UnknownHostException. 2.static InetAddress getByName(String hostname) throws UnknownHostException. 3.static InetAddress [] getAllByName(String hostname) throws UnknownHostException.
  • 7.
    InetAddress can handleboth IPv4 and IPv6address.
  • 8.
    The Inet4Address representsa traditional-style IPv4address. The Inet6Address encapsulates a new-style IPv6address. Because they are subclasses of InetAddress, an InetAddress reference can refer to either. This is one way that java was able to add IPv6 functionality without breaking existing code or adding many more classes. For the most part, you can simply use InetAddress when working with IP addresses because it can accommodate both styles.
  • 9.
    A socket isone endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host and the server.
  • 10.
    A datagram isa basic transfer unit associated with a packet- switched network. Datagrams are typically structured in header and payload sections. Datagrams provide a connectionless communication service across a packet-switched network. The delivery, arrival time, and order of arrival of datagrams need not be guaranteed by the network. A datagram needs to be self-contained without reliance on earlier exchanges because there is no connection of fixed duration between the two communicating points as there is, for example, in most voice telephone conversations.
  • 11.
    Java implements datagramson top of the UDP protocol by using two classes:
  • 12.
    Java datagramsocket classrepresents a connection-less socket for sending and receiving datagram packets. A datagram is basically an information but there is no guarantee of its content, arrival or arrival time. DatagramSocket() throws SocketException:it creates a datagram socket binds it with the available port number on the localhost machine. DatagramSocket(int port) throws SocketException:it creates a datagram socket and binds it with the given port number. DatagramSocket(int port, InetAddress address) throws SocketException:it creates a datagram socket and binds it with the specified port number and host address.
  • 13.
    Java DatagramPacket isa message that can be send or received.If you send multiple packet,it may arrive in any order.Additionally,packetdelivery is not guaranteed. DatagramPacket(byte[] barr, int length):it creates a datagram packet.This constructor is used to receive the packets. DatagramPacket(byte[] barr,int length,InetAddress address,int port):it creates a datagram packet.This constructor is used to send the packets.