Java Networking : An Overview of Networking, The Hypertext Transfer Protocol
An Overview of Networking
A network is a collection of computers that can communicate with each other. Depending on how wide the coverage is, a network can be referred to as a local area network (LAN) or a wide area network (WAN). A LAN is normally confined to a limited geographic area, such as a building, and comprises from as few as three to as many as hundreds of computers. A WAN, by contrast, is a combination of multiple LANs that are geographically separate. The largest network of all is, of course, the Internet.
The communication medium within a network can be cables, telephone lines, high-speed fiber, satellites, and so on. As the wireless technology gets more and more mature and inexpensive, a wireless local area network (WLAN) is becoming more commonplace nowadays.
Just like two people use a common language to converse, two computers communicate by using a common 'language' both agreed on. In computer jargon, this 'language' is referred to as protocol. What's confusing is that there are several layers of protocols. This is because at the physical layer two computer communicate by exchanging bitstreams, which are collections of 1s and 0s. This is too hard to be understood by applications and humans. Therefore, there is another layer that translates bitstreams into something more tangible and vice versa.
The easiest protocols are those at the application layer. Writing applications require you to understand protocols in the application layer. There are several protocols in this layer: HTTP, FTP, telnet, etc.
Application layer protocols use the protocols in the transport layer. Two popular ones at the transport layer are TCP and UDP. In turn transport layer protocols utilize the protocols at the layer below it. The diagram in Figure 1 shows some of these layers.
Figure 1. Layers of protocol in the computer network
Thanks to this strategy,
you don't have to worry about protocols in other layers than the
application layer. Java even goes the extra mile to provide classes that
encapsulate application layer protocols. For example, with Java, you do
not need to understand the HTTP to be able to send a message to an HTTP
server.
Another thing that you should know is that a network employs an addressing system to distinguish a computer from another, just like your house has an address so that the mailman can deliver your mail. The equivalent of the street address on the Internet is the IP address. Each computer is assigned a unique IP address.
The IP address is not the smallest unit in the network addressing system. The port is. The analogy is an apartment building that share the same street address but has many units, each with its own suite number.
The Hypertext Transfer Protocol (HTTP)
The
HTTP is the protocol that allows Web servers and browsers to send and
receive data over the Internet. It is a request and response protocol.
The client requests a file and the server responds to the request. HTTP
uses reliable TCP connections—by default on TCP port 80. The first
version of HTTP was HTTP/0.9, which was then overridden by HTTP/1.0.
Replacing HTTP/1.0 is the current version of HTTP/1.1, which is defined
in Request for Comments (RFC) 2616 and downloadable from
http://www.w3.org/Protocols/HTTP/1.1/rfc2616.pdf.
In the HTTP, it is always the client who initiates a transaction by establishing a connection and sending an HTTP request. The Web server is in no position to contact a client or make a callback connection to the client. Either the client or the server can prematurely terminate a connection. For example, when using a Web browser you can click the Stop button on your browser to stop downloading a file, effectively closing the HTTP connection with the Web server.
HTTP Requests
An HTTP request consists of three components:
Method—Uniform Resource Identifier (URI)—Protocol/Version
Request headers
Entity body
The following is an example of an HTTP request:
POST /examples/default.jsp HTTP/1.1
Accept: text/plain; text/html
Accept-Language: en-gb
Connection: Keep-Alive
Host: localhost
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US;
rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
Content-Length: 33
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
lastName=Franks&firstName=MichaelThe method—URI—protocol version appears as the first line of the request.
POST /examples/default.jsp HTTP/1.1
where POST is the request method, /examples/default.jsp the URI and HTTP/1.1 the Protocol/Version section.
Each HTTP request can use one of the many request methods as specified in the HTTP standards. The HTTP 1.1 supports seven types of request: GET, POST, HEAD, OPTIONS, PUT, DELETE, and TRACE. GET and POST are the most commonly used in Internet applications.
The URI specifies an Internet resource. It is usually interpreted as being relative to the server's root directory. Thus, it should always begin with a forward slash /. A Uniform Resource Locator (URL) is actually a type of URI (See http://www.ietf.org/rfc/rfc2396.txt). The protocol version represents the version of the HTTP protocol being used.
The request header contains useful information about the client environment and the entity body of the request. For example, it could contain the language the browser is set for, the length of the entity body, and so on. Each header is separated by a carriage return/linefeed (CRLF) sequence.
Between the headers and the
entity body, there is a blank line (CRLF) that is important to the HTTP
request format. The CRLF tells the HTTP server where the entity body
begins.
In the previous HTTP request, the entity body is simply the following line:
lastName=Franks&firstName=Michael
The entity body can easily become much longer in a typical HTTP request.
HTTP Responses
Similar to an HTTP request, an HTTP response also consists of three parts:
Protocol—Status code—Description
Response headers
Entity body
The following is an example of an HTTP response:
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Date: Thu, 12 Aug 2010 13:13:33 GMT Content-Type: text/html Last-Modified: Thu, 5 Aug 2010 13:13:12 GMT Content-Length: 112 <html> <head> <title>HTTP Response Example</title> </head> <body> Welcome to Brainy Software </body> </html>
The first line of the response header is similar to the first line of a request header. The first line tells you that the protocol used is HTTP version 1.1, the request succeeded (200 is the success code), and that everything went okay.
The response headers contain useful information similar to the headers in the request. The entity body of the response is the HTML content of the response itself. The headers and the entity body are separated by a sequence of CRLFs.