unit 3 part 2 networking
unit 3 part 2 networking
Socket:
A socket can be regarded as a basic network entity that allows devices to
communicate over a network with the use of data. It is used in a
communication chain and acts as an accepting and transmitting centre for
data from the applications. Sockets are a way to enable programs to
communicate with others using certain protocols like TCP or UDP. Socket
Socket vs port difference
Parameters Socket Port
Identifies different
Facilitates communication
applications/services on a
between two devices.
Function device.
import java.net.*;
class JavaApplication2 {
public static void main(String args[])
throws UnknownHostException
{
// The URL for which IP address needs to be fetched
String s = "https://www.google.com/";
try {
// Fetch IP address by getByName()
InetAddress ip = InetAddress.getByName(new URL(s)
.getHost());
o Factory Method Pattern allows the sub-classes to choose the type of objects to
create.
o It promotes the loose-coupling by eliminating the need to bind application-specific
classes into the code. That means the code interacts solely with the resultant
interface or abstract class, so that it will work with any classes that implement that
interface or that extends that abstract class.
Instance methods:
Instance Methods are the group of codes that performs a particular task.
Sometimes the program grows in size, and we want to separate the logic
of the main method from other methods. A method is a function written
inside the class. Since java is an object-oriented programming language,
we need to write a method inside some classes.
Example:
import java.io.*;
class GFG {
// static method
public static void main (String[] args) {
// Creating object of the class
GFG obj = new GFG();
// Calling instance method
obj.disp();
System.out.println("GFG!");
}
// Instance method
void disp()
{
// Local variable
int a = 20;
System.out.println(a);
}
}
Output
20
GFG!
Java URLConnection Class
The Java URLConnection class represents a communication link between the URL and
the application. It can be used to read and write data to the specified resource referred
by the URL.
o URL is an abbreviation for Uniform Resource Locator. An URL is a form of string that
helps to find a resource on the World Wide Web (WWW).
o URL has two components:
class JavaApplication4 {
public static void main(String[] args)
{
String url
= "https://www.geeksforgeeks.org/variables-in-java/";
// Calling method to get URL info
getUrlInfo(url);
}
static void getUrlInfo(String url_string)
{
// Creating object of URL class
try {
URL url = new URL(url_string);
System.out.println("Hostname: "
+ url.getHost());
System.out.println("Port Number: "
+ url.getPort());
System.out.println("File name: "
+ url.getFile());
System.out.println("Protocol: "
+ url.getProtocol());
}
catch (Exception e) {
}
}
}
Output: Hostname: www.geeksforgeeks.org
Port Number: -1
File name: /variables-in-java/
Protocol: https
Datagram:
A datagram is an independent, self-contained message sent over the
network whose arrival, arrival time, and content are not guaranteed.
Datagrams plays a vital role as an alternative.
Datagrams are bundles of information passed between machines.
Once the datagram has been released to its intended target, there is
no assurance that it will arrive or even that someone will be there to
catch it.
Likewise, when the datagram is received, there is no assurance that it
hasn’t been damaged in transit or that whoever sent it is still there to
receive a response and it is crucial point to note.
Java implements datagrams on top of the UDP (User Datagram Protocol)
protocol by using two classes:
1. DatagramPacket object is the data container.
2. DatagramSocket is the mechanism used to send or receive the
DatagramPackets.
Java has a different socket class that must be used for creating server
applications. The ServerSocket class is used to create servers that listen for
either local or remote client programs to connect to them on published
ports. ServerSockets are quite different from normal Sockets. When you
create a ServerSocket, it will register itself with the system as having an
interest in client connections. The constructors for ServerSocket reflect the
port number that you want to accept connections on and, optionally, how
long you want the queue for said port to be. The queue length tells the system
how many client connections it can leave pending before it should simply
refuse connections. The default is 50. The constructors might throw
an IOException under adverse conditions. Here are three of its constructors:
ServerSocket has a method called accept( ), which is a blocking call that will
wait for a client to initiate communications and then return with a
normal Socket that is then used for communication with the client.
TCP/IP client socket:
There are two kinds of TCP sockets in Java. One is for servers, and the other is
for clients. The ServerSocket class is designed to be a "listener," which waits
for clients to connect before doing anything. Thus, ServerSocket is for servers.
The Socket class is for clients. It is designed to connect to server sockets and
initiate protocol exchanges.
class javaApplication6{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",3335);
bw.flush();
String s1=br.readLine();
System.out.println(s1);
s.close();
}
}