WT _Unit-03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

Javascript

JavaScript (JS) is a lightweight interpreted (or just-in-time compiled) programming


language with first-class functions. While it is most well-known as the scripting
language for Web pages, many non-browser environments also use it, such
as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-
based, multi-paradigm, single-threaded, dynamic language, supporting object-
oriented, imperative, and declarative (e.g. functional programming) styles.

JavaScript's dynamic capabilities include runtime object construction, variable


parameter lists, function variables, dynamic script creation (via eval), object
introspection (via for...in and Object utilities), and source-code recovery
(JavaScript functions store their source text and can be retrieved
through toString()).

JavaScript is a high-level, dynamic, and interpreted programming language.


It is primarily used for creating interactive effects within web browsers.
It is one of the core technologies of the World Wide Web, alongside
HTML and CSS.

History:

Developed by Brendan Eich in 1995, initially named Mocha, then


LiveScript, and finally JavaScript.
It has evolved significantly and is now standardized under ECMAScript.

2. Documentation

Learning

Resources:

• MDN Web Docs: Comprehensive documentation for JavaScript and web


technologies.
• ECMAScript Specification: The official standard that JavaScript follows.
• Books and Online Courses: Various resources are available for in-
depth learning (e.g., "Eloquent JavaScript," "JavaScript: The Good
Parts").
Comments in JavaScript:

• Single-line comment: // This is a comment


• Multi-line comment: /* This is a comment */

3. JavaScript

Forms Form

Handling:

• JavaScript can validate and manipulate forms in real-time, enhancing user


experience.
• Commonly used methods: document.getElementById(), addEventListener(),
and form properties.

Example: Basic Form Validation

<form id="myForm">

<input type="text" id="name" required>

<input type="submit" value="Submit">

</form>

<script>

document.getElementById("myForm").onsubmit = function(event) {

const name = document.getElementById("name").value;

if (!name) {

alert("Name must be filled out");

event.preventDefault(); // Prevent form submission

};

</script>
4. Statements

Types of Statements:

• Declaration Statements: let, const, var for declaring variables.


• Expression Statements: Assigning values, function calls.
• Control Flow Statements: if, else, switch, for, while.

Example: Control Flow

let age = 18;

if (age >= 18) {

console.log("You are an adult.");

} else {

console.log("You are a minor.");

Functions

In JavaScript, functions are first-class objects, because they can be passed to


other functions, returned from functions, and assigned to variables and
properties. They can also have properties and methods just like any other object.
What distinguishes them from other objects is that functions can be called.

function greet(name) {

return "Hello, " + name + "!";

console.log(greet("Alice")); // Output: Hello, Alice!

Return value
By default, if a function's execution doesn't end at a return statement, or if the
return keyword doesn't have an expression after it, then the return value
is undefined. The return statement allows you to return an arbitrary value from the
function. One function call can only return one value, but you can simulate the
effect of returning multiple values by returning an object or array
and destructuring the result.

Passing arguments
Parameters and arguments have slightly different meanings, but in MDN web docs,
we often use them interchangeably. For a quick reference:

function formatNumber(num) {

return num.toFixed(2);

formatNumber(2);

In this example, the num variable is called the function's parameter: it's declared in
the parenthesis-enclosed list of the function's definition. The function expects the
num parameter to be a number — although this is not enforceable in JavaScript
without writing runtime validation code. In the formatNumber(2) call, the number
2 is the function's argument: it's the value that is actually passed to the function in
the function call. The argument value can be accessed inside the function body
through the corresponding parameter name or
the arguments object.

Arguments are always passed by value and never passed by reference. This means
that if a function reassigns a parameter, the value won't change outside the
function. More precisely, object arguments are passed by sharing, which means if
the object's properties are mutated, the change will impact the outside of the
function. For example:

function updateBrand(obj) {

// Mutating the object is visible outside the function


obj.brand = "Toyota";

// Try to reassign the parameter, but this won't affect

// the variable's value outside the function

obj = null;

}const car = {

brand: "Honda",

model:

"Accord", year:

1998,

};

console.log(car.brand); // Honda

// Pass object reference to the function

updateBrand(car);

// updateBrand mutates car

console.log(car.brand); // Toyota

The this keyword refers to the object that the function is accessed on — it does
not refer to the currently executing function, so you must refer to the function
value by name, even within the function body.

Defining functions

Broadly speaking, JavaScript has four kinds of functions:

• Regular function: can return anything; always runs to completion after


invocation
• Generator function: returns a Generator object; can be paused and
resumed with the yield operator
• Async function: returns a Promise; can be paused and resumed with
the await operator
• Async generator function: returns an AsyncGenerator object; both
the await and yield operators can be used

In JavaScript, functions can be categorized into several types based on


their syntax and usage. Here are the main types:
1. Function Declarations:

function myFunction() {

// code to be executed

2. Function Expressions:
const myFunction = function() {

// code to be executed
}

3. Arrow Functions (introduced in


ES6): const myFunction = () => {

// code to be executed

};

Or
const greet = (name) => `Hello,
${name}!`;
console.log(greet("Bob")); // Output:
Hello, Bob!
4. Anonymous Functions: These are functions without a name and can be used in
function expressions:

const myFunction = function() {


// code to be executed

};

Immediately Invoked Function Expressions (IIFE): Functions that are executed


right after they are defined:

(function() {

// code to be executed

})();

Higher-Order Functions: Functions that take other functions as arguments or


return functions:

function higherOrderFunction(callback)
{ callback();

Generator Functions (introduced in ES6): Functions that can be paused and


resumed:

function* generatorFunction() {
yield 'value';}

Async Functions (introduced in ES2017): Functions that always return a promise


and allow the use of await:
async function asyncFunction() {

const result = await

someAsyncCall();

}
Each type of function serves different purposes and can be used based on the
requirements of your code.

6. Objects
What are Objects?

The Object type represents one of JavaScript's data types. It is used to store various
keyed collections and more complex entities. Objects can be created using the
Object() constructor or the object initializer / literal syntax.

• Objects are collections of properties, with a key-value structure.


• They can represent real-world entities.

Creating Objects:

• Using object literals, constructors, or classes.

Example: Object Literal

const person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello, " +
this.name);
}
};

person.greet(); // Output: Hello, John


Object

The Object type represents one of JavaScript's data types. It is used to store various
keyed collections and more complex entities. Objects can be created using the
Object() constructor or the object initializer / literal syntax.

Description

Nearly all objects in JavaScript are instances of Object; a typical object inherits
properties (including methods) from Object.prototype, although these propertiesmay
be shadowed (a.k.a. overridden). The only objects that don't inherit
from Object.prototype are those with null prototype, or descended from other
null prototype objects.

Changes to the Object.prototype object are seen by all objects through prototype
chaining, unless the properties and methods subject to those changes are overridden
further along the prototype chain. This provides a very powerful although
potentially dangerous mechanism to override or extend object behavior. To make it
more secure, Object.prototype is the only object in the core JavaScript language that
has immutable prototype — the prototype of Object.prototype is always null and
not changeable.

Object prototype properties

You should avoid calling any Object.prototype method directly from the instance,
especially those that are not intended to be polymorphic (i.e. only its initial behavior
makes sense and no descending object could override it in a meaningful way). All
objects descending from Object.prototype may define a custom own property that
has the same name, but with entirely different semantics from what you expect.
Furthermore, these properties are not inherited by null-prototype objects. All
modern JavaScript utilities for working with objects are static. More specifically:

• valueOf(), toString(), and toLocaleString() exist to be polymorphic and


you should expect the object to define its own implementation with
sensible behaviors, so you can call them as instance methods.
However, valueOf() and toString() are usually implicitly called through type
conversion and you don't need to call them yourself in your code.
• defineGetter (), defineSetter (), lookupGetter (),
and __lookupSetter__() are deprecated and should not be used. Use the static
alternatives Object.defineProperty() and Object.getOwnPropertyDescriptor ()
instead.
• The proto property is deprecated and should not be used.
The Object.getPrototypeOf() and Object.setPrototypeOf() alternatives are
static methods.
• The propertyIsEnumerable() and hasOwnProperty() methods can be
replaced with
the Object.getOwnPropertyDescriptor() and Object.hasOwn() static
methods, respectively.
• The isPrototypeOf() method can usually be replaced with instanceof, if you
are checking the prototype property of a constructor.

In case where a semantically equivalent static method doesn't exist, or if you really
want to use the Object.prototype method, you should
directly call() the Object.prototype method on your target object instead, to prevent
the object from having an overriding property that produces unexpectedresults.

const obj =
{ foo: 1,
// You should not define such a method on your own object,
// but you may not be able to prevent it from happening if
// you are receiving the object from external input
propertyIsEnumerable() {
return false;
},
};

obj.propertyIsEnumerable("foo"); // false; unexpected result


Object.prototype.propertyIsEnumerable.call(obj, "foo"); // true; expected
result
Deleting a property from an object

There isn't any method in an Object itself to delete its own properties (suchas
Map.prototype.delete()). To do so, one must use the delete operator.
null-prototype objects

Almost all objects in JavaScript ultimately inherit


from Object.prototype (see inheritance and the prototype chain). However, you
may create null-prototype objects using Object.create(null) or the object initializer
syntax with proto : null (note: the proto key in object literals
is different from the deprecated Object.prototype. proto property). You can
also change the prototype of an existing object to null by
calling Object.setPrototypeOf(obj, null).

const obj = Object.create(null);


const obj2 = { proto : null
};

An object with a null prototype can behave in unexpected ways, because it doesn't
inherit any object methods from Object.prototype. This is especially true when
debugging, since common object-property converting/detecting utility functions
may generate errors, or lose information (especially if using silent error-traps that
ignore errors).

For example, the lack of Object.prototype.toString() often makes debugging


intractable:

const normalObj = {}; // create a normal object


const nullProtoObj = Object.create(null); // create an object
with "null" prototype

console.log(`normalObj is: ${normalObj}`); // shows


"normalObj is: [object Object]"
console.log(`nullProtoObj is: ${nullProtoObj}`); // throws error:
Cannot convert object to primitive value

alert(normalObj); // shows [object Object]


alert(nullProtoObj);
// throws error: Cannot convert object to primitive valueOther

methods will fail as well.


normalObj.valueOf(); // shows {}
nullProtoObj.valueOf(); // throws error: nullProtoObj.valueOf is
not a function

normalObj.hasOwnProperty("p"); // shows "true"


nullProtoObj.hasOwnProperty("p"); // throws error:
nullProtoObj.hasOwnProperty is not a function

normalObj.constructor; // shows "Object() { [native code] }"


nullProtoObj.constructor; // shows "undefined"

We can add the toString method back to the null-prototype object by assigning it
one:

nullProtoObj.toString = Object.prototype.toString;
// since new object lacks toString, add the original generic one back

console.log(nullProtoObj.toString());
// shows "[object Object]"
console.log(`nullProtoObj is:
${nullProtoObj}`);
// shows "nullProtoObj is: [object Object]"

Unlike normal objects, in which toString() is on the object's prototype,


the toString() method here is an own property of nullProtoObj. This is
because nullProtoObj has no (null) prototype.

You can also revert a null-prototype object back to an ordinary object


using Object.setPrototypeOf(nullProtoObj, Object.prototype).

In practice, objects with null prototype are usually used as a cheap substitute
for maps. The presence of Object.prototype properties will cause some bugs:

const ages = { alice: 18, bob: 27


}; function hasPerson(name) {
return name in ages;
}

function getAge(name)
{ return ages[name];
}

hasPerson("hasOwnProperty"); // true
getAge("toString"); // [Function: toString]

Using a null-prototype object removes this hazard without introducing too much
complexity to the hasPerson and getAge functions:

const ages = Object.create(null, {


alice: { value: 18, enumerable: true
}, bob: { value: 27, enumerable: true
},
});
hasPerson("hasOwnProperty"); // false
getAge("toString"); // undefined

In such case, the addition of any method should be done cautiously, as they can be
confused with the other key-value pairs stored as data.

Making your object not inherit from Object.prototype also prevents prototype
pollution attacks. If a malicious script adds a property to Object.prototype, it will be
accessible on every object in your program, except objects that have null
prototype.

const user = {};


// A malicious script:
Object.prototype.authenticated = true;
// Unexpectedly allowing unauthenticated user to pass through if
(user.authenticated) {
// access confidential data
}

JavaScript also has built-in APIs that produce null-prototype objects, especially
those that use objects as ad hoc key-value collections. For example:

• The return value of Object.groupBy()


• The groups and indices.groups properties of the result
of RegExp.prototype.exec()
• Array.prototype[Symbol.unscopables] (all [Symbol.unscopables] objects
should have null-prototype)
• import.meta
• Module namespace objects, obtained through import * as ns from
"module"; or import()

The term "null-prototype object" often also includes any object


without Object.prototype in its prototype chain. Such objects can be created
with extends null when using classes.

Object coercion

Many built-in operations that expect objects first coerce their arguments to objects.
The operation can be summarized as follows:

• Objects are returned as-is.


• undefined and null throw a TypeError.
• Number, String, Boolean, Symbol, BigInt primitives are wrapped into
theircorresponding object wrappers.

There are two ways to achieve nearly the same effect in JavaScript.

• Object.prototype.valueOf(): Object.prototype.valueOf.call(x) does exactly


the object coercion steps explained above to convert x.
• The Object() function: Object(x) uses the same algorithm to convert x,
except that undefined and null don't throw a TypeError, but return a plain
object.

Places that use object coercion include:

• The object parameter of for...in loops.


• The this value of Array methods.
• Parameters of Object methods such as Object.keys().
• Auto-boxing when a property is accessed on a primitive value, since
primitives do not have properties.
• The this value when calling a non-strict function. Primitives are boxed
while null and undefined are replaced with the global object.

Unlike conversion to primitives, the object coercion process itself is not observable
in any way, since it doesn't invoke custom code
like toString or valueOf methods.
Constructor
Object()

Turns the input into an object.

Static methods
Object.assign()

Copies the values of all enumerable own properties from one or more source
objects to a target object.

Object.create()

Creates a new object with the specified prototype object and properties.

Object.defineProperties()

Adds the named properties described by the given descriptors to an object.

Object.defineProperty()

Adds the named property described by a given descriptor to an object.

Object.entries()

Returns an array containing all of the [key, value] pairs of a given


object's own enumerable string properties.

Object.freeze()

Freezes an object. Other code cannot delete or change its properties.

Object.fromEntries()

Returns a new object from an iterable of [key, value] pairs. (This is the reverse
of Object.entries).

Object.getOwnPropertyDescriptor()
Returns a property descriptor for a named property on an object.

Object.getOwnPropertyDescriptors()

Returns an object containing all own property descriptors for an object.

Object.getOwnPropertyNames()

Returns an array containing the names of all of the given


object's own enumerable and non-enumerable properties.

Object.getOwnPropertySymbols()

Returns an array of all symbol properties found directly upon a given object.

Object.getPrototypeOf()

Returns the prototype (internal [[Prototype]] property) of the specified object.

Object.groupBy()

Groups the elements of a given iterable according to the string values


returned by a provided callback function. The returned object has separate
properties for each group, containing arrays with the elements in the group.

Object.hasOwn()

Returns true if the specified object has the indicated property as


its own property, or false if the property is inherited or does not exist.

Object.is()

Compares if two values are the same value. Equates all NaN values (which
differs from both IsLooselyEqual used by == and IsStrictlyEqual used
by ===).
Object.isExtensible()

Determines if extending of an object is allowed.

Object.isFrozen()

Determines if an object was frozen.

Object.isSealed()

Determines if an object is sealed.

Object.keys()

Returns an array containing the names of all of the given


object's own enumerable string properties.

Object.preventExtensions()

Prevents any extensions of an object.

Object.seal()

Prevents other code from deleting properties of an object.

Object.setPrototypeOf()

Sets the object's prototype (its internal [[Prototype]] property).

Object.values()

Returns an array containing the values that correspond to all of a given


object's own enumerable string properties.

Instance properties

These properties are defined on Object.prototype and shared by all


Object instances.
Object.prototype. proto Deprecated

Points to the object which was used as prototype when the object was
instantiated.

Object.prototype.constructor

The constructor function that created the instance object. For


plain Object instances, the initial value is the Object constructor. Instances
of other constructors each inherit the constructor property from their
respective Constructor.prototype object.

Instance methods
Object.prototype. defineGetter () Deprecated

Associates a function with a property that, when accessed, executes that


function and returns its return value.

Object.prototype. defineSetter () Deprecated

Associates a function with a property that, when set, executes that function
which modifies the property.

Object.prototype. lookupGetter () Deprecated

Returns the function bound as a getter to the specified property.

Object.prototype. lookupSetter () Deprecated

Returns the function bound as a setter to the specified property.

Object.prototype.hasOwnProperty()

Returns a boolean indicating whether an object contains the specified


property as a direct property of that object and not inherited through the
prototype chain.

Object.prototype.isPrototypeOf()
Returns a boolean indicating whether the object this method is called uponis
in the prototype chain of the specified object.

Object.prototype.propertyIsEnumerable()

Returns a boolean indicating whether the specified property is the


object's enumerable own property.

Object.prototype.toLocaleString()

Calls toString().

Object.prototype.toString()

Returns a string representation of the object.

Object.prototype.valueOf()

Returns the primitive value of the specified object.

Examples

Constructing empty objects

The following example creates empty objects using the new keyword with
different arguments:

const o1 = new Object();


const o2 = new Object(undefined);
const o3 = new Object(null);

Using Object() constructor to turn primitives into an Object of their respective


type

You can use the Object() constructor to create an object wrapper of a primitive
value.

The following examples create variables o1 and o2 which are objects


storing Boolean and BigInt values:
// Equivalent to const o1 = new Boolean(true)
const o1 = new Object(true);

// No equivalent because BigInt() can't be called as a constructor,


// and calling it as a regular function won't create an object const
o2 = new Object(1n);

Object prototypes

When altering the behavior of existing Object.prototype methods, consider


injecting code by wrapping your extension before or after the existing logic. For
example, this (untested) code will pre-conditionally execute custom logic before
the built-in logic or someone else's extension is executed.

When modifying prototypes with hooks, pass this and the arguments (the call state)
to the current behavior by calling apply() on the function. This pattern can be used
for any prototype, such as Node.prototype, Function.prototype, etc.

const current = Object.prototype.valueOf;

// Since my property "-prop-value" is cross-cutting and isn't always


// on the same prototype chain, I want to modify Object.prototype:
Object.prototype.valueOf = function (...args) {
if (Object.hasOwn(this, "-prop-value"))
{ return this["-prop-value"];
} else {
// It doesn't look like one of my objects, so let's fall back on
// the default behavior by reproducing the current behavior as best we can.
// The apply behaves like "super" in some other languages.
// Even though valueOf() doesn't take arguments, some other hook may.
return current.apply(this, args);
}
};
Ajax
Asynchronous JavaScript and XML (Ajax, or AJAX) is a web development
technique in which a web app fetches content from the server by making
asynchronous HTTP requests, and uses the new content to update the relevant
parts of the page without requiring a full page load. This can make the page more
responsive, because only the parts that need to be updated are requested.

Ajax can be used to create single-page apps, in which the entire web app consists
of a single document, which uses Ajax to update its content as needed.

Initially Ajax was implemented using the XMLHttpRequest interface, but


the fetch() API is more suitable for modern web applications: it is more powerful,
more flexible, and integrates better with fundamental web app technologies such as
service workers. Modern web frameworks also provide abstractions for Ajax.

This technique is so common in modern web development that the specific term
"Ajax" is rarely used.

• AJAX (Asynchronous JavaScript and XML) allows web pages to update


asynchronously by exchanging data with a web server behind the scenes.
• It enhances user experience by updating parts of a web page without
reloading the entire page.

How AJAX Works:

• Uses the XMLHttpRequest object or the Fetch API to send requests to the
server and handle responses.

Example: Simple AJAX Call


const xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.example.com/data", true);
xhr.onload = function() {
if (xhr.status === 200) {
console.log(JSON.parse(xhr.responseText));
}
};
xhr.send();
Using Fetch API:

fetch("https://api.example.com/data")

.then(response => response.json())

.then(data => console.log(data))

.catch(error => console.error("Error:", error));


NETWORKING
Internet addressing in networking is a fundamental concept in web technology.
Here's a brief overview:

Internet Protocol (IP) Address


An IP address is a unique identifier assigned to each device connected to a network,
including the internet. It allows devices to communicate with each other by
specifying the source and destination of data packets2.

Types of IP Addresses
1. IPv4: Uses a 32-bit address scheme allowing for over 4 billion unique
addresses. An example IPv4 address is 192.168.1.1.
2. IPv6: Uses a 128-bit address scheme to accommodate the growing number of
devices. An example IPv6 address is
2001:0db8:85a3:0000:0000:8a2e:0370:7334.

Domain Name System (DNS)


DNS translates human-readable domain names (like www.example.com) into IP
addresses that computers use to identify each other on the network. This makes it
easier for users to access websites without needing to remember complex IP
addresses3.

Subnetting
Subnetting divides a larger network into smaller sub-networks, improving
performance and security. Each subnet has its own range of IP addresses.

Public vs. Private IP Addresses


• Public IP Addresses: Unique across the entire internet, assigned by ISPs.
• Private IP Addresses: Used within a private network (like a home or office)
and not routable on the internet.

Dynamic vs. Static IP Addresses
• Dynamic IP Addresses: Assigned temporarily by a DHCP server and can
change over time.
• Static IP Addresses: Manually configured and remain constant.

IP Address Allocation
The Internet Assigned Numbers Authority (IANA) and regional Internet registries
manage the allocation of IP addresses to ensure they are unique and properly
distributed.
FACTORY METHODS
Factory methods in networking and web technology are often used to create
instances of network-related classes in a way that promotes flexibility and loose
coupling. Here's a detailed look at how factory methods work in this context:

Factory Method Design Pattern

The Factory Method Design Pattern is a creational pattern that provides an interface
for creating objects in a superclass but allows subclasses to alter the type of objects
that will be created. This pattern is particularly useful when a class cannot anticipate
the class of objects it needs to create.

Factory Methods in Java Networking

In Java, factory methods are commonly used in the java.net package to create
instances of network-related classes. Here are some examples:

1. InetAddress.getByName(String host): This static method returns an


InetAddress object representing the IP address of the host specified by the host
parameter.

2. InetAddress.getLocalHost(): This static method returns the InetAddress object


representing the local host.

3. URL.openConnection(): This method opens a connection to the resource


referenced by the URL.

Benefits of Factory Methods

• Encapsulation: Factory methods hide the instantiation logic and provide a


simple interface for creating objects.

• Flexibility: Subclasses can decide which class to instantiate, allowing for


greater flexibility in the code.

• Loose Coupling: Reduces dependencies on specific classes, making the code


more modular and easier to maintain.
Example of Factory Method in Networking

Here's an example of how a factory method might be used in a networking context:


java
public abstract class NetworkConnection {
public abstract void connect();
}

public class TcpConnection extends NetworkConnection {


@Override
public void connect() {
System.out.println("TCP connection established.");
}
}

public class UdpConnection extends NetworkConnection {


@Override
public void connect() {
System.out.println("UDP connection established.");
}
}

public class NetworkFactory {


public static NetworkConnection getConnection(String type) {
if (type == null) {
return null;
}
if (type.equalsIgnoreCase("TCP")) {
return new TcpConnection();
} else if (type.equalsIgnoreCase("UDP")) {
return new UdpConnection();
}
return null;
}
}

public class Main {


public static void main(String[] args) {
NetworkConnection connection = NetworkFactory.getConnection("TCP");
connection.connect(); // Output: TCP connection established.
}
}
In this example, the NetworkFactory class has a static method getConnection that
returns an instance of either TcpConnection or UdpConnection based on the input
parameter. This allows the Main class to create a network connection without
knowing the specific implementation details.

Conclusion

Factory methods are a powerful tool in networking and web technology, providing a
way to create objects in a flexible and loosely coupled manner. They help in
managing complexity and promoting code reusability.

INSTANCE METHODS

Instance methods are functions defined in a class that operate on instances (objects)
of that class. In networking and web technology, these methods are crucial for
performing various operations and managing network communication. Here's a
detailed look at instance methods in this context:

What are Instance Methods?

• Definition: Instance methods are functions that belong to an instance of a


class. They typically manipulate instance variables and perform operations
specific to an object.

• Access: Instance methods have access to the instance variables (self in Python,
this in Java) and other instance methods within the same class.

Instance Methods in Java Networking

In Java, instance methods are frequently used in networking classes to perform


operations related to network communication. Here's an example using the Socket
class from the java.net package:
java
import java.io.*;
import java.net.*;

public class MyNetworkClass {


private Socket socket;
private BufferedReader in;
private PrintWriter out;

public MyNetworkClass(String address, int port) throws IOException {


socket = new Socket(address, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
}

// Instance method to send a message


public void sendMessage(String message) {
out.println(message);
}

// Instance method to receive a message


public String receiveMessage() throws IOException {
return in.readLine();
}

// Instance method to close the connection


public void closeConnection() throws IOException {
in.close();
out.close();
socket.close();
}

public static void main(String[] args) {


try {
MyNetworkClass network = new MyNetworkClass("localhost", 8080);
network.sendMessage("Hello, server!");
System.out.println("Received: " + network.receiveMessage());
network.closeConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Advantages of Instance Methods

• Encapsulation: Instance methods encapsulate behavior specific to instances,


promoting a clean and organized code structure.

• Reuse and Maintenance: They can be reused by different objects, making the
code more maintainable and scalable.

• Inheritance: Instance methods can be overridden in subclasses, providing


flexibility in extending functionality.

Common Networking Operations with Instance


Methods

• Sending and Receiving Data: Instance methods manage the process of sending
and receiving data over a network.

• Managing Connections: They handle establishing, maintaining, and closing


network connections

• Error Handling: Instance methods can include error handling to manage


network-related exceptions.

Understanding and using instance methods effectively is key to developing robust


networking applications. They allow you to create organized, modular, and reusable
code that can handle complex network interactions.
TCP/IP Client Sockets

TCP/IP client sockets are fundamental in networking and web technology, enabling
communication between client and server applications. Here’s a detailed overview:

1. Introduction to Sockets

• Socket: An endpoint for sending or receiving data across a computer network.

• TCP/IP (Transmission Control Protocol/Internet Protocol): A suite of


communication protocols used to interconnect network devices on the internet.
TCP/IP specifies how data should be packetized, addressed, transmitted,
routed, and received.

2. TCP Client Sockets

• TCP (Transmission Control Protocol): Provides reliable, ordered, and error-


checked delivery of a stream of data between applications.

3. Creating a TCP Client Socket

To create a TCP client socket, follow these steps:

a. Socket Creation

1. Initialize Socket: Create a socket using appropriate libraries (e.g., socket in


Python, java.net in Java).

2. Address and Port: Specify the server address and port number to connect to.

b. Connecting to the Server

1. Connect Method: Use the connect() method to establish a connection with


the server.

c. Sending Data

1. Send Method: Use the send() or equivalent method to transmit data to the
server
d. Receiving Data

1. Receive Method: Use the recv() or equivalent method to receive data from the
server.

e. Closing the Socket

1. Close Method: Close the socket connection using the close() method.

4. Example Code

import java.io.*;

import java.net.*;

public class TCPClient { public static void main(String[] args)

{ String serverAddress = "localhost";

int port = 8080;

try (Socket clientSocket = new Socket(serverAddress, port);

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientSocket.getInputStream()))) { // Send data String message =

"Hello, server!";

out.println(message); // Receive response String response = in.readLine();

System.out.println("Received: " + response);


} catch (IOException e) { e.printStackTrace();
}
}
}
URL
What is a URL?

A URL (Uniform Resource Locator) is a reference or address used to access


resources on the internet. It’s a fundamental element of web technology and
networking, providing a means to locate web pages, images, documents, and other
resources.

Structure of a URL

A URL typically consists of several components:

scheme://user:password@host:port/path?query#fragment

Let’s break down these components:

1. Scheme: Specifies the protocol used to access the resource (e.g., http, https,
ftp).
o Example: https://

2. User Info (optional): Username and password for accessing the resource. This
component is less common and often not included due to security concerns.
o Example: user:password@

3. Host: The domain name or IP address of the server where the resource is
located.
o Example: www.example.com

4. Port (optional): The network port on the host to connect to. If omitted, the
default port for the scheme is used (e.g., 80 for HTTP, 443 for HTTPS).
o Example: :8080

5. Path: The specific path to the resource on the server.


o Example: /path/to/resource
6. Query (optional): A string of data to be sent to the server, often used for
search queries or parameters.
o Example: ?search=keyword

7. Fragment (optional): A reference to a section within the resource, often used


to jump to a specific part of a web page.
o Example: #section

Example of a Complete URL

Here's an example URL incorporating these components:

https://user:[email protected]:8080/path/to/resource?search=keyw
ord#section

URL Components in Detail

Scheme

• HTTP (HyperText Transfer Protocol): The foundation of data communication


on the web, used for transferring web pages.
• HTTPS (HTTP Secure): An extension of HTTP, uses encryption (SSL/TLS)
for secure communication.
• FTP (File Transfer Protocol): Used for transferring files between a client and a
server.

User Info
• Not commonly used due to security reasons, but it includes credentials for
accessing resources.
• Example: ftp://username:[email protected]

Host
• Can be a domain name (e.g., www.example.com) or an IP address (e.g.,
192.0.2.1).
• The host component is essential for locating the server where the resource
resides.
Port
• Indicates the port number on the server to connect to.
• Default ports are typically implied: 80 for HTTP, 443 for HTTPS, and 21 for
FTP.

Path

• Specifies the exact location of the resource on the server.


• Structured as a hierarchical path, often reflecting the directory structure on the
server.

Query

• Contains key-value pairs separated by &, used for passing parameters to the
server.
• Example: ?id=123&name=John

Fragment

• Allows direct access to a specific part of a resource.


• Commonly used in HTML documents to jump to a section identified by an id
attribute.

URL Encoding

• Special characters in URLs are encoded to ensure they are transmitted


correctly over the internet.
• Spaces are encoded as %20, a

URL and URI

• URI (Uniform Resource Identifier): A broader term encompassing URLs and


URNs (Uniform Resource Names).
• URLs are a subset of URIs, providing a means to access a resource.

Understanding URLs is crucial for navigating the web, developing web applications,
and configuring network services. URLs are the backbone of how we access and
share resources on the internet.
URL Composition
A URL connection in networking and web technology is typically managed through
a class or library that allows you to connect to a URL and perform various operations
such as sending requests and receiving responses. Let's delve into the details, using
Java as an example for illustration since it has a robust URLConnection class.

Understanding URLConnection in Java

1. What is URLConnection?

URLConnection is a class in Java that represents a communication link between the


application and a URL. It provides methods to read from and write to the URL.

Basic Steps to Use URLConnection

1. Create a URL Object


o Definition: The URL class in Java represents a URL and its
components.
o Example:
java
URL url = new URL("https://www.example.com");

2. Open a Connection
o Definition: The openConnection() method of the URL class returns a
URLConnection object that represents a connection to the remote object
referred to by the URL.
o Example:
Java

URLConnection connection = url.openConnection();

3. Set Request Properties (Optional)


o Definition: You can set various request properties (headers) before
connecting to the URL.
o Example:
java
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
4. Connect to the URL
o Definition: Although not always necessary, you can explicitly connect
to the URL.
o Example:
java
connection.connect();

5. Read from the URL


o Definition: You can read the content of the URL using an input stream.
o Example:
java
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();

6. Write to the URL (Optional)


o Definition: You can write data to the URL using an output stream if the
URL supports output.
o Example:
java
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("data");
out.close();

Complete Example in Java

Here's a complete example that demonstrates how to read content from a URL using
URLConnection:
java
import java.io.*;
import java.net.*;

public class URLConnectionExample {


public static void main(String[] args) {
try {
// Create a URL object
URL url = new URL("https://www.example.com");

// Open a connection to the URL


URLConnection connection = url.openConnection();

// Set request properties


connection.setRequestProperty("User-Agent", "Mozilla/5.0");

// Read from the URL


BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Other Use Cases of URLConnection

• HTTP Requests: For making HTTP requests, you can use


HttpURLConnection, which is a subclass of URLConnection specialized for
HTTP.

• File Downloads: URLConnection can be used to download files from the


internet by reading the input stream and saving it to a local file.

• REST APIs: You can use URLConnection to connect to RESTful web


services, send GET/POST requests, and parse the responses.
Advanced Features

• Timeouts: Set connection and read timeouts to avoid indefinite waiting.


java
connection.setConnectTimeout(5000); // 5 seconds
connection.setReadTimeout(5000); // 5 seconds

• Authentication: Handle authentication mechanisms such as Basic Auth by


setting appropriate headers.

• Handling Cookies: Manage cookies by setting and reading Cookie headers.


Benefits of Using URLConnection

• Simplicity: Provides a straightforward way to connect to and interact with


URLs.

• Flexibility: Supports various protocols (HTTP, HTTPS, FTP, etc.) and allows
customization through request properties.

• Integration: Easily integrates with other Java I/O streams for reading and
writing data.

Understanding URL connections is crucial for tasks like web scraping, accessing
web services, and automating web interactions.
TCP/IP Server Sockets

TCP/IP server sockets are essential for establishing communication


between a server and its clients over the internet. They enable the server
to listen for incoming connections, accept them, and handle data
transmission.
Here's a detailed look at TCP/IP server sockets:

1. What is a TCP/IP Server Socket?

A TCP/IP server socket is a software endpoint that listens for incoming


TCP connections on a specific IP address and port. It allows the server to
communicate with clients using the TCP protocol, which ensures reliable,
ordered, and error-checked data transmission.

2. Creating a TCP/IP Server Socket

To create a TCP/IP server socket, you typically follow these steps:

a. Create a Server Socket

1. Define the Server Address and Port: Choose an IP address and port
number for the server to listen on.
2. Create the Server Socket: Use a socket library to create a server
socket.

b. Bind the Server Socket

1. Bind the Socket to the Address and Port: Associate the server socket
with the IP address and port number.

c. Listen for Connections

1. Listen for Incoming Connections: The server socket starts listening


for incoming connection requests from clients.
d. Accept Connections

1. Accept Incoming Connections: When a client attempts to connect,


the server socket accepts the connection, creating a new socket for
communication with the client.

3. Example in Java

Here's an example of how to create a TCP/IP server socket in Java:

import java.io.*;
import java.net.*;

public class TCPServer {


public static void main(String[] args) {
int port = 12345; // Port number
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server is listening on port " + port);
try (Socket clientSocket = serverSocket.accept();
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()))) {

String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received: " + inputLine);
out.println("Echo: " + inputLine);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Key Points of TCP/IP Server Sockets

• Reliable Communication: TCP ensures that data is delivered reliably


and in the correct order.

• Connection-Oriented: A connection must be established between the


server and client before data exchange.

• Port Numbers: Each server socket is associated with a specific port


number, which clients use to connect.

5. Use Cases

• Web Servers: Serve web pages to clients.

• Email Servers: Handle incoming and outgoing emails.

• File Transfer Servers: Manage file uploads and downloads.

6. Advanced Features

• Non-Blocking I/O: Use non-blocking I/O for handling multiple


connections simultaneously.

• Threaded Servers: Create a multi-threaded server to handle multiple


clients concurrently.

• SSL/TLS: Implement SSL/TLS for secure communication.

Understanding TCP/IP server sockets is crucial for developing networked


applications that require reliable and secure communication.
DATAGRAM
A datagram is a self-contained, independent packet of data that is sent
over a network without the need for a pre-established connection between
the sender and receiver. Datagrams are used primarily in the User
Datagram Protocol (UDP), one of the core protocols of the Internet
Protocol (IP) suite. Here's a detailed look at datagrams and their role in
networking and web technology:

1. What is a Datagram?

• Definition: A datagram is a basic transfer unit associated with a


packet-switched network. It contains enough information to be
routed from the source to the destination without relying on earlier
exchanges between the host and the destination.

• Characteristics:

o Connectionless: Datagrams are sent without establishing a


connection, making the communication faster but less reliable.

o Self-contained: Each datagram contains all the necessary


information for routing to the destination.

o No Acknowledgment: The sender does not wait for an


acknowledgment from the receiver, leading to faster
transmission but potential data loss.

2. User Datagram Protocol (UDP)

• Definition: UDP is a transport layer protocol that uses datagrams for


communication. It is designed for applications that require efficient
transmission of data without the overhead of establishing and
maintaining a connection.
Features:
o Simple and Fast: UDP has low latency and overhead, making
it suitable for applications where speed is critical.

o Unreliable: There is no guarantee that datagrams will be


delivered or received in order.

o No Flow Control: UDP does not provide mechanisms for flow


control or congestion control.

3. Datagram Structure

A typical datagram consists of two main parts:

a. Header

• Source Port: The port number of the sender.

• Destination Port: The port number of the receiver.

• Length: The length of the datagram, including header and data.

• Checksum: A value used for error-checking the header and data.

b. Data

• Payload: The actual data being transmitted. The size of the payload
can vary depending on the application and the network's maximum
transmission unit (MTU).

4. Use Cases of Datagram and UDP

• Streaming Media: Audio and video streaming often use UDP


because it tolerates some packet loss and prioritizes speed.
• Online Gaming: Fast-paced games use UDP to reduce latency,
accepting occasional data loss.

• DNS (Domain Name System): DNS queries and responses use


UDP to resolve domain names quickly.

4. Example Code

Datagram Server (Receiver)

The server listens for incoming datagrams and prints the received data.

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class DatagramServer {


public static void main(String[] args) {
DatagramSocket socket = null;
try {
// Create a socket to listen on port 9876
socket = new DatagramSocket(9876);
byte[] receiveBuffer = new byte[1024];

System.out.println("Server is listening on port 9876...");

// Infinite loop to keep the server running


while (true) {
// Prepare a packet to receive data
DatagramPacket receivePacket = new
DatagramPacket(receiveBuffer, receiveBuffer.length);
socket.receive(receivePacket); // Receive packet
// Extract data from the received packet
String message = new String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Received: " + message);

// Sending acknowledgment to the client


String ackMessage = "Ack: " + message;
byte[] ackBuffer = ackMessage.getBytes();
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
DatagramPacket ackPacket = new DatagramPacket(ackBuffer,
ackBuffer.length, clientAddress, clientPort);
socket.send(ackPacket); // Send acknowledgment
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
}
Datagram Client (Sender)
The client sends a message to the server and waits for an
acknowledgment.
java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class DatagramClient {


public static void main(String[] args) {
DatagramSocket socket = null;
try {
// Create a socket
socket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");

// Message to be sent
String message = "Hello, Datagram Server!";
byte[] sendBuffer = message.getBytes();

// Create a packet to send data


DatagramPacket sendPacket = new DatagramPacket(sendBuffer,
sendBuffer.length, serverAddress, 9876);
socket.send(sendPacket); // Send packet

// Prepare buffer to receive acknowledgment


byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new
DatagramPacket(receiveBuffer, receiveBuffer.length);
socket.receive(receivePacket); // Receive acknowledgment

// Extract acknowledgment message


String ackMessage = new String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Received: " + ackMessage);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
}
How It Works

1. DatagramServer.java:

o Creates a DatagramSocket listening on port 9876.


o Continuously waits for incoming DatagramPackets.
o Prints the received message and sends an acknowledgment
back to the client.

2. DatagramClient.java:

o Creates a DatagramSocket and determines the server's IP


address (localhost in this example).
o Prepares a message to be sent and creates a DatagramPacket.
o Sends the packet to the server and waits for an
acknowledgment.
o Prints the acknowledgment received from the server.

Explanation of Key Components

• DatagramSocket: The main class for creating sockets for sending


and receiving datagram packets.

• DatagramPacket: Represents the data being sent or received in a


network packet.

• send(): Sends a datagram packet from the client to the server.

• receive(): Receives a datagram packet at the server.

This example demonstrates the basics of using datagrams in Java for


network communication.

You might also like