ch3 Networking
ch3 Networking
PROGRAMMING
COURSE CODE : 223CCS-3
CHAPTER 3: NETWORKING
SUBJECT COORDINATOR: MRS. MARIYAM AYSHA BIVI
SUBJECT TEACHERS: MR. OMER BIN HUSSAIN
MS. SHUMUKH HUSSAIN
DEPARTMENT OF COMPUTER SCIENCE
KING KHALID UNIVERSITY
ABHA, KSA.
Networking Basics
The Networking Classes and Interfaces
URL
URL connection
2
3.1 NETWORKING BASICS
Java’s networking support is the concept of a socket.
A socket identifies an endpoint in a network. socket allows a single computer to
serve many different clients at once, as well as to serve many different types of
information. This is accomplished through the use of a port, which is a numbered
socket on a particular machine.
A server process is said to "listen" to a port until a client connects to it.
A server is allowed to accept multiple clients connected to the same port number
Socket communication takes place via a protocol.
Internet Protocol (IP) : low-level routing protocol that breaks data into small
packets and sends them to an address across a network, which does not
guarantee to deliver said packets to the destination.
Transmission Control Protocol (TCP) : higher-level protocol that manages to
robustly string together these packets, sorting and retransmitting them as
necessary to reliably transmit data.
User Datagram Protocol (UDP) : sits next to TCP and can be used directly to
support fast, connectionless, unreliable transport of packets. 3
Hyper Text Transfer protocol(HTTP): The protocol that web browsers and
servers use to transfer hyper-text pages and images
3.2 THE NETWORKING CLASSES AND INTERFACES
The java.net package provides the classes that support socket-based client/server
communication
Java supports both the TCP and UDP protocol families
Some of java.net package classes:
InetAddress
URL
URLConnection
Socket
ServerSocket
DatagramSocket
DatagramPacket
Socket
ServerSocket
SocketAddress
Inet4Address 4
To access the actual bits or content information of a URL, create a URLConnection object
from it, using its openConnection( ) method, like this:
urlc = url.openConnection()
openConnection( ) has the following general form:
URLConnection openConnection( ) throws IOException 7
3.4 URL (CONT..)
PROGRAM 2: P2_URL_Demo.java
import java.net.*;
class P2_URL_Demo { OUTPUT OF PROGRAM 2:
public static void main (String[] args) throws MalformedURLException { Protocol : http
URL u1 = new URL("http://www.kku.edu.sa:80/index.html"); Host Name : www.kku.edu.sa
System.out.println("Protocol : "+u1.getProtocol()); Port Number : 80
// Protocol : http File Name : /index.html
System.out.println("Host Name : "+u1.getHost()); Ext : http://www.kku.edu.sa:80/index.html
// Host Name : www.kku.edu.sa Protocol : http
System.out.println("Port Number : "+u1.getPort()); / Host Name : www.kku.edu.sa
// Port Number : 80
Port Number : 80
System.out.println("File Name : "+u1.getPath());
File Name : index.html
// File Name : /index.html
Ext : http://www.kku.edu.sa:80index.html
System.out.println("Ext : "+u1.toExternalForm());
Protocol : http
// Ext : http://www.kku.edu.sa:80/index.html
Host Name : www.kku.edu.sa
Port Number : -1
URL u2 = new URL("http","www.kku.edu.sa",80,"index.html");
File Name : index.html
System.out.println("Protocol : "+u2.getProtocol()); // Protocol : http
Ext : http://www.kku.edu.saindex.html
System.out.println("Host Name : "+u2.getHost());
// Host Name : www.kku.edu.sa
Process completed.
System.out.println("Port Number : "+u2.getPort()); // Port Number : 80
System.out.println("File Name : "+u2.getPath()); // File Name : /index.html
System.out.println("Ext : "+u2.toExternalForm()); // Ext
If the port is –1; this means that a port
was not explicitly set.
URL u3 = new URL("http","www.kku.edu.sa","index.html");
System.out.println("Protocol : "+u3.getProtocol()); // Protocol : http
System.out.println("Host Name : "+u3.getHost());
// Host Name : www.kku.edu.sa
8
System.out.println("Port Number : "+u3.getPort()); // Prot Number : 80
System.out.println("File Name : "+u3.getPath()); // File Name : /index.html
System.out.println("Ext : "+u3.toExternalForm()); // Ext
} }
3.5 URL CONNECTION
URLConnection is a general-purpose class for accessing the attributes of a remote
resource.
Once a connection to a remote server is made, URLConnection can be used to inspect the
properties of the remote object before actually transporting it locally.
These attributes are exposed by the HTTP protocol specification
URLConnection defines several methods.
Int getContentLengthLong()
Sting getContentType()
Long getDate()
Long getExpiration()
Long getLastModified()
InputStream getInputStream() throws IOException
9
3.5 URL CONNECTION (CONT..)
PROGRAM 3: P3_URLConnection_Demo.java
// Demonstrate URLConnection.
import java.net.*; OUTPUT OF PROGRAM 3:
import java.io.*; Date: Thu Oct 19 00:05:58 AST 2023
import java.util.Date; Content-Type: text/html
class P3_URLConnection_Demo { No expiration information.
public static void main(String args[]) throws Exception No last-modified information.
{ Content-Length: 8
int c; === Content ===
URL hp = new URL("http://www.yahoo.com"); redirect
URLConnection hpCon = hp.openConnection(); // get date Process completed.
long d = hpCon.getDate();
if(d==0) System.out.println("No date information.");
else System.out.println("Date: " + new Date(d)); // get content type
System.out.println("Content-Type: " + hpCon.getContentType());
d = hpCon.getExpiration(); // get expiration date
if (d==0) System.out.println("No expiration information.");
else System.out.println("Expires: " + new Date(d)); //get last-modified date
d = hpCon.getLastModified();
if (d==0) System.out.println("No last-modified information.");
else System.out.println("Last-Modified: " + new Date(d)); //get content length
long len = hpCon.getContentLengthLong();
if(len == -1) System.out.println("Content length unavailable.");
else System.out.println("Content-Length: " + len);
if(len != 0)
{ System.out.println("=== Content ===");
InputStream input = hpCon.getInputStream();
while (((c = input.read()) != -1)) 10
{ System.out.print((char) c);
} input.close();
} else { System.out.println("No content available.");
} } }
3.6 TCP/IP CLIENT SOCKETS
TCP/IP sockets are used to implement reliable, bidirectional, persistent, pointto-point,
stream-based connections between hosts on the Internet.
A socket can be used to connect Java’s I/O system to other programs that may reside
either on the local machine or on any other machine on the Internet.
Two kinds of TCP sockets in java.net package :
ServerSocket class: for servers, designed to be a “listener” which waits for clients to
connect before doing anything.
Socket class: for clients, designed to connect to server socket and initiate protocol
exchanges
The creation of a Socket object establishes a connection between the client and server.
Two constructors used to create client sockets:
sc.close(); pw.close();
s.close(); s.close();
} }
} }