Java Reference
In-Depth Information
With so many protocols, requests, and back-and-forth communication going on, it is nothing short
of amazing that you are able to view a simple web page in less than one second. To standardize
this wealth of protocols, the International Organization of Standardization (ISO) maintains the
so‐called Open Systems Interconnection (OSI) model, which defines aspects of communication into
seven layers, from bottom to top:
Layer 1: Physical layer: Includes the Ethernet protocol, but also USB, Bluetooth, and other
radio protocols
Layer 2: Data link layer: Includes the Ethernet protocol
Layer 3: Network layer: Includes IP (Internet Protocol)
Layer 4: Transport layer: TCP and UDP
Layer 5: Session layer: Includes protocols for opening/closing and managing sessions. TCP's
session management is found on this layer.
Layer 6: Presentation layer: Includes protocols to format and translate data. This layer also
includes encryption protocols.
Layer 7: Application layer: HTTP and DNS, for instance
Not all network communications need to use protocols from all these layers. To request a web page
for instance, layers 1 (physical), 2 (Ethernet), 3 (IP), 4 (TCP), and 7 (HTTP) are involved, but the
layers are constructed so that each protocol found at a higher level can be contained inside the mes-
sage of a lower‐layer protocol. When you request a secure web page, for instance, the HTTP mes-
sage (layer 7) will be encoded in an encrypted SSL or TLS message (layer 6).
The lower the layer you aim for when programming networked applications, the more functionality
and complexity you need to deal with. In Java, for example, it is possible to set up so‐called server
and client “sockets,” which are networking endpoints that define a TCP/IP, UDP/IP, or raw IP
connection. Once this connection is established, it is up to you, the programmer, to define how the
actual messages you transmit should look, so that you are then effectively creating a higher‐layered
protocol on top of this.
Creating a Simple Networked Application in Java
try it out
Now that you have the basics of networking under your belt, take a quick look at how you would
implement the most basic client‐server networking application in Java using a simple example.
Similarly to how HTTP works, you will create a simple request‐reply program using TCP/IP sockets
in Java.
Note that this is the only time when you will be applying Java sockets directly. Earlier in this chapter,
you read that programming this close to the “networking metal” is a complex and challenging task,
and in this Try It Out, you'll see the reasons why.
1.
Create two classes in Eclipse: TCPServer and TCPClient . Put them in a package called
socketexample .
2.
Write the following source code for the TCPServer class:
Search WWH ::




Custom Search