Chapter no 4 Marks 10 Mrs. A. V. Chechare Networking Basics
Networking in java 1. Networking is connecting different nodes, devices or PCs together for data sharing and communication purpose. 2. Simply writing programs that can be executed over various computer devices, in which all the devices are connected to each other to share resources using a network.
1. All the Java program communications over the network are done at the application layer. 2. The java.net package is used for networking in java. Important Terminology: ⚫ IP Address ⚫ Protocol ⚫ Port Number ⚫ MAC Address ⚫ Connection-oriented and connection-less protocol ⚫ Socket
Socket ⚫ A socket is one endpoint of a two-way communication link between two programs running on different computers on a network.
Client Server Architecture A server is the one who provides requested services. Clients are the ones who request services.
⚫ So in java programming for establishing communication between two devices we need to create 2 sockets one for Server i.e ServerSocket and another for Client i.e ClientSocket(Socket).
1.Server runs on a specific computer and has a socket that is bound to a specific port number. 2. The server instantiates a ServerSocket object, indicating at which port number communication will occur. ServerSocket server = new ServerSocket(port); 3. After creating object Server calls accept() method and keeps on waiting for client request to connect server.accept();
4.On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. 5.To make a connection request, the client tries to connect with the server on the server's machine and port. 6. If everything goes well, the server accepts the connection.
7.Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. For Above explained communication to take place we take help from following packages and classes in java. 1. java.net 2. java.net.ServerSocket 3. java.net.Socket
Reserved Ports ⚫ Reserved/Well-known ports—Ports in the range 0 to 1023 are assigned and controlled. ⚫ Registered ports—Ports in the range 1024 to 49151 are not assigned or controlled, but can be registered to prevent duplication. ⚫ Dynamic ports—Ports in the range 49152 to 65535 are not assigned, controlled, or registered. They are used for temporary or private ports. They are also known as private or non-reserved ports.
Reserved Sockets in Java ⚫ The Sockets Which Are Reserved By Specific Protocols For Communication Are Reserved Sockets. ⚫ Once The Connection Is Ready A Higher-Level Protocol Is In Use. ⚫ This Is Dependent On The Port Which You Are Using. ⚫ TCP/IP Uses Or Reserves The Lower 1024 Ports For Specific Protocols. Examples: 1. FTP – Port No. 21 2. TELNET- Port No. 23 3. E-Mail – Port No. 25 4. Finger- Port No. 79
20 File Transfer Protocol (FTP) Data Transfer 22 Secure Shell (SSH) Secure Login 53 Domain Name System (DNS) service 67, 68 Dynamic Host Configuration Protocol (DHCP) 80 Hypertext Transfer Protocol (HTTP) used in the World Wide Web 110 Post Office Protocol (POP3) 123 Network Time Protocol (NTP) 143 Internet Message Access Protocol (IMAP) Management of digital mail 161 Simple Network Management Protocol (SNMP) 194 Internet Relay Chat (IRC) 443 HTTP Secure (HTTPS) HTTP over TLS/SSL
Proxy Server ⚫ A proxy server is a system or router that provides a gateway between users and the internet. ⚫ Therefore, it helps prevent cyber attackers from entering a private network. It is a server, referred to as an “intermediary” because it goes between end-users and the web pages they visit online.
1. A proxy server performs the function of a firewall and filter. 2. The end-user or a network administrator can choose a proxy designed to protect data and privacy. 3. This examines the data going in and out of your computer or network. 4. It then applies rules to prevent you from having to expose your digital address to the world. 5. Only the proxy’s IP address is seen by hackers or other bad actors. 6. Without your personal IP address, people on the internet do not have direct access to your personal data, schedules, apps, or files.
Internet Addressing ⚫ An Internet Protocol (IP) address is a unique numerical identifier for every device or network that connects to the internet. ⚫ Typically assigned by an internet service provider (ISP), an IP address is an online device address used for communicating across the internet. ⚫ Java InetAddress class represents an IP address ⚫ An IP address helps to identify a specific resource on the network using a numerical representation. ⚫ An IP address is represented by 32-bit or 128-bit unsigned number.
IPV4 ⚫ IPv4 is the primary Internet protocol. ⚫ It is a widely used IP version to differentiate devices on network using an addressing scheme. ⚫ A 32-bit addressing scheme is used to store 232 addresses that is more than 4 million addresses. ⚫ It is a connectionless protocol. ⚫ It utilizes less memory and the addresses can be remembered easily with the class based addressing scheme. For example:
IPV6 ⚫ IPv6 is the latest version of Internet protocol. ⚫ It aims at fulfilling the need of more internet addresses. ⚫ It provides solutions for the problems present in IPv4. ⚫ It provides 128-bit address space that can be used to form a network of 340 undecillion unique IP addresses. ⚫ IPv6 is also identified with a name IPng (Internet Protocol next generation).
Prefix: The prefix part of IP address identifies the physical network to which the computer is attached. . Prefix is also known as a network address. Suffix: The suffix part identifies the individual computer on the network. The suffix is also called the host address.
IPV4 classes
⚫ This IP address class is used when there are a large number of hosts. ⚫ In a Class A type of network, the first 8 bits (also called the first octet) identify the network, and the remaining have 24 bits for the host into that network. ⚫ example :102.168.212.226. Here, “102” helps you identify the network and 168.212.226 identify the host. ⚫ Class A addresses 127.0.0.0 to 127.255.255.255 cannot be used and is reserved for loopback and diagnostic Class A
Class B ⚫ In this IP address, the class decimal number that can be between 128 to 191. ⚫ The number 127 is reserved for loopback, which is used for internal testing on the local machine. ⚫ The first 16 bits (known as two octets) help you identify the network. The other remaining 16 bits indicate the host within the network. ⚫ An example of Class B IP address is 168.212.226.204, where *168 212* identifies the network and *226.204* helps you identify the network host.
Class C ⚫ Class C is a type of IP address that is used for the small network. ⚫ In this class, three octets are used to indent the network. This IP ranges between 192 to 223. ⚫ Example 192.168.178.1
Class D ⚫ Class D addresses are only used for multicasting applications. ⚫ Class D is never used for regular networking operations. ⚫ This class addresses the first three bits set to “1” and their fourth bit set to use for “0”. ⚫ Class D addresses are 32-bit network addresses. ⚫ All the values within the range are used to identify multicast groups uniquely.
Class E ⚫ Class E IP address is defined by including the starting four network address bits as 1, which allows you two to incorporate addresses from 240.0.0.0 to 255.255.255.255. ⚫ E class is reserved, and its usage is never defined. ⚫ Therefore, many network implementations discard these addresses as undefined or illegal. ⚫ Example 243.164.89.28
TCP ⚫ The TCP stands for Transmission Control Protocol. ⚫ If we want the communication between two computers and communication should be good and reliable. ⚫ TCP is Connection Oriented Protocol ⚫ It uses Three Way handshake mechanism.
UDP ⚫ The UDP stands for User Datagram Protocol. ⚫ UDP is a connectionless protocol. ⚫ Here, connectionless means that no connection establishes prior to communication. ⚫ It also does not guarantee the delivery of data packets. ⚫ It does not even care whether the data has been received on the receiver's end or not, so it is also known as the "fire-and-forget" protocol.. ⚫ UDP is faster than TCP as it does not provide
Working with UDP DatagramSockets ⚫ Creation of DatagramSocket:- 1. public DatagramSocket() 2. public DatagramSocket(int port) 3. public DatagramSocket(int port, InetAddress inetaddress)
⚫ Creation of DatagramPacket: Constructor to send data: public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) buf - the packet data. offset - the packet data offset. length - the packet data length. address - the destination socket address.
⚫ Constructor to receive the data: ⚫ Syntax: public DatagramPacket(byte buf[], int length) buf - the packet data. length - the packet data length.
⚫ Invoke a send() or receive() call on socket object ⚫ void send(DatagramPacket packet) ⚫ void receive(DatagramPacket packet)
InetAddress ⚫ The InetAddress class is used to encapsulate both the numerical IP address and the domain name for that address. ⚫ We interact with this class by using the name of an IP host, which is more convenient and understandable than its IP address. ⚫ The InetAddress class hides the number inside. ⚫ InetAddress can handle both IPv4 and IPv6 addresses
1.static InetAddress getLocalHost( ) This method returns the InetAddress object that represents the local host. 2.static InetAddress getByName(String hostName) it returns an InetAddress for a host name passed to it 3.static InetAddress[ ] getAllByName(String hostName) This method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to.
URL ⚫ URL known as Uniform Resource Locator ⚫ It is simply a string of text that identifies all the resources on the Internet, telling us the address of the resource, how to communicate with it, and retrieve something from it. ⚫ The Web is a loose collection of higher-level protocols and file formats, all unified in a web browser. ⚫ The URL provides a reasonably intelligible form to uniquely identify or address information on the Internet.
Components of URL Htttp://www.google.com/flower.jpeg Protoco l Domain Name/Host name File Protocol: HTTP is the protocol here Hostname: Name of the machine on which the resource lives. File Name: The pathname to the file on the machine. Port Number: Port number to which to connect (typically optional) http://www.google.com:80/flower.j peg Port No

