DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
CS1401 – INTERNET PROGRAMMING BRANCH – CSE
SEMESTER - VII
UNIT – I: BASIC NETWORK AND WEB CONCEPTS Internet standards – TCP and UDP protocols – URLs – MIME – CGI – Introduction to SGML.
UNIT – II: JAVA PROGRAMMING Java basics – I/O streaming – files – Looking up Internet address – Socket programming – Client/Server programs – E-mail client – SMTP – POP3 programs – web page retrieval – protocol handlers – content handlers – applets – image handling – Remove Method Invocation.
UNIT – III: SCRIPTING LANGUAGES HTML forms – frames – tables – web page design – Java Script introduction – control structures – functions – arrays – objects – simple web applications
UNIT – IV: DYNAMIC HTML Dynamic HTML – introduction – cascading style sheets (CSS) – object model and collections – event model – filters and transition – data binding – data control – ActiveX control – handling of multimedia data.
UNIT – V: SERVER SIDE PROGRAMMING Servlets –deployment of simple servlets – web server (Java web server / Tomcat / Web logic) – HTTP GET and POST requests – session tracking – cookies – JDBC – simple web applications – multi-tier applications.
TEXT BOOKS 1. Deitel, Deitel and Nieto, “Internet and WWW – how to program”, Pearson Education Publishers, 2000. 2. Elliotte Rusty Harold, “Java Network Programming”, O’Reilly Publishers, 2002
REFERENCES 1. R. Krishnamoorthy & S. Prabhu, “Internet and java programming”, New Age International publishers, 2004. 2. Thomno A. Powell, “The complete reference HTML and XHTML”, Fourth Edition, Tata McGraw Hill, 2003. 3. Naughton, “The Complete Reference – Java2”, Tata McGraw-Hill, 3rd Edition, 1999.
2
CS1401 – INTERNET PROGRAMMING UNIT I – BASIC NETWORK AND WEB CONCEPTS 1.1 Internet Standards 1.2 TCP and UDP protocols 1.3 URLs 1.4 MIME 1.5 CGI 1.6 Introduction to SGML.
1.1INTERNET STANDARDS The Internet is a system that allows the computers all over the world to communicate with each other. The Internet is a network of network that connects computers all over the world. The computers on the Internet can talk to each other because they are networked. All the systems in the network can share or exchange information electronically because of the network connection only. The systems are connected through local and long distance telephone lines, and some even use wireless satellite communications.
CHARACTERISTICS OF INTERNET •
It is a Loose Organization. Internet is not a big organization of company or club. It is a Loose Organization. The computers on the internet are controlled by their owners. Nobody controls the Internet.
•
It is a Relaxing Place. Internet enables users to exchange information in an open and public way. Internet provides various information about Film, TV, Medicine, Nutrition, Astrology, Politics, Employment, Games, and Mails etc.
•
It is a Business Tool Big companies relies computers in internet for business communication. Nowadays, several organizations are running their business through internet for various purposes like exchange of information, material specifications, payments and billings etc.
•
It is an Information Pool Internet is a pool of information. The information the users in need, it can be easily located and retrieved from the internet.
3
•
Internet satisfies the various levels of users like kids, from students to business professionals, from small industries to multinational Companies Researchers and Government Organizations including Military Services.
INTERNET STANDARD (STD) •
In computer network an Internet Standard (STD) is a normative specification or set of rules or a technology or methodology applicable to the Internet.
•
Internet Standards are created and published by the Internet Engineering Task Force (IETF).
•
An Internet Standard is a Special Request For Comments (RFC) or set of RFCs.
•
An RFC that is to become a Standard or part of a Standard begins as an Internet Draft, and is later (usually after several revisions) accepted and published by the RFC Editor as a RFC and labeled a Proposed Standard.
•
RFC is labelled a Draft Standard, and finally a Standard.
•
Collectively, these stages are known as the standards track, and are defined in RFC 2026.
•
The label Historic is applied to deprecated standards-track documents or obsolete RFCs that were published before the standards track was established.
• •
Only the IETF, represented by the Internet Engineering Steering Group (IESG), can approve standards-track RFCs. The definitive list of Internet Standards is maintained in Internet Standards Document STD 1: Internet Official Protocol Standards.
EXPERIMENTAL STANDARD A proposal after being submitted is considered as an experimental standard.
4
PROPOSED STANDARD A Proposed Standard (PS) is Stable well-understood, has received Significant community review, and appears to enjoy enough community interest to be considered valuable. It is provided by IETF (Internet Engineering Task Force). IETF include TCP/IP, MIME and SMTP. IETF standards are published as Internet Draft and Request For Comments (RFCs). It deals from informational documents to detailed specification of standard Internet Protocol. RFCs documents are published with the help of Internet Engineering Steering Group (IESG).
DRAFT STANDARD A Specification from which at least two independent and interoperable implementations from different code bases have been developed, and for which sufficient successful operational experience has been obtained, may be elevated to the Draft Standard (DS) level. A Draft Standard is normally considered to be a final specification, and changes are likely to be made only to solve specific problems encountered. In most circumstances, it is reasonable for vendors to deploy implementations of Draft Standards into a disruption sensitive environment.
EDIT STANDARD A Specification for which significant implementation and successful operational experience has been obtained may be elevated to the Internet Standard (STD) level. An Internet Standard, which may simply be referred to as a Standard, is characterized by a high degree of technical maturity and by a generally held belief that the specified protocol or service provides significant benefit to the Internet community
WHY NETWORKING IN JAVA IS ESSENTIAL? •
Java is the first programming language designed from the ground up with networking in mind.
•
As the global internet continues to grow, java is uniquely suited to build the next generation of network applications.
5
•
Java is platform independence, it also provides security. The Java programs compiled one computer can be executed in some other computer because of the platform independency.
•
It makes network programs easy. It is easy for java applications to send and receive data across the internet. The client-server implementation using HTTP, FTP and POP3 is also very easy using Java programs.
•
It is also possible for applets to communicate across the internet, though they are limited by security restrictions.
WHAT CAN A NETWORK PROGRAM DO? •
Networking adds a lot of power to simple programs. With networks, a single program can retrieve information stored in millions of computers located anywhere in the world.
•
A single program can communicate with tens of millions of people. A single program can harness (tie together) the power of many computers to work on one problem.
•
Network applications generally take one of several forms. The distinction is clients and servers.
•
In the simplest case, clients retrieve data from a server and display it. More complex clients filter and reorganize data, repeatedly retrieve changing data, send data to other people and computers, and interact with peers in real time for chat, multiplayer games, or collaboration.
HOW JAVA IS USEFUL IN RETRIEVING DATA AND TO DISPLAY? •
A network client retrieves data from a server and shows it to a user.
•
Web browsers are limited. They can talk to only certain kinds of servers (Generally web, FTP, gopher, and perhaps mail and news servers.
•
They can understand and display certain kinds of data (generally text, HTML, and a few standard image formats.)
•
A java program can send SQL queries in a database.
6
•
•
A Java program can connect to a network time-server to synchronize itself with an atomic clock. A java program can speak any custom protocols it needs to speak, including the one to control the HAWC (High Resolution Airborne Wideband Camera) on the Stratospheric Observatory for Infrared Astronomy (SOFIA). A java program can use the full power of a modern graphical user interface to show this data to the user and get a response to it.
1.1.2 REPEATEDLY RETRIEVE DATA •
Used sending data continuously.
•
File storage
•
Massively parallel computing
•
Smart forms – includes various controls like text box, radio button, text area control etc.
•
Peer-to-Peer Interaction – used in various client/server models.
•
Games – Combine the ability to easily include networking in your programs with java’s powerful graphics and you have the recipe for truly awesome multiplayer games.
•
Chat – Used in various chat applications.
•
Whiteboards – Java programs are not limited to sending ext and data across the network. A number of programmers have developed whiteboard software that allows users in diverse locations to draw on their computers.
•
Servers Java applications can listen for network connections and respond to them. This makes it possible to implement servers in Java. Both Sun and W3C have written web servers in Java designed to be as fully functional and fast as servers written in C.
•
Various protocols are available in Server for communication like HTTP, FTP, and TCP etc.
7
ELECTROIC COMMERCE (E-COMMERCE) 1. Shopping sites have proven to be one of the few real ways to make money from consumers on the web. 2. Although many sites accept credit cards through HTML forms, the mechanism is clunky (clubbing).
1.1.3 THE LAYERS OF A NETWORK Application Layer
Logical path
Transport Layer (TCP, UDP) Internet layer (IP) Physical path
Application Layer Transport Layer (TCP, UDP)
The Host to Host Layer
Internet layer (IP) Physical path(Ethernet, FDDI etc
THE HOST TO NETWORK LAYER 1. This layer is also referred as data link layer, or network-interface layer. 2. This layer defines how a particular network interface, such as an Ethernet card or PPP connection, sends IP data grams over its physical connection to the local network and the world. 3. The part of H to H layer made up of the hardware used to connect different computer (wires, fiber-optic cables, microwave relays, or smoke signals) is sometimes called the physical layer of the network.
THE INTERNET LAYER 1. The internet protocol is the most widely used network layer protocol in the world and the only network layer protocol java understands.
8
2. Data is sent across the internet layer in packets called data grams. Each IP datagram contains a header from 20 to 60 bytes along a payload that contains up to 65,515 bytes of data.
TRANSPORT LAYER Data grams packets are generated and transported only via this layer.
APPLICATION LAYER The layer that delivers data to the user is called the application layer. The 2 lower layers all work together to define how data is transferred from one computer to another.
1.1.4 IP ADDRESSES AND PORTS 1. It is very difficult to remember the IP address of each and every node. 2. It order to avoid this problem domain names are used. 3. Example google.com, rediff.com etc.
PORTS 1. Ports are used in receiving and sending data to another server or client. 2. Port number can take the value in the range of 0 to 65535. 3. Example for port numbers Protocol
Port
Protocol
Purpose
Echo
7
TCP/UDP
Discard
9
TCP/UDP
FTP SMTP HTTP POP3 NTP
21 25 80 110 119
RMI Registry
1099
Echo is a test protocol used to verify that 2 machines are able to connect by having one echo back the other’s input. Discard is a less useful test protocol in which all data received by the server is ignored. For File Transfer Protocol For Simple Mail Transfer Protocol For Hyper Text Transfer Protocol For Post Office Protocol Usenet news transfer is more formally known as the Network News Transfer Protocol This is the registry service for Java Remote Objects.
9
1.1.5 INTERNET ADDRESS CLASS 1. Internet addresses are assigned to different organizations by the Internet Assigned Numbers Authority (IANA). 2. ISP – Internet Service Providers gives block addresses. 3. Class C address block specifies the first 3 bytes of address, for example 199.1.32. This allows room for 254 individual addresses from 199.1.32.1 to 199.1.32.254 4. Class B address specifie only the first 2 bytes of the addresses. 5. There are also Class D and E addresses used for IP multicast group. 6. Each node on the Internet has a unique number and it is called as Internet Protocol Address or IP address. 7. The format of IP address is
A.B.C.D
Where A, B, C and D values will be in the range of 0 to 255 and each one is called as an octet.
Examples are as follows: 1. 195.244.178.145
Valid IP address
2. 155.233.254
Invalid IP address because the fourth octet is missed (should be the combination of four octets. Example 128.156.252.250)
3. 255.0.258.158
Invalid because the third octet exceeds 255
10
1.1.6 INTERNET SERVICE PROVIDER An ISP is an organization that provides individuals and other companies’ access to the Internet and other related services. They are three types of connections seen in homes/organizations. They are 1. ISDN – Integrated Services Digital Network 2. Cable Modem 3. A dial-up Telephone Modem
FIREWALLS The hardware and software that sits between the Internet and the local network, checking all the data that comes and goes out is called “firewalls”.
PROXY SERVERS Proxy servers are related to firewalls prevents hosts on a network from making direct connections to the outside world, a proxy server can act as a go-between. Thus a machine that is prevented from connecting to the external network by a firewall would make a request for a web page from the local proxy server instead of requesting the web page directly from the remote web server.
1.1.7 THE CLIENT/SERVER MODEL - MODEL 1 - CLIENT/SERVER Web Server
Request Internet Response Client
1. Model 1 is the simplest of client/server model in web architecture.
11
2. The client sends Request to the Web Server through Internet. 3. The Request is acknowledged by the Web Server and it sends Responses to the Client. 4. Now the Server sends the Requested information to the Client from Web Server. 5. At the end of data transfer, the Client disconnects from the Server.
MODEL 2 – WWW Database Client/Server Request Internet Response Client Web server
Database Server
1. In Model 2 the client sends Request to the Web Server through Internet. 2. The Request is acknowledged by the Web Server and it sends Responses to the Client. 3. If the information is not available, then the Server searches data in the Database Server also. 4. Now the Server sends the Requested information to the Client from Web Server or from Database Server. 5. At the end of data transfer, the Client disconnects from the Server.
MODEL 3 – WEB SERVER / APPLICATION SERVER Request
12
Internet Response
Client Web server
Application Server
1. In Model 3 the client sends Request to the Web Server through Internet. 2. The Request is acknowledged by the Web Server and it sends Responses to the Client. 3. If the information is not available, then the Server searches data in the Application Server also. 4. Now the Server sends the Requested information to the Client from Web Server or from Application Server. 5. At the end of data transfer, the Client disconnects from the Server.
MODEL 4 – WEB SERVER / APPLICATION SERVER/DATABASE SERVER Request
Web server
Internet
13
Application Server
Client Response
Database Server
1. In Model 4 the client sends Request to the Web Server through Internet. 2. The Request is acknowledged by the Web Server and it sends Responses to the Client. 3. If the information is not available, then the Server searches data in the Application Server and Database Server also. 4. Now the Server sends the Requested information to the Client from Web Server or from Application Server or Database Server. 5. At the end of data transfer, the Client disconnects from the Server.
MODEL 5: WEB SERVER / TRANSACTION SERVER/DATABASE SERVER
14
Request Internet Response
Client Web server
Transaction Server
Database Server
1. In Model 5 the client sends Request to the Web Server through Internet. 2. The Request is acknowledged by the Web Server and it sends Responses to the Client. 3. If the information is not available, then the Server searches data in the Transaction Server and Database Server also. 4. Now the Server sends the Requested information to the Client from Web Server or from Transaction Server or Database Server. 5. At the end of data transfer, the Client disconnects from the Server. 6. The Transaction ensures the ACID properties for several transactions with the Databases.
MODEL 6: WEB SERVER / TRANSACTION SERVER / DATABASE SERVER/APPLICATION SERVER Request Internet
15
Response Client Web server
Transaction Server
Application Server
Database Server
1. In Model 6 the client sends Request to the Web Server through Internet. 2. The Request is acknowledged by the Web Server and it sends Responses to the Client. 3. If the information is not available, then the Server searches data in the Web Server, Transaction Server, Application Server and Database Server also. 4. Now the Server sends the Requested information to the Client from Web Server or from Transaction Server or Database Server or Application Server. 5. At the end of data transfer, the Client disconnects from the Server.
1.1.8 W3C RECOMMENDATIONS (WWW 3 - World Wide Web Consortium) W3C LEVELS 1. Recommendation - is the highest level of W3C standard. However, the W3C is very careful not to actually call this a “standard” for fear of running afoul (afraid) of antivirus statutes.
16
2. Proposed Recommendation - A proposed recommendation is mostly complete and unlikely to undergo more than minor changes. The main purpose of a proposed recommendation is to work out bugs in the specification document rather than in the underlying technology documented. 3. Candidate Recommendation A candidate Recommendation indicates that the working group has reached consensus on all major issues and is ready for third-party comment and implementations.
1.2 TCP AND UDP PROTOCOLS 1.2.1 TCP/IP PROTOCOL ARCHITECTURE TCP/IP Protocol architecture contains the following layers. They are 1. 2. 3. 4. 5.
Application Layer Transport Layer Internet Layer Network Layer Physical Layer
COMPARISON OF OSI AND TCP/IP PROTOCOL ARCHITECTURE Application Layer
Application Layer
Presentation Layer Session Layer Transport (host – to – host) Layer Transport Layer Network Layer
Internet Layer
17
Data Link Layer
Network Access Layer
Physical Layer
Physical Layer
TCP/IP PROTOCOL SUITE •
Socket communication takes place via protocol. Protocol is a set of rules and regulations which should be obeyed by both server and client system.
•
Internet Protocol (IP) is a low level routing protocol that breaks data into small packets and sends them to an address across a network.
•
But IP does not provide guarantee to the delivered packets at the destination side.
•
Transmission Control Protocol (TCP) is a higher level protocol solves this problem and also provides guarantee to the delivered datagram packets.
•
A third protocol, User Datagram Protocol (UDP) is next to TCP and can be used directly to support fact, connectionless, unreliable protocol.
TCP PROTOCOL FORMAT Source Port
Destination Port Sequence Number Acknowledgment Number
DO
R
C W R
E C E
U R G
A C K
P S H
18
R S T
S F Y I N N
Destination Port
Checksum
Urgent Pointer Options + Padding
DO – Data Offset R - Reserved CWR - Congestion Window Reduced ECE - Explicit Congestion Notification URG - Urgent Pointer field significant ACK - Acknowledgement field significant PSH - Push function RST - Reset the connection SYN - Synchronize the sequence numbers FIN - No more data from sender
SL. NO
DIFFERENCE BETWEEN CONNECTION ORIENTED AND CONNECTION LESS PROTOCOLS Connection Oriented Connection less Protocol Protocol
1
Clients sends request to the Server Making Request to the server is not necessary
2
Client has to wait until its receives Not applicable Response from the Server After receiving Response from the No need to establish a connection Server, Client establishes connection with the Server.
3
4
5
6
After establishing a connection with the Server Clients starts transmitting the packets to the Server. At the end of data transfer client disconnect the connection with the Server.
Establishing a connection is not necessary. In connection less service the packets are transmitted continuously with out getting permission or without making connection with the server.
Guarantee is provided to datagram Fast but unreliable, because no guarantee is packets. provided to the data.
19
THE NETWORKING CLASSES AND INTERFACES The classes contained in the java.net packages are shown below: Authenticator
ServerSocket
CacheRequest
Socket
CacheResponse
SocketAddress
ContentHandler
SocketPermission
DatagramPacket
URI
DatagramSocket
URL
MulticastSocket
URLEncoder
APPLICATION LAYER •
Application layer contains the logic needed to support the various user applications.
•
File manager, emails etc.
•
Application layer provides access to OSI environment for users and also provides distributed information services.
TRANSPORT (HOST TO HOST) LAYER •
Provides reliable data transmission and maintain that all of the data arrived in the same order in which they were sent.
INTERNET LAYER •
Concerned with data access and routing data across a network for two end systems attached to the same network.
20
NETWORK ACCESS LAYER •
Takes care of exchange of data between an end system and the network to which it is attached.
PHYSICAL LAYER •
Covers the physical interface between data transmission device and a network. It specifies the characteristics of transmission medium, nature of signals, data rate, related informations.
1.3 URLs •
Uniform Resource Locator
•
URL identifies the location of a resource on the internet. It is used to specify the type of protocol used, name of the server, location of file on the server.
•
The syntax of URL is as follows.
SYNTAX
Protocol name://username@hostname:port/path/filename#fragment?Query
1.3.1 PROTOCOL SCHEME Protocol scheme specifies the name of the protocol used by the server. It includes file, ftp, https, gopher, news, telnet and other strings.
USER NAME It specifies the optional user name for the server.
PORT It specifies optional port numbers, default port number value is 80 for HTTP server.
PATH
21
Path points to a particular file in the directory specified.
FRAGMENT (ANCHOR / REFERENCE) It is used to identify a particular part in the document.
QUERY STRING It provides additional arguments for the server. It is commonly used only in http URLs.
RELATIVE URLs URLs that are not complete but inherit pieces of information from their parent are called relative URLs. •
In a relative URL, any pieces that are missing are assumed to be the same as corresponding pieces from the URL of the document in which the URL is found.
•
If any hyperlink
is to be clicked the browser cuts the example.html from the root URL and it attaches example.html to the URL and loads the document as http://www.cse.com/dept/example.html •
If the relative URL begin with a /. Then it is relative to the document root instead of relative to current file.
1.3.2 UNIFORM RESOUCE NAME (URN) URN is a name for a particular resource but without reference to a particular location. The use of URN is used to handle resources that are mirrored in many different locations. URN identify the resources itself, not the place where the resource lives.
SYNTAX OF URN Urn:name:resource-name
22
Name space - is the name of a collection of certain resources contained by some authority. Resource-name – is the name of resource within that collection. There are 2 types of URLs. 1. URL – Uniform Resource Locaters (is a pointer to a particular resource on the Internet at a particular location.) 2. URNs – Uniform Resource Name (is a name for a particular resource but without reference to a particular location)
1.3.3 URI (UNIFORM RESOURCE IDENTIFIER) It takes tow types 1. Uniform Resource Locators (URLs) 2. Uniform Resource Name (URNs)
FORMAT OF URI URI URL
URN
URN is a name for a particular resource but without reference to a particular location. The main use of URN is to handle resources that are mirrored in many different locations. They identify the resources itself, not the place where the resources live.
1.3.4 URL
23
•
Uniform Recourse Locator is a way to unambiguously identify the location of a resource on the Interned.
URI Uniform Resource Identifier is a string of characters in a particular syntax that identifies a resource. •
The resource identified may be a file on a server, but it may also be an email address, a news message, b book, a person’s name, an Internet host.
SYNTAX Scheme:scheme-specific-part 1. Tells the web browser a lot about the document 2. The protocol used to retrieve the document 3. The name of the host where the document lives and 4. The path to that document on the host.
RELATIVE URL URL that is not complete but inherits pieces from their parent is called relative URLs.
ABSOLUTE URL In contrast, a completely specified URL is called an absolute URL.
SYNTAX FOR URL Protocol://username@hostname:port/path/filename#fragment?query
Where,
24
•
Protocol is another word for what was called the scheme of the URL.
•
Scheme is the word used in the URI RFC.
•
Protocol part can be file, ftp, http, https, gopher, news, telnet, wais or various other strings (through not urn)
•
The hostname part of a URL is the name of the server that provides the resource you want, like www.oreilly.com.
•
It can also be the server’s IP address, like 128.55.47.56
•
The user-name is an optional user name for the server.
•
The port number is also optional.
•
Path points to a particular directory on the specified server. The path is relative to the document root of the server, not necessarily to the root of the file system on the server.
SCHEME TYPES 1. data – base 64 encoded data included directly in a link 2. file – A file on a local disk 3. FTP – An FTP server 4. gopher– a Gopher server 5. mailto – an email address 6. news – A Usenet newsgroup 7. Telnet – A connection to a Telnet based service (only used in Remote Login System)
25
8. urn – Uniform Resource Name
1.4 MIME HTTP is a standard protocol that defines how a web client talks to a server and how data is transferred from the server back to the client. HTTP relies heavily on two other standards. MIME (Multi purpose Internet Mail Extensions HTML 1. MIME is a way to encode different kinds of data, such as sound and text, to be transmitted over a 7-bit ASCII connection. . 2. It also lets the recipient know what kind of data has been sent, so that it can be displayed properly. 3. MIME was originally designed to facilitate multimedia email and to provide an encoding that could get binary data past the most train –damaged mail transfer programs. 4. It is an open standard for sending multipart, multimedia through internet email. 5. MIME supports almost a hundred predefined types of content.
6. The MIME contents are classified into two levels. They are • •
Type – It specifies what kind of data is contained (picture, text, movie) Subtype – It specifies the type of data (html, plain, gif, jpeg) etc.
1. MIME is an open standard for sending multipart, multimedia data through Internet email. 2. MIME was originally intended for email, it has become a widely used technique to describe a file’s contents so that client software can tell the difference between different kinds of data.
26
MIME SUB TYPES Type
Subtype
Description
Text
Calendar
Calendar control
Css
A CSS used for HTML and XML
Directory
Address book information such as name, phone, email address. Font and paragraph level formatting such as bold, email etc Simply raw ASCII text Html languages used by web browsers. This is an HTML like markup for encoding formatting into pure ASCII text. Sgml standard language
Enriched Plain Html Richtext Sgml
1.4.1 APPLICATIONS OF MIME 1. The data returned by HTTP is sent in MIME format only. 2. Java program relies on MIME format to find an appropriate content handler for a particular stream of data. 3. The original SMTP protocol was designed but MIME is designed for sending multimedia data base like audio, video content types.
1.4.2 MIME MESSAGE FORMAT Header Field
Data
•
MIME message consists of header field followed by data field.
•
Header field indicates 1. MIME version 2. The content type 3. Data transfer encoding scheme 4. Message identification 5. Message description
27
•
The message body contains the actual data being transferred which may consist of plain text, images, WWW electronic mail
MIME SUB TYPES 1.
MIME version field – indicate a version number to declare a message to be to be conformant (conflict with this specification and allows mail processing agents to distinguish between such ,messages and those generated by older or non-conformant software, which is presumed to lack such a field.
2.
A Content type header field, which is used to specify the type and subtype of data in the body of a message and fully specify the native representation of such data. 2.1 A TEXT content type is used to represent textual information in a number of character sets and formatted text description languages in a standardized manner. 2.2 A multipart content type value, which can be used to combine several body parts, possible of differing types of data, into a single message. 2.3 An application content type can be used to transmit application data or binary data, and hence, among other uses, to implement an electronic mail file transfer service. 2.5 A message content type is used to encapsulate another mail message. 2.5 An image content type is used for transmitting still image or picture data. 2.6 An audio content type is used for transmitting audio or voice data. 2.7 A video content type is used for transmitting video or moving image data with audio as part of the composite video data format. 3. A content transfer encoding header field, used to specify an auxiliary encoding that was applied to the data in order to allow it to pass through mail transport mechanisms which may have data or character set limitations. 4. Content-ID and Content-Description header fields can be used to describe the data in a message body.
1.4.3 MIME MEDIA TYPES There are 4 top lever media types they are:
28
1. Text – Textual information. The subtype “plain” in particular indicates plain text containing no formatting commands or directives of any sort. 2. Image – Image data. “Image” requires a display device such as a graphical display, a graphics printer, or a FAX machine) to view the information. 3. Audio – Audio data. “Audio” requires an audio output device such as speaker or a telephone to display the contents. 4. Video – Video requires the capability to display moving images, typically including specialized hardware and software. The two composite top level media types are 4.1 Multipart – data consisting of multiple entities of independent data types. 4.2 Message – An encapsulated message.
1.5 CGI – COMMON GATEWAY INTERFACE •
In general a programming language is something which has its own keywords, operators, compilers etc.
•
But CGI does not own any keywords, operators, compilers etc of its own.
•
Instead, CGI has got its own requirement of what a web enabled programming language should have .
•
So CGI is called as a specification. CGI is not a programming language, it is actually the specification defined by the W3C.
29
•
So CGI is an environment, which has necessary functions and interfaces for interacting with web servers .
•
CGI can be viewed as an entity which acts as the middleware between the web servers and external databases. Thus CGI applications perform specific information processing on the databases on behalf of web servers.
1.5.2 CGI ARCHITECTURE
Web Browser
Web Server
CGI Program
File
Database
•
CGI allows the user to give dynamic operations to its pages.
•
Traditionally, HTML program is static. CGI provides more interaction to their users.
•
The CGI programs are saved with an extension of .cgi
•
The CGI server can recognize any extension type, for example 1. 2. 3. 4. 5. 6.
.exe .cgi .asp .php3 .shtml .pl and so on.
For example named example.pl can be executed as follows.
http://www.server-name.com/cgi-bin/example.pl
30
•
In the above diagram the web browser sends a request to Web Server.
•
The Web server forwards this request to CGI program.
•
Now the CGI server searches the output from the file or database.
•
The CGI server can understand the above mentioned type of files.
1.5.3 CGI ENVIRONMENT VARIABLES CGI environment variables are entities which are known by both web server and CGI programs for communicating useful data between them.
CGI ENVIRONMENT VARIABLES SL. NO 1
Variable
Meaning
AUTH_TYPE
Contains the authentication method used to validate the web browser. This checks the user name and password.
2
CONTENT_LENGTH
Returns the length of the user provided content from the web page requesting the CGI script.
3
CONTENT_TYPE
Returns the type of the data. Example text/html or image/jpeg.
4
GATEWAY_INTERFACE
Returns the version of the CGI interface being used.
5
PATH_INFO
Holds the additional path information for the CGI script.
6
PATH_TRANSLATED
Holds the additional path information for the CGI script. This value is set from the information appended to the URL requesting the CGI script.
31
7
QUERY_STRING
Returns the string in the query.
8
REMOTE_ADDR
Stores the IP address pf the machine running the Web browser requesting the CGI script.
9
REMOTE_IDENT
Stores the user’s login name only if he web server supports identification.
10
REMOTE_USER
Stores the username the web browser specified for authentication. This is only set if the server supports authentication and the CGI script is protected.
11
SCRIPT_NAME
Stores the virtual path and name of the CGI script being executed. This is used for self-referencing URLs.
12
SERVER_NAME
Contains the name, either domain name or IP address, of the machine running the Web server.
13
SERVER_PORT
Contains the port number on which the web browser sent the request to the web server.
14
SERVER_PROTOCOL
Displays the name and version of the protocol being used to make the CGI script. In most cases, this will be the HTTP protocol and will look something like HTTP/1.0.
15
SERVER_SOFTWARE
Stores the name and version of the Web server software that executed the CGI script. Fox example Netscape Communication Server Version 1.1.
1.5.4 HTTP REQUEST HEADER ENVIRONMENT VARIABLES SL. NO
Variable
Meaning
1
HTTP_ACCEPT
Contains the list of media types separated by comma. Fox example audio/basic, image/gif, text/*.* and so on.
32
2
HTTP_ACCEPT_ENCODING
Contains the valid encoding methods the browser can receive in response from the web server. Examples are x-zip, x-stuffit, x-tar.
3
HTTP_ACCEPT_LANGUAGE
Contains the browser’s preferred language for a response from the Web Server. An example en_UK which si the English of the United Kingdom.
4
HTTP_AUTHORIZATION
Contains the authorization information from the web browser. Example is user name and password.
5
HTTP_CHARGE_TO
Formats for this field are still undermined.
6
HTTP_FROM
Contains the name of the requesting user us supplied by the web browser in an e-mail address format. For example [email protected]
7
HTTP_IF_MODIFIED_SINCE
Can contain a value specified in a valid ARPANET date standard, such as Weekday, DDMon-YY HH:MM:SS TIMEZONE.
8
HTTP_USER_AGENT
Contains the name of the web browser that requested the document. An example Mozilla/2.0, which would be the user agent for the Netscape 2.0 browser for Windows 95.
1.5.5 CGI – PARSED HEADERS •
Every CGI script must precede any data to the web server with a parsed header.
•
A parsed header is the lines of code output by CGI script that get parsed by the web server.
33
•
This parsed header is in the same format as an HTTP header and can contain any of the CGI variable names.
•
Parsed headers must always be immediately followed by a blank line.
•
Any lines in the parsed header that are not directives to the web server are sent back to the web browsers as part of the HTTP response header.
SERVER DIRECTIVES FOR PARSED HEADERS SL. NO 1
Variable
Meaning
CONTENT_TYPE
Specifies to the Web server the MIME type of the data being returned by the CGI script.
2
LOCATION
Contains either the virtual path or the URL of a document that CGI script wants retuned to the Web browser requesting your script.
3
Status
Returns to the web sever an HTTP status line, which will then be returned to the web browser. Status code and the reason string. Examples are 404 Not Found and 403 Forbidden (not allowed).
1.5.6 HTTP RESPONSE HEADERS SL. NO 1
Variable
Meaning
ALLOWED
Specifies to the requesting browser which request methods are allowed. Examples are GET, HEAD and PUT.
2
CONTENT_ENCODING
Specifies which encoding method Examples are x-zip, x-stuffit and x-tar.
34
is
used.
3
CONTENT_LANGUAGE
Specifies the language the returning document is in. An example is “en” which is English in one of its forms.
4
CONTENT_LENGTH
Specifies the size in bytes of the returning data.
5
CONTENT_TRANSFER_ENCO Specifies the encoding of the data between the web DING server and the web browser. The default is binary.
6
CONTENT_TYPE
Contains the type of the data being transferred. Examples are text/html and image/gif.
7
COST
Will contain the cost of the retrieval of the object being requested. The format of this header has not yet been specified.
8
DATE
Contains a creation date of the requested object in a valid ARPAET format.
9
DERIVED_FROM
10
EXPIRES
11
LAST_MODIFIED
12
LINK
13
MESSAGE_ID
14
PUBLIC
Can contain a version number for the requested object, allowing for version control of editable documents. Contains an expiration data for the requested information, after which the document should be retrieved again. This header is used primarily for caching mechanisms and is an ARPANET date format. Contains the date when the requested object was last modified. This header is in an ARPANET date format. Holds information about the document being returned. You can use it to specify information such as the inclusion of another URL within the returned document or the creator of the returned object. Contains a unique identifier for the HTTP message. Fairly similar to the ALLOW response header. However, it specifies the required methods that anyone can use, not just the requesting browser. Examples for this header are GET, HEAD, and TEXTSEARCH.
35
Contains the title of the document being returned. For an HTML file, this is equivalent to the value contained with in the <TITLE> .. tags.
15
TITLE
16
URI
Gives the URI where the requested object can be found. This will not always be the URL the user entered in the web browser requesting the returned object. However, it will point to an object that should be the same as the one being returned, with some degree of variance. An example HTTP://www.google.com , version which gives a URI with the same document, which might vary in language or version.
17
VERSION
Defines the version of an object that can be changed. Its format is currently undefined.
1.5.6 CGI – COMMON GATEWAY INTERFACE - EXAMPLE • • •
CGI is used to generate web pages dynamically. It is not a programming language. It is an environment which provides facility for executing programs and
SIMPLE CGI PROGRAM <TITLE> CGI EXAMPLE
1.6.1 SGML – STANDARD GENERALIZED MARKUP LANGUAGE •
HTML is an instance of SGML.
•
SGML was invented in the mid-1970s by Charles Goldfarb at IBM
•
SGML is now an International Standards Organization (ISO) standard, specifically ISO 8879:1986.
•
SGML allows the user to create various user defined tags easily without any rules.
1.6.2 XML – EXTENSIBLE MARKUP LANGUAGE •
Similar to SGML
•
Allows the user to create any number of user defined tags.
•
The value of an attribute may be enclosed in double of single quotes like this:
•
THIS IS CENTERED H1 HEADING
•
STYLES can be introduced for XML program like CSS using XLS file(XML Style Sheet program)
•
Here XML styles are saved with an extension of .xls (XML style sheet)
•
Using .xls files various styles can be given to the data which is inside the XML program.
1.6.3 XML DATA FILE FOR PRODUCT DETAILS
37
-Assumed as record name KEY BOARD KEY BOARD 1500
EXAMPLE 2 – XML DATA FILE FOR EMPLOYEE DETAILS <EMPLOYEE”> - Assumed as record name 1001 JACK 10000 3000 2000 500 1000 15500 14500 <EMPLOYEE”> - Assumed as record name 1002 JILL 20000 3000 2000 500 1000 25500 24500 <EMPLOYEE”> - Assumed as record name 1005 BLACK 5000 1000 500 500 1000 7000 6000
38
HTTP 1. Standard protocol for communication between web browsers and web servers. 2. HTTP specifies how a client and server establish a connection, how the client requests data from the server, how the server responds to that request, and finally how the connection is closed. 3. HTTP 1.0 is the currently accepted version of the protocol. It uses MIME to encode data. The basic protocol defines a sequence of 4 steps for each request from a client to the server. a) b) c) d)
Making the connection Making a request Receiving the response Closing the connection
1.6.4 HTTP 1.0 RESPONSE CODES 1. 2xx Successful – between 200 and 299 indicates that the request was received, understood, and accepted. 2. 200 OK - common response code for GET/POST. 3. 201 Created – The server has created a date file at a URL, specified in the body of the response. Now web browser attempt to load that URL. 4. 202 Accepted – request has been accepted 5. 204 No content – The server has successfully processed the request but has no information to send back to the client. 6. 3xx Redirection – Response codes from 300 to 399 indicate that the web browser needs to go to a different page.
Note HTTP 1.1 doubles the number of responses. However, a response code •
From 200 to 299 always indicates success;
39
• • •
A response code from 300 to 399 always indicates redirection; 400 to 499 indicate client error; 500 to 599 indicate a server error.
1.6.5 APPLETS AND SECURITY •
Applets are just more files that are transferred like any other.
•
Applets are small windows in which the data can be given and data can be displayed also.
WHERE DO APPLETS AND CLASSES COME FROM? •
When a browser sees an applet tag and decides to download and play the applet, it starts a long chain of events.
•
Let’s say your browser sees the following applet tag:
1. The web browser sets aside a rectangular area on the page 200 pixels wide and 300 pixels height. 2. The browser opens a connection to the server specified in the code base parameter, using port 80. 3. The browser requests the .class file from the web server as it requests any other file. If a code base is present, it is prefixed to the requested filename. Otherwise, the document base (the directory that contains the HTML page) is used. 4. The server responds by sending a MIME header followed by a blank line (\r\n) followed by the binary data in the .class file. 5. The web browser receives the data and stores it in a byte array. 6. The byte code verifier goes over the byte codes that have been received to make sure they don’t do anything forbidden, such as converting an integer in to pointer.
SECURITY: WHO CAN AN APPLET TALK TO AND WHAT CAN IT SAY?
40
There is much FUD (fear, uncertainty, and doubt) in the press about what Java applets can and cannot do. 1. Applets cannot access arbitrary addresses in memory. Applet produces output in the form of a Window. Applets are useful in writing programs using AWT (Abstract Window Toolkit) controls. 2. Applets cannot access the local file system in any way. They cannot read from or write to the local file system nor can they find out any information about files. 3. Applets cannot launch other programs on the client. In other words, they can not call Sysem.exec() or Runtime.exec(). 4. Applets cannot load native libraries or define native method calls. 5. Applets may not define any system properties. Finally, most important usage of applets is 1. An applet can only open network connections to the host from which the applet itself was downloaded. 2. An applet cannot listen on ports below 1024 (IE 5.0 does not allow applets to listen on any ports) 3. Even if an applet can listen on a port, it can accept incoming connections only from the host from which the applet itself was downloaded.
END OF UNIT - I
UNIT II – JAVA PROGRAMMING 2.1 Java Basics 2.2 I/O Streaming 2.3 Files 2.4 Looking up Internet Address 2.5 Socket Programming 2.6 Client/Server Programs 2.7 E-mail client 2.8 Web Page Retrieval 2.9 Protocol Handlers 2.10 Content Handlers
41
2.11 Applets 2.12 Image Handling 2.13 Remote Method Invocation
2.1 JAVA BASICS •
Java is related to C++, which is a direct descendant of C. Java derives its syntax from C. Java’s Object oriented features were influenced by C++.
•
Java is platform independent compiled language.
•
Java is byte code, Byte code is a highly optimized set of instructions designed to be executed by the Java run time system, which is called Java Virtual Machine (JVM).
•
A Java program is executed by the JVM solves the major problems associated with web based programs.
•
Since java programs are executed JVM, which can run on any computer once the runtime package exists for a given system.
CHARACTERISTICS OF JAVA PROGRAM 1. Simple 2. Secure 3. Portable 4. Object Oriented 5. Robust 6. Multithreaded 7. Architecture-neutral 8. Interpreted 9. High Performance 10. Distributed 11. Dynamic
SIMPLE: •
Java was designed to be easy for the professional programmer to learn and use effectively.
•
Object oriented programming language concepts are explained using Java very easily.
OBJECT ORIENTED
42
•
Java program describes the basic principles of object oriented programming.
ROBUST •
Java is robust because it clears 1. It clears Memory management mistakes 2. It clears Mishandled exceptional conditions 3. It eliminates these problems by managing memory allocation and deallocation. 4. It also provides garbage collection for unused objects.
MULTITHREADED 1. To create interactive, networked programs, Java threaded programming. 2. Using multi threading concept multiple process can be executed. 3. Java also maintains smooth synchronization during multi threading process.
ARCHITECTURE NEUTRAL 1. The central issue for the Java designers was code longevity and portability. 2. The main problem is that no guarantee exists that if you write program today, it will run tomorrow – even on the same machine. 3. Operating system upgrades, processor upgrades make a program malfunction. But Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation. 4. The main goal of Java program is “Write once, run anywhere, any time, forever”.
INTERPRETED AND HIGH PERFORMANCE 1. Java program is compiled in JVM which in turn creates Java byte Code. 2. The Java byte code is cross platform and can run in any machine.
DISTRIBUTED
43
1. Java is designed for the distributed environment of the Internet because it handles TCP/IP protocols. 2. Java also supports Remote Method Invocation (RMI). 3. RMI method enables a program to invoke methods across a network.
DYNAMIC 1. Java programs carry with them substantial amounts of run time information that is used to verify and resolve accesses to objects at run time. 2. This makes it possible to dynamically link code in a safe and expedient manner. 3. This is crucial to the robustness of the Java environment, in which small fragments of byte code may be dynamically updated on a running system.
2.2 I/O STREAMING 1. I/O in Java is built on streams. 2. Input streams are used to read data; Output streams are used to write data. 3. Different fundamental stream classes such as java.io.FileInputStream and sun.net.TelnetOutputStream can be used to read and write particular sources of data. 4. Filter streams can be chained to an input stream or an output stream. 5. Filters can modify the data as it is read or written – for instance, by encrypting or compressing it. 6. Java.io.DataOutputStream class provides a method that converts an integer to four bytes and writes those bytes onto its underlying output stream. 7. Finally, readers and writers can be chained to input and output streams to allow programs to read and write text (that is characters) rather than bytes.
OUTPUT STREAMS Java’s basic output class is java.io.OutputStream. It takes the following methods:
44
public abstract class OutputStream This class provides the fundamental methods needed to write data. The methods in OutputStream classes are: 1. public abstract void write(int b) throws IOException This method is used to write integer values. 2. public void write(byte[] data) throws IOException This method is used to write data in the form of bytes. 3. public void write(byte[] data, int offset, int length ) throws IOException This method is used to write data in bytes. Here while writing the data the Offset value can also be specified. If the Offset values mentioned in write method, the write operation will be performed starting from the Offset value. 4. public void flush() throws IOException Flush method rescues the user form the deadlock problem by forcing the buffered stream to send its data even if the buffer is not yet full. 5. public void close() throws IOException This method is used to close the Output stream class.
THE FLOW OF BUFFERED STREAM & FLUSH COMMAND Flush Method Message is sent from server to client
This message is not enough, so it is waiting to get some more messages
45
The whole message is sent to the client
WEB SERVER
Buffered Output Stream
WEB CLIENT
EXPLANATION •
The message is sent from the Server to the Client.
•
If the message in buffer is not enough to send, then it automatically calls Flush method. The Flush method forces the buffer stream to send the data to Client even if the buffer is not yet full.
INPUT STREAMS Java’s basic output class is java.io.InputStream This class provides the fundamental methods needed to read data. The methods in InputStream classes are as follows: 1. public abstract int read() throws IOException This method is used to read the data from the buffer. 2. public int read(byte[] input) throws IOException The method is used to read byte of information. Here byte refers an array of valaues. 3. public int read(byte[] input, int offset, int length ) throws IOException This method is used to read byte of information. Offset values and length can also be included.
46
4. public long skip(long ) throws IOException The method is used to skip value or values during read operation. 5. public int available() throws IOException This method is used for checking whether the data is available or not for read operation. 6. public void close() throws IOException This method is used to close the input stream class.
EXAMPLE PROGRAM FOR READING 10 BYTES FORM INPUTSTREAM byte[] input = new byte[10]; for (int I = 0; I < input.length; i++) { int b = in.read(); if (b = = -1) break; input[i] = (byte) b; } •
Here read method is used to read a character at a time.
•
The value of b becomes -1 if it reaches to the end of file.
•
If the value of b is -1 then the read operation is suspended, because it reaches to the end of file
MARKING AND RESETTING The Input Stream class also has three less commonly used methods that allow programs to back up and reread data they’ve already read. These are: 1. public void mark(int readAheadLimit)
47
2. public void reset() throws IOException 3. public Boolean markSupported()
MARKING 1. Mark the current position in the stream with the mark() method. 2. At a later point, you can reset the stream back to the marked position using the reset() method. 3. Subsequent reads then return data starting from the marked position. 4. If you try to reset back too far, then IOException will be thrown. 5. There can be only one mark in a stream at any given time. Marking a second location erases the first mark. 6. Marking and resetting are usually implemented by storing every byte read from the marked position in an internal buffer.
FILTER STREAMS 1. Input/Output stream are fairly raw classes. They allow you to read and write bytes, either singly or in groups, but deciding what those bytes mean – whether they are integers or IEEE 754 floating numbers or Unicode text is completely up to the programmer and the code. 2. Many files transferred by FTP are stored in the zip format. Java provides a number of filter classes you can attach to raw streams to translate the raw bytes to and from these and other formats.
THE FLOW OF DATA THROUGH A CHAIN FILTERS APPLICATION
InputStreamReader
48
GZIPInputStream
CipherInputStream
BufferedInputStream
TelnetInputStream
Network
BUFFERED STREAMS •
The BufferedOutputStream class stores written data in a buffer until the buffer is full or the stream is flushed.
•
Then it writes the data onto the underlying output stream at the time.
•
BufferedInputStream class works Similar to BufferedOutputStream class for read operation.
49
CONSTRUCTORS FOR BUFFERED IN/OUT STREAM CLASSES 1. public BufferedInputStream(InputStream in) 2. public BufferedInputStream(InputStream in, int bufferSize) 3. public BufferedInputStream(InputStream out) 4. public BufferedInputStream(InputStream out, int bufferSize)
PRINTSTREAM CLASS The printStream class is the first filter output stream most programmers encounter’ because System.out is a PrintStream. However, other output streams can also be chained to print streams, using these two constructors: 1. public printStream(OutputStream out) 2. public printStream(OutputStream out, Boolean autoflush)
METHODS OF PRINTSTREAM CLASS It has 9 overloaded print() methods and 10 overloaded println() methods. They are 1. public void print(boolean b) 2. public void print(char b) 3. public void print(int b) 4. public void print(long b) 5. public void print(float b) 6. public void print(double b) 7. public void print(char[] text) 8. public void print(String s) 9. public void print(Object o)
10 overloaded println() methods. 1. public void println(boolean b) 2. public void println(char b) 3. public void println(int b)
50
4. public void println(long b) 5. public void println(float b) 6. public void println(double b) 7. public void println(char[] text) 8. public void println(String s) 9. public void println(Object o) 10. println()
PUSHBACK INPUT STREAM •
It is a subclass of FilterInputStream that provides a pushback stack so that a program can “unread” bytes onto the input stream.
•
The read() and available() methods are invoked exactly as with normal input streams.
•
public void unread(int b) throws IOException – pushes an unsigned byte given as an int between o and 255 onto the stream.
•
More unread() methods that push a specified array or sub array onto the stream. public void unread[byte[] input) throws IOException public void unread[byte[] input, int offset, int length) throws IOException
DATA STREAMS •
The DataInputStream and DataOutputStream classes provide methods for reading and writing java’s primitive data types and strings in a binary format.
•
It has 11 methods for writing a particular java data types. They are 1. public final void writeBoolean(boolean b) throws IOException 2. public final void writeByte(int b) throws IOException
51
3. public final void writeShort(int s) throws IOException 4. public final void writeChar(int c) throws IOException 5. public final void writeInt(int i) throws IOException 6. public final void writeLong(long l) throws IOException 7. public final void writeFloat(float f) throws IOException 8. public final void writeDouble(double d) throws IOException 9. public final void writeChars(Strong s) throws IOException 10. public final void writeBytes(String s) throws IOException 11. public final void writeUTF(Sring s) throws IOException
COMPRESSING STREAMS Java .util.zip package contsins filter streams that compress and decompress streams in zip, gzip, and deflate(shrink or reduce) formats.
There are 6 stream classes that perform compression and decompression. 1. public class DeflaterOutputStream extends FilterOutputStream 2. public class DeflaterInputStream extends FilterInputStream 3. public class QDeflaterOutputStream extends FilterOutputStream 4. public class DeflaterOutputStream extends FilterOutputStream 5. public class DeflaterOutputStream extends FilterOutputStream 6. public class DeflaterOutputStream extends FilterOutputStream
DIGEST (assimilate, absorb, grasp, observe) STREAMS •
The java.util.security package contains two filter streams that can calculate a message digest for a stream.
•
They are DigestInputStream and Digest-OutputStream.
•
A message digest, represented in Java by the java.util.security.MessageDigest class, is a strong hash code for the Stream; it is a large integer (typically 20 bytes long in binary format) that
52
can easily be calculated from a stream of any length in such a fashion that no information about the stream is available from the message digest. •
A message Digest object that uses a particular algorithm, such as the Secure Hash Algorithm (SHA).
ENCRYPTING STREAMS •
CipherInputStream and CipherOutputStream are part of a standard extension to java called the Java Cryptography Extension.
•
public CipherInputStream(InputStream in, Cipher c)
•
public CipherOutStream(InputStream in, Cipher c) – used for encryption and decryption respectively.
READERS AND WRITERS •
Java’s native character set is the 2-byte Unicode character set. Consequently, Java provides an almost complete mirror of the input and output stream class hierarchy that’s designed for working with characters instead of bytes.
•
Two abstract super classes define the basic API for reading and writing characters. The java.io.Reader class specifies the API by which characters are
read. The java.io.Writer class specifies the API by which characters are written. •
Wherever input and output streams use bytes, readers and writers use Unicode Characters.
•
The most important sub classes are InputStreamReader and the OutputStreamWriter classes.
•
In addition java.io.package also includes several raw reader and writer classes that read characters without directly requiring an underlying input stream. They are
53
1. FileReader 2. FileWriter 3. StringReader 4. StringWriter 5. ChararrayReader 6. CharArrayWriter
WRITERS The Writer class mirrors the java.io.OutputStream.class. It is abstract class and has two protected constructors. Writer class is never used directly. The various methods of Writer classes are as follows: 1. protected Writer() 2. protected Writer(Object lock) 3. public abstract void write(char[] text, int offset, int length) throws IOException 4. public void write(int c) throws IOException 5. public void write(char[] text) throws IOException 6. public void write(String s, int offset, int length) throws IOException 7. public abstract void flush() throws IOException 8. public abstract void close() throws IOException
READERS •
The Reader class mirrors the java.io.InputStream class.
•
All the classes of writer are applicable to readers also, change read instead of write operation.
LINENUMBERREADER
54
•
The LineNumberReader class replaces the deprecated LineNumberInputStream class from Java 1.0. It’s a subclass of BufferedReader that keeps track of the current line number being read.
•
Line number can be retrieved at any time with the getLineNumber() method. public LineNumberReader(Reader in) public LineNumberReader(Reader in, int bufferSize)
Similarly we have 1. PushbackReader 2. PrintWriter classes.
THREADS •
A thread with a little t is a separate independent path of execution in the virtual machine.
•
A Thread with a capital T is an instance of the java.lang.Thread class.
•
There is a one-to one relationship between threads executing in the virtual machine and Thread objects also constructed by the virtual machine.
•
To start a new thread running in the virtual machine, construct an instance of Thread class and invoke its start() method as follows: 1. For creating a Thread Thread t = new Thread(); t.start() 2. To run a Thread Public void run()
RACE ALGORITHM Java Virtual Machine uses different algorithms to allot time to different threads. This is called a race condition.
POLLING
55
The solution mostly adopt is to have the getter method return a flag value (or perhaps throw an exception) until the result field is set. Then the main thread periodically polls the getter method to see whether it’s returning something other than the flag value.
CALLBACKs There are more efficient ways to handle the problem. The infinite loop that repeatedly polls each ReturnDigest object to see whether it is finished can be eliminated.
SYNCHRONIZATION Synchronization is the process of organizing threads in a proper way. That is if one thread is activated another thread will be put into sleep mode. In the next cycle the thread which is in sleep mode is activated now and the other thread is put in to sleep mode. Synchronization process is very much essential for handling Threads properly.
SYNCHRONIZED BLOCKS Synchronization must be considered any time multiple threads share same resources. These threads may be instances of the same Thread subclass or use the same Run able class, or they may be instances of completely different classes.
ALTERNATIVES TO SYNCHRONIZATION Synchronization is not always the best possible solution to the problem of inconsistent behavior as a result of thread scheduling. There are a number of techniques you can use to avoid the need for synchronization.
1.1 Use local variables instead of fields wherever possible. Local variables do not have synchronization problems. 1.2 String arguments are safe because they are immutable that is, once a string object has been created, it cannot be changed by any thread. An immutable object never changes its state. 1.3 A third technique is to use a thread unsafe class but only as a private field of a class that is thread-safe. As long as the containing class accesses the unsafe class only in a
56
thread –safe fashion, and as long as it never lets a reference to the private field leak out into another object, the class is safe. 1.4 Each separate thread its own separate log so that no resources were shared between the individual threads. DEADLOCK Deadlock occurs when two threads each need exclusive access to the same set of resources, but each thread possesses a different subset of those resources. THREAD SCHEDULING 1. The order in which the threads are executed is called thread scheduling. 2. It is used to schedule the thread one by one without overlapping. 3. If the scheduling is not proper it may lead to errors.
PRIORITIES 1. Each thread has a priority that’s specified as an integer from 1 to 10. When multiple threads are able to run, generally the VM will run only the highest-priority thread, though that’s not a hard-and-fast rule. 2. public static final int MIN_PRIORITY = 1; public static final int MIN_PRIORITY = 5; public static final int MIN_PRIORITY = 10; The priority which has 10, 5 and 1 will get executed in order. Thread scheduling is divided into Preemptive -> It is Possible to alter the sequence in the middle NonPreemptive -> It is not possible to alter the sequence.
THERE ARE 10 WAYS A THREAD CAN PAUSE IN FAVOR OF OTHER THREADS OR INDICATE THAT IT IS READY TO PAUSE. They are as follows:
57
1. It can block on I/O 2. It can block on a synchronized object 3. It can yield 4. It can go to sleep 5. It can join another thread 6. It can wait on an object 7. It can finish 8. It can be preempted by a higher-priority thread. 9. It can be suspended 10. It can be stopped
BLOCKING •
Blocking occurs at any time a thread has to stop and wait for a resource it doesn’t have.
•
The most common way a thread in a network program will voluntarily give up control of the CPU is by blocking on I/O.
•
Threads can also block when they enter a synchronized method or block.
YIELDING •
The second way for a thread to give up control is to explicit yield.
•
A thread does this by invoking the static Thread.yield() method public static void yield()
SLEEPING •
Sleeping is more powerful form of yielding. Whereas yielding indicates only that a thread is willing to pause and let other equal-priority threads have a turn, a thread that goes to sleep will pause whether any other thread is ready to run or not.
58
•
Sometimes sleeping is useful even if you don’t need to yield to other threads. Putting a thread to sleep for a specified period of time lets you write code that executes one every second, every minute, every ten minutes, and so forth. Thread.sleep() – used to put the thread in sleep mode.
JOINING THREADS – used to join two threads. Public final void join(long milliseseconds) throws InterrupteException Public final void join(long milliseseconds, int nanoseconds) throws InterrupteException
WAITING ON AN OBJECT •
A thread can wait on an object it has locked.
•
While waiting, it releases the lock on the object and pauses until it is notified by some other thread.
•
Another thread changes the object in some way, notifies the thread waiting on that object, and then continues.
•
It remains asleep until one of three thing happens 1. The timeout expires 2. The thread is interrupted 3. The object is notified.
THREAD POOLS 1. Adding multiple threads to a program dynamically improves performance, especially for I/O – bound programs such as most network programs. 2. Even if the threads finish quickly, this can overload the garbage collector or other parts of the VM, and hurt performance.
2.3 FILES •
Java provides a number of classes and methods that allow the user to read and write files.
59
•
In java, all files are byte-oriented, and Java provides methods to read and write bytes from and to a file.
•
The common file stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files. The following methods are used in files. FileInputStream(String filename) throws FilenotFoundException FileOutputStream(String filename) throws FilenotFoundException
Here, •
filename specifies the name of the file that you want to open.
•
When you create an input stream, if the file does not exist, then FileNotFoundException is thrown.
•
When the file operations are over then close it by calling close(). It is defined by both FileInputStream and FileOutputStream classes. The close method takes the following form. void close() throws IOException read() returns -1 when the end of the file is encountered.
2.4 LOOKING UP INTERNET ADDRESSES DNS, IP ADDRESSES
60
1. Devices connected to the Internet are called nodes. 2. Nodes that are computers are called hosts. 3. Each node or host is identified by at least one unique 32-bit number called an Internet address. 4. An IP address is normally written as four bytes of memory, each ranging from 0 to 255. 5. For example 152.255.6.9 is called the dotted quad format. 6. To avoid the complexity of IP address problem, the designers of the Internet invented the domain Name System (DNS).
IPv6 AND 128-bit Addresses •
The current IP address standard uses 32 bits, which is enough to address more than four billion computers, almost one for every person on earth.
•
A new standard called IPv6 will begin using 16-byte, 128-bit addresses.
•
This expands the available address space to 2128 or 1.6043703E32 different addresses.
THE INETADDRESS CLASS •
The java.net.InetAddress class is Java’s encapsulation of an IP address.
•
It is used by most of the other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more.
CREATING NEW INETADDRESS OBJECTS 1. public static InetAddress InetAddress.getByname(String hostname) throws UnknownHostException 2. public static InetAddress[] InetAddress.getAllByname(String hostname) throws UnknownHostException 3. public static InetAddress InetAddress.getLocalHost() throws UnknownHostException
61
Byte 0 A B C D E
0 1 1 1 1
N/W 0 1 0 1 1 1 1
Byte 1
Byte 2 HOST
Byte 3
N/W
N/W HOST 0 Multi cast group 1 0 Reserved for feature use Method 1 – Used most frequently. This is a static method that takes the hostname you’re looking for as its argument. It looks up the host’s IP address using DNS.Call getByname() method. Example – InetAddress address = InetAddress.getByname(www.oreilly.com);
Method 2 – Some computers have more than one Internet address. Given a hostname, it returns an array that contains all the addresses corresponding to that name.
Method 3 – Contains one final means of getting an InetAddress object. It returns the IP address on which it’s running.
THE COMMON LOG FILE FORMAT Web server logs track the hosts that access a web site. By default, the log reports the IP addresses of the sites that connect to the server. However, you can often get more information from the names of those sites than from their IP address.
Remotehost rfc931 authuser [date] “request” status bytes 1. remotehost – is either the hostname or IP address from which the browser connected. 2. rfc931 – username of the user on the remote system, as specified by Internet protocol RFC 931. 3. authuser – is the authentication username as specified by RFC931. 4. [date] – the date and time of the request are given in brackets. 5. “request” – request from the client 6. status – This is a numeric HTTP status code returned to the client.
62
7. bytes – This is the number of bytes of data that was sent to the client as a result of this request.
RETRIEVING DATA WITH URLs 1. The simplest way for a Java program to locate and retrieve data from the network is to use the URL class. 2. User do not need to worry about the details of the protocol being used, the format of the data being retrieved, or how to communicate with the server; you simply tell Java the URL, and it gets the data for you.
THE URL CLASS •
The java.net.URL class is an abstraction of a Uniform Resource Locator like ftp://ftp.redhat.com/pub.
•
It extends java.lang.object, and it is final class that cannot be subclassed.
•
The URL class itself forms the context through which the different strategies are selected.
•
public final class URL extends object implements Serializable
CREATING NEW URLs The simplest URL constructor just takes an absolute URL in string form as its single argument Public URL(String url) throws MalformedURLException Try { URL u = new URL(http://www.macfaq.com/personal.html); } catch (MalformedURLException e) { System.err.println(e) }
METHODS OF URL URL provides the following five public methods. They are as follows:
63
1. 2. 3. 4. 5. 6.
getFile() getHost() getPort() getProtocol() getRef() getAuthority()
1. getProtocol() – method returns a String containing the scheme of the URL. For example http,https or file. 2. getHost() – method returns a String containing the hostname of the URL. 3. getPort() – method returns the port number specified in the URL as an int. If no port was specified in the URL, then getport() returns -1 to signify that the URL does not specify the port explicitly, and will use the default port for the protocol. 4. getFile() – method returns a String that contains the path and file portion of a URL; remember that java does not break a URL into separate path and file parts. 5. getRef() – method returns the named anchor part of the URL. If the URL does not have a named anchor, the method returns null. 6. getQuery() – method returns the query of the URL, 7. getAuthority() – Between the scheme and the path of a URL, you will find the authority. The term authority is taken from the URI specification (RFC 2396), where this part of the URI indicates the authority that’s resolvind the resource.
RETRIEVING DATA FROM A URL •
Data can be retrieved using URL class.
•
The URL class has three methods (four in java 1.3) to retrieve data from a URL. They are 1. public final InputStream openStream() throws IOException 2. public URLConnection openConnection() throws IOException 3. public final Object getContent() throws IOException 4. public final Object getContent[class[] classes) throws IOException
64
OpenStream() – method connects to the resource referenced by the URL, performs any necessary handshaking between the client and the server, and then returns an InputStream from which data can be read.
Openconnection() – method opens a socket to the specified URL and returns a URLConnection object. An URLConnection represents an open connection to a network resource. If the call fails, openConnection() throws an IOException.
getContent() – method is the third and final way to download data referenced by a URL. The getContent() method retrieves the data referenced by the URL and tries to make it into some type of object.
Object getContent[class[] classes – Starting with JDK1.3, it is possible for a content handler to provide different views of an object. This overloaded variant of the getContent() method lets you choose what class you’d like the content returned as.
UTILITY METHODS 1. The URL class contains a couple of utility methods that perform common operations on URLs. 2. The samefile() method tests whether two URL objects point to the same file. If they do, the sameFile() returns true, otherwise, it returns false. 3. equals(Object o) – An object is equal to a URL only if it is also a URL, both URLs point to the same file as determined by the sameFile() method, and both URLs have the same ref(or both URLs have null refs). 4. hashCode() – method returns an int that is used when URL objects are used as keys in hash tables. Thus, it is called by the various methods of java.util.Hashtable.
THE URLEncoder and URLDecoder Classes •
One of the problems that the designers of the web faced was differences between local operating systems.
•
To solve these problems, characters used in URLs must come from a fixed subset of ASCII, in particular:
65
1. 2. 3. 4.
The capital letters A-Z The lowercase letters a-z The digits 0-9 The punctuation characters -,!, @, #, $, * ‘ (and ,)
URLEncoder •
The java.net.URLEncoder class contains a single static method called encode() that encodes a String according to these rules.
•
The java.net.URLDecoder class contains a single static method called encode() that decodes a String according to these rules:
•
Public static String encode(String s) and static String decode(String s) methods can be used to encoding and decoding strings respectively.
COMMUNICATING WITH CGIs AND Servlets through GET METHOD 1. The URL class makes it easy for Java applets and applications to communicate with server-side CGI programs and servlets that use the GET method. 2. CGI programs and servlets that use the POST method require the URLConnection class. 3. Study the coding for creating textbox, checkbox, label, radio button and checkbox elements.
ACCESSING PASSWORD-PROTECTED SITES •
Java.net package includes an Authenticator class that can be used to provide a username and password for sites that provide themselves using HTTP authentication.
•
Since Authenticator is an abstract class, you must subclass it.
•
To make the URL class use your subclass, you install it as the default authenticator by passing it to the static Authenticator.setDefault() method.
•
Authenticator.setDefault(new DialogAuthenticator())
2.5 SOCKET PROGRAMMING
66
SOCKETS FOR CLIENTS DATAGRAMS •
Data is transmitted across the Internet in packets of finite size called datagrams.
•
Each datagram contains a header and payload.
•
The header contains the address and port to which the packet is going, the address and port from which the packet came, and various other house keeping information used to ensure reliable transmission.
•
Sockets are an innovation of Berkely Unix that allow the programmer to treat a network connection as just another stream onto which bytes can be written and from which bytes can be read.
•
Sockets shield the programmer from low-level details of the network, such as media types, packet sizes, packet transmission, network address, and more.
SOCKET BASICS •
A socket is a connection between two hosts. It can perform 7 basic operations: 1. 2. 3. 4. 5. 6. 7.
Connect to a remote machine Send data Receive data Close a connection Bind a port Listen for incoming data Accept connections from remote machine on the bound port
Java programs normally use client sockets in the following fashion 1. The program creates a new socket with a Socket() constructor. 2. The socket attempts to connect to the remote host. 3. Once connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other.
67
4. This connection is full-duplex; both hosts can send and receive data simultaneously. 5. There will normally be some agreed-upon hand-shaking followed by the transmission of data from one to the other. 6. When the transmission of data is complete, one or both sides close the connection. Some protocols, such as HTTP 1.0, require the connection to be closed after each is serviced. 7. FTP, allows multiple requests to be processed in a single connection.
THE SOCKET CLASS 1. The java.net.Socket class is Java’s fundamental class for performing client-side TCP operations. 2. Each lets you specify the host and the port you want to connect to. 3. Hosts may be specified as an InetAddress or a String. 4. Ports are always specified as integer values from 0 to 65,535.
CONSTRUCTERS USED FOR CREATING SOCKETS 1. public Socket(String host, int port) throws UnknownHostException, IOException Creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host. 2. public Socket(InetAddress host, int port) throws IOException This constructor creates a TCP socket to the specified port on the specified host and tries to connect. It differs by using an InetAddress object. 3. public Socket(String host, int port, InetAddress interface, int localPort) throws IOException This constructor creates a socket to the specified port on the specified host and tries to connect. It connects to the host and port specified in the first two arguments. 4. public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException
68
Identical to the previous one except that the host to connect to is passed as an InetAddress, not a String. It creates a TCP socket to the specified port on the specified host from the specified interface and local port, and tries to connect.
GETTING INFORMATION FROM SOCKET 1. public InetAddress getInetAddress() – Tells which remote host the Socket is connected, if the connection is closed or not. 2. public int getPort – Tells the port number in which the socket is connected. 3. public int getLoalPort() - There are two ends to a connection: the remote host and the local host. It is used for getting the local port number. 4. public InetAddress getLocalAddress() – returns the address of the local host.
CLOSING THE SOCKET Public synchronized void close() throws IOException – is used to close the socket.
SETTING SOCKET OPTIONS Socket options specify how the native sockets on which the Java Socket class relies to send and receive data. The following options can be used to set sockets. •
TCP_NODELAY – ensures that packets are sent as quickly as possible regardless of their size.
•
SO_BINDADDR
•
SO_TIMEOUT – ensure that the call will not block for more than a fixed number of milliseconds.
•
SO_LINGER – option specifies what to do with datagrams that have not yet been sent when a socket is closed.
•
SO_SNDBUF – used to get and set the suggested send buffer used for network output.
69
•
SO_RCVBUF – Most TCP stacks use buffers to improve network performance, say 10 Mbps and up.
•
SO_KEEPALIVE – is turned on, then the client will occasionally send a data packet over an idle connection, off terminates the data transfer.
FINGER •
Finger is a straightforward protocol described in RFC 1288.
•
The client makes a TCP connection to the server on port 79 and sends a one line query.
•
The server responds to the query and closes the connection.
•
The format of the query precisely, defied, the format of the response somewhat less so.
•
The data transferred is pure printable and ASCII text.
WHOIS •
Whois is a simple directory service protocol defined in RFC 954.
•
It was originally designed to keep track of administrators responsible for Internet hosts and domains.
•
A whois client connects to one of several central servers and requests directory information for a person or persons; it can usually give you a phone number, and email address, and a U.S. mail address (not necessarily current ones though).
THE STRUCTURE OF WHOIS PROTOCOL The client opens a TCP socket to port 43 on the server whois.internic.net. When you are using whois, you almost always connect to this server; there are a few other servers, but these are relatively rare. But there is a separate whois server for the U.S. department of Defencse. 1. The client sends a search string terminated by a carriage return/linefeed pair (/r/n). You can also search for domain names, like oreilly.com or netscape.com, which give you information about a network.
70
2. The server sends an unspecified amount of human-readable information in response to the command and closes the connection. 3. The client displays this information to the user. THE FOLLOWING TABLE SHOWS PREFIX SHOULD BE PLACED BEFORE SEARCH STRIND ON THE COMMAND LINE. Prefix Domain Gateway Group Host Network Organization ASN Full or = Person SUBdisplay %
Meaning Find only domain records Find only gateway records Find only group records Find only host records Find only network records Find only organization records Find only Autonomous System Number records Show complete record for each match Find only person records Show the users of the specified host, the hosts on the specified network etc.
SOCKETS FOR SERVERS The ServerSocket() class contains everything you need to write servers in Java. It has constructors that create new ServerSocket objects, methods that listen for connection on a specified port, and methods that return a Socket object when a connection is made so that you can send and receive data.
THE SERVERSOCKET CLASS The basic life cycle of a server is 1. A new ServerSocket created on a particular port using a ServerSocket() constructor. 2. The ServerSocket listens for incoming connection attempts on that port using its accept() method. Accept() blocks until a client attempts to make a connection, at which point accept() returns a socket object connecting the client and server.
71
3. Depending on the type of server, either the socket’s getInputStream() method, getOutputStream() method, or both are called to get input and output streams that communicate with the client. 4. The server and the client interact according to an agreed-upon protocol until it is time to close the connection. 5. The server, the client, or both close the connection. 6. The server returns to step 2 and waits for the next connection.
THE CONSTRUCTORS FOR SERVERSOCKET There are 3 public ServerSocket constructors 1. public ServerSocket(int port) throws IOException 2. public ServerSocket(int port, int quuelength) throws IOException, BindException 3. public ServerSocket(int port, int quuelength, InetAddress bindaddress) throws IOException Method 1 This constructor creates a server socket on the port specified by the argument. If you pass 0 for the port number, the system selects an available port for you. A port chosen for you by the system is called an anonymous port since you don’t know it number. Method 2 This constructor creates a ServerSocket on the specified port with a queue length of you choosing. Method 3 Along with method 2 it also binds the specified IP address.
HTTP SERVERS 1. HTTP is a large protocol. 2. A full featured HTTP server must respond to requests for files, convert URLs into file-names on the local system.
72
3. It also respond to POST and GET requests, handle requests for files that don’t exist, interpret MIME types, launch CGI programs. 4. Our investigation of HTTP servers begins with a server that always sends out the same file, no matter who or what the request.
A FULL-FLEDGED HTTP SERVER 1. HTTP server can serve an entire document tree, including images, applets, HTML files, and more. 2. This server is called full fledged server.
SECURE SOCKETS 1. Confidential communication through an open channel such as the public Internet that nonetheless resists eavesdropping absolutely requires that the data be encrypted. 2. In traditional secret key (or symmetric) encryption, the same key is used both to encrypt and decrypt the data. 3. In public key (or asymmetric) encryption, different keys are used to encrypt and decrypt the data. One key, called the public key, is used to encrypt the data. This key can be given to anyone.
METHODS OF THE SSL Socket Class (Secure Socket Layer socket class) The SSLSocket class has a number of methods for configuring exactly how much and what kind of authentication and encryption is performed.
EVENT HANDLERS •
Network communications are slow compared to the speed of most computers. Authenticated network communications are even slower
•
JSSE(Java Secure Socket Extension) uses the standard event model to notify programs when the handshaking between client and server is completes.
•
It can be implemented using HandshakeCompletedListener interface.
•
Public interface HandshakecompledListener extends java.util.EventListner
73
SESSION MANAGEMENT •
SSL is most commonly used on web servers.
•
Web connections tend to be transitory(fleeting or temporary)
•
Using JSSE, you don’t need to do anything extra to take advantage of sessions.
•
Using session various informations can be retrieved using the following methods.
1. public byte[] getId() 2. public SSLSessionContext getSessionContext() 3. public long getCreateation() 4. public long getLastAccessdTime() 5. public void invalidate() 6. public void removeValue(String name)
UDP DATAGRAMS AND SOCKETS UDP PROTOCOL • UDP – User Datagram protocol is an alternative protocol for sending data over IP that is very quick, but not reliable. •
That is, when you send UDP data, you have no way of knowing whether it arrived, much less whether different pieces of data arrived in the order in which you sent them.
THE STRUCTURE OF UDP DATAGRAM 0 Version
4
8 Header Length Identification
12 16 20 24 28 Type of service Datagram length Flags
Fragment offset Header checksum
Time-to-live(TTL)
protocol Source address Destination address options Source (0 – 65535) Destination port (0 – 65535) Combined length of data and UDP header(8 Destination port (0 – 65535) – 65535) Data
74
31
CONSTRUCTORS FOR RECEIVING DATAGRAMS The following constructors create a new DatagramPacket objects for receiving data from the network. 1. public DatagramPacket(byte[] buffer, int length) 2. public DatagramPacket(byte[] buffer, int offset, int length)
CONSTRUCTORS FOR SENDING DATAGRAMS The following constructors create a new DatagramPacket objects for sending data across the network. 1. public DatagramPacket(byte[] data, int length, InetAddress destination, int port) 2. DatagramPacket(byte[] data, int offset, int length, InetAddress destination, int port)
DATA GRAM METHODS
It has 5 methods that retrieve different parts of a datagram. The actual data plus several fields from its header. These methods are mostly used for datagrams you receive from the network. 1. public InetAddress getAddress(): Returns InetAddress object containing the address of the remote host. 2. public int port(): Returns an integer specifying the remote port. 3. public byte[] getData[]: Method returns a byte array containing the data from the datagram. 4. public int getLength() – method returns the number of bytes of data in the datagram. 5. public int getOffset() – returns the point in the array returned by getData() where the data from the datagram begins.
DATA GRAM SET METHODS 1. public void setData(byte[] data) – changes the payload of the UDP datagram. 2. public void setData(byte[] data, int offset, int length) – alternative method for sending large amount of data.
75
3. public void setAddress(InetAddress remote) – lets you change the address a datagram packet is sent to. 4. public void setPort(int port) – changes the port a datagram is addressed to. 5. public void setLength(int length) – changes the number of bytes of data in the internal buffer that are considered to be part of the datagram’s data as opposed to merely unfilled space.
SENDING AND RECEIVING DATA GRAMS public void send(DatagramPacket dp) throws IOException – for sending datagram packet. ublic void receive(DatagramPacket dp) throws IOException – for receiving datagram packet.
MULTICAST SOCKETS/UNICAST SOCKETS •
Unicast socket provides point-to-point communication.
•
Unicast sockets create a connection with two well-defined endpoints.
•
There is one sender and one receiver, and although they may switch roles, at any given time it is easy to tell which is which.
UNICAST SOCKETS •
Multicasting sends data from one host to many different hosts, but not to everyone.
•
The data goes only to clients that have expressed an interest in the data by joining a particular multicast group.
•
Protocols require broadcasts only when there is no alternative, and routers limit broadcast to the local network or subnet, preventing broadcasts from reaching the Internet at large.
•
Inicast is useful during Real Audio, Video broadcasting.
76
COVERAGE OF PACKET WITH A TTL OF 5 0 CAMPUS WORLD
1
3
4
2
5
3
4
3
4
2
1
2
3
0
1
Packet dies 2
1
2
COMMON PERMANENT MULTICAST ADDRESS Domain name BASE-ADDRESS.MCAST.NET
IP address 224.0.0.0
Purpose The reserved base address. This is never assigned to ay multicast group
ALL-SYSTEMS.MCAST.NET
224.0.0.1
All systems on the local subnet
ALL-ROUTERS.MCAST.NET
224.0.0.2
All routers on the local subnet
AVMRP.MCAST.NET
224.0.0.4
All distance vector multicast routing protocol (ADVMRP) routers on this subnet.
MOBILE-AGENT.MCAST.NET
224.0.0.11
Mobile agents on the local subnet.
77
CLIETNS AND SERVERS IN NULTICAST GROUP 1. When a host wants to send data to a multicast group, it puts that data in multicast datagrams, which are nothing more than UDP datagrams addressed to a multicast group. 2. Most multicast data is either audio or video, or both. 3. If few pixels or even a whole frame of video is lost in transit, the signal isn’t blurred beyond recognition. 4. Therefore, multicast data is sent via UDP, which, though unreliable, can be as much as three times faster than data sent via connection-oriented TCP. ESTIMATED TTL VALUES FOR DATAGRAMS ORIGINATING IN CONTIENTAL US
DESTINATION The local host The local subnet The local campus – that is, the same side of the nearest Internet router – but on possible different LANs High band width sites The United States North America High bandwidth sites worldwide All sites worldwide
TTL VALUE TO USE 0 1 16 32 48 64 128 255
ROUTERS AND ROUTING •
A multicast socket sends one stream of data over the Internet to the clients’ router.
•
The router duplicates the stream and sends it to each of the clients.
•
Without multicast sockets, the server would have to send four separate but identical streams of data to the router, which would route each stream to a client.
78
•
Using the same stream to send the same data to multiple clients significantly reduces the bandwidth required on the Internet backbone.
With Multicast Socket
With out Multicast Sockets
Clients
clients
----------------------------------------------------------------------------------------------------------Router
-----------------------------------------------------------------------------------------------------------THE INTERNET Router -------------------------------------------------------------------------------------------------------------Server
Just create a Multisocket, have the socket join a multicast group, and stuff the address of the multicast group in the DatagramPacket you want to send. •
The routers and the MulticastSocket class take care of the rest.
•
The biggest restriction on multicasting is the availability of special multicast routers (mrouters).
•
Mrouters are reconfigured internet routers or workstations that support the IP multicast extension.
79
•
Multicasting is supported in the built-in TCP stack of Microsoft Windows 95, 98 and NT.
•
On Macs, multicasting is supported by Open Transport but not by MacTCP.
•
Pinging in multicasting can be done using all-routers.mcast.net.
CONSTRUCTORS IN MULTICASTING 1. public MulticastSocket() throws SocketException 2. public MulticastSocket(int port) throws SocketException – receives datagram on a well known port.
COMMUNICATING WITH A MULTICAST GROUP Once a Multicast Socket has been created, it can perform four key operations. These are 1. 2. 3. 4.
Join a multicast group Send date to the members of the group Receive data from the group Leave the multicast group
THE URL CONNECTION CLASS •
URLConnection is an abstract class that represents an active connection to a resource specified by a URL.
•
The URLConnection class has two different but related purposes.
•
First it provides more control over the interaction with a server than the URL class.
•
With a URLConnection, you can inspect the MIME headers sent by an HTTP server and respond accordingly.
•
Second, the URLConnection class is part of Java’s protocol handler mechanism, which also includes the URLStreamHandler class. OPENING URLCONNECTION: A program that uses the URLConnection class directly follows this basic sequence of steps.
•
80
1. Construct a URL object 2. Invoke the URL object’s openconnection() method to retrieve a URLConnection object for that URL. 3. Configure the URLConnection 4. Read the header fields. 5. Get an input stream and read data. 6. Get an output stream and write data. 7. Close the connection.
SYNTAX try { URL u = new URL(http://www.gogle.com); URLConection uc = u.openConnection(); } catch (IOException e) { System.err.println(e); }
EXAMPLE FOR URL CONNECTION IMPLEMENTATION JAVA PROGRAM TO DISPLAY THE CONTENTS OF THE HOME PAGE UrlConnection.java // Demonstrate URLConnection import java.net.*;
81
import java.io.*; import java.util.Date; class UrlConnection { public static void main(String args[]) throws Exception { int c; URL hp = new URL("http://www.google.com"); URLConnection hpCon = hp.openConnection(); System.out.println("Date
: " + new
Date(hpCon.getDate()));
System.out.println("Content-Type : " + hpCon.getContentType()); System.out.println("Expires
: " + hpCon.getExpiration());
System.out.println("Last-Modified : " + new Date(hpCon.getLastModified())); int len = hpCon.getContentLength(); System.out.println("Content-Length : " + len); if(len > 0) { System.out.println("=========Content ========="); InputStream input = hpCon.getInputStream(); int i = len; while (((c = input.read()) != -1) && (--i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No content Available "); } }
82
}
EXPLANATION 1. getDate()- Returns the date. 2. getContentType()- Returns the type of Content, for example whether text/html document. 3. getExpiration()- Returns 0 if the web site is not expired. 4. getLastModified()- Returns the date in which the web site is modified.(Latest date of modification)
RETRIEVING SPECIFIC MIME HEADER FIELDS 1. public String getContentType() – returns the MIME content type of the data. 2. public String getContentLength() – returns the MIME content length in bytes. 3. public String getContentEncoding() – returns a String that tells you how the content is encoded. 4. public long getDate() – methods returns a long that tells you when the document was sent, in milliseconds since midnight, GMT. 5. public long getExpiration – Some documents have server-based expiration dates that indicate when the document should be deleted from the cache and reloaded from the server. 6. public long getLastModified() – returns the date on which the document was last modified.
83
2.6 CLIENT SERVER PROGRAMS 2.6.1 HTTP SERVER PROGRAM import java.net.*; import java.io.*; class WebServer { public static void main(String args[]) { ServerSocket serversocket= null; Socket clientSocket = null; int connects=0; try { //Create the serverSocket serversocket = new ServerSocket(80,5); while(connects<5) { clientSocket = serversocket.accept(); ServiceClient(clientSocket); connects++; } serversocket.close(); } catch(IOException ioe) { System.out.println("Error in Simple Webserver:" + ioe); } } public static void ServiceClient(Socket client) throws IOException { DataInputStream inbound =null; DataOutputStream outbound=null; try { //Accquire the streams for IO inbound = new DataInputStream(client.getInputStream());
84
outbound=new DataOutputStream(client.getOutputStream()); //Format the output(response header and tiny HTML Document StringBuffer buffer=new StringBuffer(" Welcome to Sample WebServer "); String inputLine; while((inputLine=inbound.readLine())!=null) { if(inputLine.equals("")) { outbound.writeBytes(buffer.toString()); break; } } } finally { //cleanup System.out.println("Cleaning up connectiojn:"+client); outbound.close(); inbound.close(); client.close(); client.close(); } } }
HTTP CLIENT PROGRAM import java.io.*; import java.net.*; public class WebClient { public static void main(String args[]) { try { Socket clientSocket1=new Socket("localhost",80); System.out.println("Client1:"+clientSocket1); getPage(clientSocket1); } catch(UnknownHostException uhe) {
85
System.out.println("UnknownHostException:"+uhe); } catch(IOException ioe) { System.out.println("IOxception"+ioe); } } public static void getPage(Socket clientSocket) { try { DataOutputStream outbound=new DataOutputStream(clientSocket.getOutputStream()); DataInputStream inbound=new DataInputStream(clientSocket.getInputStream()); outbound.writeBytes("GET/HTTP/1.0\r\n\r\n"); String responseLine; while((responseLine=inbound.readLine())!=null) { System.out.println(responseLine); if(responseLine.indexOf("