Client - Server Programming: Introduction
With the emergence of more powerful desktop computers it became less and less necessary for companies to rely on big, fast mainframe computers in the mid 80's. So, instead of using one big computer with centralized software and 'dumb' terminals on each desk they put a personal computer on each desk and 'empowered' the employee to use whatever software was best for the task at hand. However, companies had large databases which could not be put on every personal computer: first, they were simply too big for typical PC hard disks, and secondly, if one person updated the database, the next person needs to see these changes. Thus, with large databases, they had two choices: stick with the old approach of having one central mainframe computer to handle all database requests, or develop a new strategy. Of course, they chose the second approach and developed the 'client-server' approach. Actually, this approach was not so new: it has been used since the late 60's for the Internet as it existed then. But the need that big corporations had for this client-server approach made it quickly very popular (similar to the Internet today: many Universities used the Internet extensivly for decades, but the 'general population' did not even know about its existence; as soon as large corporations thought that they could use the Internet for their own purposes, the term became commonplace). So, what is this client - server approach ?
- The Server (typically)
- An applications runs on a large computer (large meaning either fast or large storage space or both). The server application is usually written in such a way that it does not provide any method to interact with a user. Instead, it waits for other programs to connect (i.e. other programs take the place of a user) and interacts with these programs. Typically, a server application has control over large amounts of data, and can access that data fast and efficiently. It can also handle requests by many clients (more or less) simultaneously.
- The Client (typically)
- An application that runs on a personal computer. It has an extensive and appealing user interface, but it has no data that it can manipulate or present to the user. The client application 'asks' what a user wants, then connects to the server application to obtain that data. Once the data has been obtained, it is presented to the user in a nice format. A client usually gets the data from one server at a time, and interacts with one user only.
- A client-server database program would work as follows: The database is stored on the computer where the server application is running. The server application can access each part of the data very fast, but a user can not interact with the server program. A person sits on another, comparatively small computer and starts the client program. It presents a convenient screen image, and the user enters, say, search criteria to search for a particular item in the database. Once the criteria has been entered in the client program, it connects to the server program and presents the request. The server program searches for the appropriate data in the database and delivers it (in a machine-suitable, often compressed) format. The client then 'decodes' the information and displays it nicely on the screen.
Client-Server programs can run on any type of network. These days, however, people are most interested in them when they utilize the Internet. So, we will only talk about client-server programs running on the Internet, and we will oversimplify many issues to understand the basic ideas and concepts.
Connections to a machine on the Internet are made through what's called a 'port'. A Unix machine, for example, has thousands of ports, and behind each portsa particular server program could wait for a connection. Ports are like 'doors', and behind each door could be a server program waiting for a client program to know at that particular door. The ports 1 - 1000 are reserved to be accessible only by programs with special rights, while the ports 1000 - 9999 are available for every program, unless a program already 'guards' that port. Many programs are standardized to use specific ports:
|Server||Description||Standard Port||Sample Client|
|telnetd||Terminal program. Passes everything it receives to the operating system, and all results are passed back to the client. Little 'translation' takes place.||23||telnet|
|ftpd||File Transfer Protocol: understands specific commands such as 'dir', 'get', etc, and acts according to them by serving files to the client.||21||ftp|
|smtpd||Small Mail Transport Protocol: handles sending and retrieving of electronic mail.||25||built-in to many email programs;|
|httpd||Hyper Text Transfer Protocol: understands specific commands and serves web pages to the client||80||Netscape, Mosaic, and others|
|gopherd||Gopher Server: similar to httpd||70||Gopher, Netscape, Mosaic, and others|
|popd||Post Office Protocol: understands specific commands and accesses electronic mail already received and stored on a machine.||110||built-in to many email programs|
|nntpd||Network News Transfer Protocol: handles the transfer of newsgroup lists, posting, etc.||119||Unix news packages|
|echod||Simply returns everything that a client sends to it||7||Anything|
|daytime||Returns the system time and date||13||Time-synchronizing programs|
|fingerd||Returns information about users of the system||79||finger|
Since the telnet client-server combination is the most general, we can use it to explore some of the other protocols. First, the Unix telnet client command has the following form:
telnet host [port]
where host is the machine name or number to connect to and port is the port to try and access. If no port number is given, the standard telnet port 23 is used. But, you can use telnet to connect to any other port as well. Let's try the following:
to retrieve the time on those machines. Next, try:
and type a couple of lines. Now, try
now we need to know which commands to type. Try "help" first. Then try "helo", finally try "quit".
we again need to know which commands to type. Try "help", as before. Then try: "list". Does not work. Try "user". Not enough. Try "user anonymous". Try "pass email@example.com". Now try "list". Still does not work, so try "quit".
try any command. Then try again, but enter a username instead (such as 'wachsmut').
try any command. Then try 'get index.html'.
try anything. Try 'control-]' and type 'quit' to quit.
Try "user username" and "pass yourPassWord" to login, then
"quit" to exit again.
Note that almost all Unix machines allow access to these ports, whether you have an account on the system or now. Once you are connect, it is the job of the server program to make sure you do not have any unauthorized access to information on the system. Moreover, most server programs understand some basic commands and can act upon these commands. The commands and the corresponding actions are usually called the "protocol" that the server speaks.
What is a Socket:
A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively. Here is an example of a Java client program that connects to the echo client of a particular machine.
This client program is straightforward and simple because the Echo server implements a simple protocol. The client sends text to the server, the server echos it back. When your client programs are talking to a more complicated server such as an http server, your client program will also be more complicated. However, the basics are much the same as they are in this program:
|Open a socket.|
|Open an input stream and output stream to the socket.|
|Read from and write to the stream according to the server's protocol.|
Only the third step differs from client to client, depending on the server. The other
steps remain largely the same.