Networking Basics1ofjavaprogramming.pptx.pdf

  • 1.
    Chapter no 4Marks 10 Mrs. A. V. Chechare Networking Basics
  • 2.
    Networking in java 1.Networking is connecting different nodes, devices or PCs together for data sharing and communication purpose. 2. Simply writing programs that can be executed over various computer devices, in which all the devices are connected to each other to share resources using a network.
  • 3.
    1. All theJava program communications over the network are done at the application layer. 2. The java.net package is used for networking in java. Important Terminology: ⚫ IP Address ⚫ Protocol ⚫ Port Number ⚫ MAC Address ⚫ Connection-oriented and connection-less protocol ⚫ Socket
  • 4.
    Socket ⚫ A socketis one endpoint of a two-way communication link between two programs running on different computers on a network.
  • 5.
    Client Server Architecture Aserver is the one who provides requested services. Clients are the ones who request services.
  • 6.
    ⚫ So injava programming for establishing communication between two devices we need to create 2 sockets one for Server i.e ServerSocket and another for Client i.e ClientSocket(Socket).
  • 7.
    1.Server runs ona specific computer and has a socket that is bound to a specific port number. 2. The server instantiates a ServerSocket object, indicating at which port number communication will occur. ServerSocket server = new ServerSocket(port); 3. After creating object Server calls accept() method and keeps on waiting for client request to connect server.accept();
  • 8.
    4.On the client-side:The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. 5.To make a connection request, the client tries to connect with the server on the server's machine and port. 6. If everything goes well, the server accepts the connection.
  • 9.
    7.Upon acceptance, theserver gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. For Above explained communication to take place we take help from following packages and classes in java. 1. java.net 2. java.net.ServerSocket 3. java.net.Socket
  • 10.
    Reserved Ports ⚫ Reserved/Well-knownports—Ports in the range 0 to 1023 are assigned and controlled. ⚫ Registered ports—Ports in the range 1024 to 49151 are not assigned or controlled, but can be registered to prevent duplication. ⚫ Dynamic ports—Ports in the range 49152 to 65535 are not assigned, controlled, or registered. They are used for temporary or private ports. They are also known as private or non-reserved ports.
  • 11.
    Reserved Sockets inJava ⚫ The Sockets Which Are Reserved By Specific Protocols For Communication Are Reserved Sockets. ⚫ Once The Connection Is Ready A Higher-Level Protocol Is In Use. ⚫ This Is Dependent On The Port Which You Are Using. ⚫ TCP/IP Uses Or Reserves The Lower 1024 Ports For Specific Protocols. Examples: 1. FTP – Port No. 21 2. TELNET- Port No. 23 3. E-Mail – Port No. 25 4. Finger- Port No. 79
  • 12.
    20 File TransferProtocol (FTP) Data Transfer 22 Secure Shell (SSH) Secure Login 53 Domain Name System (DNS) service 67, 68 Dynamic Host Configuration Protocol (DHCP) 80 Hypertext Transfer Protocol (HTTP) used in the World Wide Web 110 Post Office Protocol (POP3) 123 Network Time Protocol (NTP) 143 Internet Message Access Protocol (IMAP) Management of digital mail 161 Simple Network Management Protocol (SNMP) 194 Internet Relay Chat (IRC) 443 HTTP Secure (HTTPS) HTTP over TLS/SSL
  • 13.
    Proxy Server ⚫ Aproxy server is a system or router that provides a gateway between users and the internet. ⚫ Therefore, it helps prevent cyber attackers from entering a private network. It is a server, referred to as an “intermediary” because it goes between end-users and the web pages they visit online.
  • 14.
    1. A proxyserver performs the function of a firewall and filter. 2. The end-user or a network administrator can choose a proxy designed to protect data and privacy. 3. This examines the data going in and out of your computer or network. 4. It then applies rules to prevent you from having to expose your digital address to the world. 5. Only the proxy’s IP address is seen by hackers or other bad actors. 6. Without your personal IP address, people on the internet do not have direct access to your personal data, schedules, apps, or files.
  • 15.
    Internet Addressing ⚫ AnInternet Protocol (IP) address is a unique numerical identifier for every device or network that connects to the internet. ⚫ Typically assigned by an internet service provider (ISP), an IP address is an online device address used for communicating across the internet. ⚫ Java InetAddress class represents an IP address ⚫ An IP address helps to identify a specific resource on the network using a numerical representation. ⚫ An IP address is represented by 32-bit or 128-bit unsigned number.
  • 16.
    IPV4 ⚫ IPv4 isthe primary Internet protocol. ⚫ It is a widely used IP version to differentiate devices on network using an addressing scheme. ⚫ A 32-bit addressing scheme is used to store 232 addresses that is more than 4 million addresses. ⚫ It is a connectionless protocol. ⚫ It utilizes less memory and the addresses can be remembered easily with the class based addressing scheme. For example:
  • 17.
    IPV6 ⚫ IPv6 isthe latest version of Internet protocol. ⚫ It aims at fulfilling the need of more internet addresses. ⚫ It provides solutions for the problems present in IPv4. ⚫ It provides 128-bit address space that can be used to form a network of 340 undecillion unique IP addresses. ⚫ IPv6 is also identified with a name IPng (Internet Protocol next generation).
  • 19.
    Prefix: The prefixpart of IP address identifies the physical network to which the computer is attached. . Prefix is also known as a network address. Suffix: The suffix part identifies the individual computer on the network. The suffix is also called the host address.
  • 20.
  • 22.
    ⚫ This IPaddress class is used when there are a large number of hosts. ⚫ In a Class A type of network, the first 8 bits (also called the first octet) identify the network, and the remaining have 24 bits for the host into that network. ⚫ example :102.168.212.226. Here, “102” helps you identify the network and 168.212.226 identify the host. ⚫ Class A addresses 127.0.0.0 to 127.255.255.255 cannot be used and is reserved for loopback and diagnostic Class A
  • 23.
    Class B ⚫ Inthis IP address, the class decimal number that can be between 128 to 191. ⚫ The number 127 is reserved for loopback, which is used for internal testing on the local machine. ⚫ The first 16 bits (known as two octets) help you identify the network. The other remaining 16 bits indicate the host within the network. ⚫ An example of Class B IP address is 168.212.226.204, where *168 212* identifies the network and *226.204* helps you identify the network host.
  • 24.
    Class C ⚫ ClassC is a type of IP address that is used for the small network. ⚫ In this class, three octets are used to indent the network. This IP ranges between 192 to 223. ⚫ Example 192.168.178.1
  • 25.
    Class D ⚫ ClassD addresses are only used for multicasting applications. ⚫ Class D is never used for regular networking operations. ⚫ This class addresses the first three bits set to “1” and their fourth bit set to use for “0”. ⚫ Class D addresses are 32-bit network addresses. ⚫ All the values within the range are used to identify multicast groups uniquely.
  • 26.
    Class E ⚫ ClassE IP address is defined by including the starting four network address bits as 1, which allows you two to incorporate addresses from 240.0.0.0 to 255.255.255.255. ⚫ E class is reserved, and its usage is never defined. ⚫ Therefore, many network implementations discard these addresses as undefined or illegal. ⚫ Example 243.164.89.28
  • 27.
    TCP ⚫ The TCPstands for Transmission Control Protocol. ⚫ If we want the communication between two computers and communication should be good and reliable. ⚫ TCP is Connection Oriented Protocol ⚫ It uses Three Way handshake mechanism.
  • 28.
    UDP ⚫ The UDPstands for User Datagram Protocol. ⚫ UDP is a connectionless protocol. ⚫ Here, connectionless means that no connection establishes prior to communication. ⚫ It also does not guarantee the delivery of data packets. ⚫ It does not even care whether the data has been received on the receiver's end or not, so it is also known as the "fire-and-forget" protocol.. ⚫ UDP is faster than TCP as it does not provide
  • 30.
    Working with UDP DatagramSockets ⚫Creation of DatagramSocket:- 1. public DatagramSocket() 2. public DatagramSocket(int port) 3. public DatagramSocket(int port, InetAddress inetaddress)
  • 31.
    ⚫ Creation ofDatagramPacket: Constructor to send data: public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) buf - the packet data. offset - the packet data offset. length - the packet data length. address - the destination socket address.
  • 32.
    ⚫ Constructor toreceive the data: ⚫ Syntax: public DatagramPacket(byte buf[], int length) buf - the packet data. length - the packet data length.
  • 33.
    ⚫ Invoke asend() or receive() call on socket object ⚫ void send(DatagramPacket packet) ⚫ void receive(DatagramPacket packet)
  • 34.
    InetAddress ⚫ The InetAddressclass is used to encapsulate both the numerical IP address and the domain name for that address. ⚫ We interact with this class by using the name of an IP host, which is more convenient and understandable than its IP address. ⚫ The InetAddress class hides the number inside. ⚫ InetAddress can handle both IPv4 and IPv6 addresses
  • 35.
    1.static InetAddress getLocalHost() This method returns the InetAddress object that represents the local host. 2.static InetAddress getByName(String hostName) it returns an InetAddress for a host name passed to it 3.static InetAddress[ ] getAllByName(String hostName) This method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to.
  • 36.
    URL ⚫ URL knownas Uniform Resource Locator ⚫ It is simply a string of text that identifies all the resources on the Internet, telling us the address of the resource, how to communicate with it, and retrieve something from it. ⚫ The Web is a loose collection of higher-level protocols and file formats, all unified in a web browser. ⚫ The URL provides a reasonably intelligible form to uniquely identify or address information on the Internet.
  • 37.
    Components of URL Htttp://www.google.com/flower.jpeg Protoco l DomainName/Host name File Protocol: HTTP is the protocol here Hostname: Name of the machine on which the resource lives. File Name: The pathname to the file on the machine. Port Number: Port number to which to connect (typically optional) http://www.google.com:80/flower.j peg Port No