Advanced Java Tutorial


Total available pages count: 55
Subject - Java Technologies

Socket programming

It is communication between two applications that running on different runtime environments.it can be connection-oriented 0r connectionless.

 A client creates a socket on the end of the communication & attempt to connect the socket to the server. 

The java.net.Socket class represent the socket & provides the methodology for the server program to listen for client & establish a connection between them.

When we establish the TCP connection between two computing devices using a socket.

  • The server proceeds the ServerSocket object, which denotes which port number communication is to occur on
  • The server actives the accept() method of ServerSocket class. the method waits until the client connects to the server on the given port
  • After the server is waiting, the client proceeds the Socket object that specifies the server name & port number to connect
  • The constructor of the Socket class attempts to connect the client to the port number & the specified server. When communication gets established the client had a Socket object capable of communicating with the server.
  • On the server-side, the accept() method returns a reference to a new socket on a server that connected to the socket of the client-side
  1. For connection-oriented socket programming, socket & ServerSocket classes are used
  2. For connection-less socket programming, DatagramSocket & DatagramPacket classes are used

The Socket programming must require two information:
IP address &  port number

URL?

Uniform Resource Locator is short known as URL.it is to resource on the world wide web.
e.g.  https://www.slightbook.com/Subjects

where,
https://  – protocol
www.slightbook.com – hostname
Subjects – file
URL contains information like protocol, server name/IP address ,port number & directory/file name.

URL Connection?

The URLConnection represents the communication link between the application & URL. It can be used to write & read the data to a specified resource referred by URL.

Get object of URLConnection:

The URLConnection has provided many methods, you can display all the information of the webpage by using getInputStream(). The getInputStream() returns all the data of the specified URL in the stream to read & displayed

public URLConnection openConnection()throws IOException{}

 

HttpURLConnection class

The HttpURLConnection has HTTP specific URLConnection. It is working for HTTP protocol only. Using class, you can have information of HTTP URL such as header information, response code & status code, etc. The HttpURLConnection also called a subclass of URLConnection class.

To get Object of HttpURLConnection class,

/* openConnection() method */
public URLConnection openConnection()throws IOException{}

/* typecast to HttpURLConnection type*/
URL url=new URL("https://www.slightbook.com/java-tutorial");
HttpURLConnection huc=(HttpURLConnection)url.openConnection();

 

DatagramSocket & DatagramPacket class

For connection-less socket programming, DatagramSocket & DatagramPacket classes are used.

The DatagramSocket class used for sending & receiving the datagram packet. Normally it is just information but there is no guarantee of data arrival or arrival time.

The DatagramSocket used to view messages that can be sent or received if you sent more than one packet. It will arrive in any order so basically, packet delivery is not guaranteed.



Comments