Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Networking

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Java Networking

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. Advantages of
Java Networking are sharing resources and centralize software management

Java Networking Terminology

 IP Address: IP address is a unique number assigned to a node of a


network e.g. 192.168.0.1 . It is composed of octets that range from 0
to 255. It is a logical address that can be changed.

 Protocol: A protocol is a set of rules basically that is followed for


communication. For example: TCP, FTP Telnet, SMTP, POP etc.

 Port Number: The port number is used to uniquely identify different


applications. It acts as a communication endpoint between
applications. The port number is associated with the IP address for
communication between two applications.

 MAC Address: MAC (Media Access Control) Address is a unique


identifier of NIC (Network Interface Controller). A network node can
have multiple NIC but each with unique MAC.

 Connection-oriented and connection-less protocol: In


connection-oriented protocol, acknowledgement is sent by the
receiver. So it is reliable but slow. The example of connection-oriented
protocol is TCP.
But, in connection-less protocol, acknowledgement is not sent by the
receiver. So it is not reliable but fast. The example of connection-less
protocol is UDP.

 Socket
A socket is an endpoint between two way communication.

Java Socket Programming

Java Socket programming is used for communication between the


applications running on different JRE. Java Socket programming can be
connection-oriented or connection-less.
Socket and ServerSocket classes are used for connection-oriented socket
programming and DatagramSocket and DatagramPacket classes are used for
connection-less socket programming.

The client in socket programming must know two information:


1. IP Address of Server
2. Port number.

This is one-way client and server communication. In this application, client


sends a message to the server, server reads the message and prints it.
Here, two classes are being used: Socket and ServerSocket. The Socket
class is used to communicate client and server. Through this class, we can
read and write message. The ServerSocket class is used at server-side. The
accept() method of ServerSocket class blocks the console until the client is
connected. After the successful connection of client, it returns the instance of
Socket at server-side.
Socket class

A socket is simply an endpoint for communications between the machines.


The Socket class can be used to create a socket.

Important methods

Method Description
1) public InputStream getInputStream() returns the InputStream
attached with this socket.
2) public OutputStream getOutputStream() returns the OutputStream
attached with this socket.
3) public synchronized void close() closes this socket

ServerSocket class

The ServerSocket class can be used to create a server socket. This object is
used to establish communication with the clients.

Important methods

Method Description
1) public Socket accept() returns the socket and establish a
connection between server and
client.
2) public synchronized void close() closes the server socket.

Creating Server:

To create the server application, we need to create the instance of


ServerSocket class. Here, we are using 6666 port number for the
communication between the client and server. You may also choose any
other port number. The accept() method waits for the client. If clients
connects with the given port number, it returns an instance of Socket.

1. ServerSocket ss=new ServerSocket(6666);


2. Socket s=ss.accept();//establishes connection and waits for the client

Creating Client:
To create the client application, we need to create the instance of Socket
class. Here, we need to pass the IP address or hostname of the Server and a
port number. Here, we are using "localhost" because our server is running
on same system.

Socket s=new Socket("localhost",6666);

Java URL

The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a
resource on the World Wide Web.

https://www.google.com/java-notes

A URL contains many information:

1. Protocol: In this case, http is the protocol.


2. Server name or IP Address: In this case, www.google.com is the server name.

3. Port Number: It is an optional attribute. If we write http//www.google.com:80/java/ , 80


is the port number. If port number is not mentioned in the URL, it returns -1.

4. File Name or directory name: In this case, “java-notes” is the file name.

Constructors of java URL

URL(String spec)

Creates an instance of a URL from the String representation.

URL(String protocol, String host, int port, String file)

Creates an instance of a URL from the given protocol, host, port number, and file.

URL(String protocol, String host, int port, String file, URLStreamHandler handler)

Creates an instance of a URL from the given protocol, host, port number, file, and handler.

URL(String protocol, String host, String file)

Creates an instance of a URL from the given protocol name, host name, and file name.

URL(URL context, String spec)

Creates an instance of a URL by parsing the given spec within a specified context.
URL(URL context, String spec, URLStreamHandler handler)

Creates an instance of a URL by parsing the given spec with the specified handler within a given
context.

The java.net.URL class provides many methods. The important methods of URL class are given
below.

Method Description
public String getProtocol() it returns the protocol of the URL.
public String getHost() it returns the host name of the URL.
public String getPort() it returns the Port Number of the URL.
public String getFile() it returns the file name of the URL.
public String getAuthority() it returns the authority of the URL.
public String toString() it returns the string representation of the URL.
public String getQuery() it returns the query string of the URL.
public String getDefaultPort() it returns the default port of the URL.
public URLConnection it returns the instance of URLConnection i.e. associated with this
openConnection() URL.
public boolean equals(Object obj) it compares the URL with the given object.
public Object getContent() it returns the content of the URL.
public String getRef() it returns the anchor or reference of the URL.
public URI toURI() it returns a URI of the URL.

The Java URLConnection class represents a communication link between the URL and the
application. This class can be used to read and write data to the specified resource referred by the
URL.

The openConnection() method of URL class returns the object of URLConnection class. Syntax:

public URLConnection openConnection()throws IOException{}

The URLConnection class provides many methods, we can display all the data of a webpage by
using the getInputStream() method. The getInputStream() method returns all the data of the
specified URL in the stream that can be read and displayed.

Java HttpURLConnection class

The Java HttpURLConnection class is http specific URLConnection. It works for HTTP
protocol only.

By the help of HttpURLConnection class, you can information of any HTTP URL such as header
information, status code, response code etc.
The java.net.HttpURLConnection is subclass of URLConnection class.

How to get the object of HttpURLConnection class

The openConnection() method of URL class returns the object of URLConnection class. Syntax:

1. public URLConnection openConnection()throws IOException{}

You can typecast it to HttpURLConnection type as given below.

1. URL url=new URL("http://www.javatpoint.com/java-tutorial");


2. HttpURLConnection huc=(HttpURLConnection)url.openConnection();

Java InetAddress class

Java InetAddress class represents an IP address. The java.net.InetAddress class provides


methods to get the IP of any host name for example www.javatpoint.com, www.google.com,
www.facebook.com, etc.

An IP address is represented by 32-bit or 128-bit unsigned number. An instance of InetAddress


represents the IP address with its corresponding host name. There are two types of address types:
Unicast and Multicast. The Unicast is an identifier for a single interface whereas Multicast is an
identifier for a set of interfaces.

Moreover, InetAddress has a cache mechanism to store successful and unsuccessful host name
resolutions.

Commonly used methods of InetAddress class

Method Description
public static InetAddress getByName(String host) throws it returns the instance of InetAddress containing
UnknownHostException LocalHost IP and name.
public static InetAddress getLocalHost() throws it returns the instance of InetAdddress containing
UnknownHostException local host name and address.
public String getHostName() it returns the host name of the IP address.
public String getHostAddress() it returns the IP address in string format.

Java DatagramSocket and DatagramPacket

Java DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.

Java DatagramSocket class


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.

Commonly used Constructors of DatagramSocket class

 DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port
Number on the localhost machine.
 DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given
Port Number.

 DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and
binds it with the specified port number and host address.

Java DatagramPacket class

Java DatagramPacket is a message that can be sent or received. If you send multiple packet, it
may arrive in any order. Additionally, packet delivery is not guaranteed.

Commonly used Constructors of DatagramPacket class

 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.

You might also like