Java TOPIC 21 – CLIENT SERVER
PROGRAM LESSON NOTE INTRO – SERVER PROGRAM A
server is a program that waits for requests from other programs (usually
clients). It then processes the
request and sends an appropriate response. There
are different types of servers. One
common example is a webserver which processes requests for html pages. Another example is an ftp server that
receives and sends requested files using the FTP protocol. All online games have a server that simply
waits for people to join the game. It
then logs you in and sends you into the game. You
can create a server to do many things.
INTRO – CLIENT PROGRAM A
client program is simply a program that sends requests to a server. It also gets responses from the server and
displays them as necessary. COMMUNICATION DETAILS When
you pick up a telephone to call somebody, you need to know their area code,
their phone number and their name. In
order for a client program to communicate with a server program, it needs to
know the following information:
When
the server gets a request from a client, it also receives the client’s IP
address and port information. This
way, the server knows where to send its reply. LAYERS We
all have heard that computers only deal with zeros and ones. So why then don’t we see these famous
sequences of zeros and ones anywhere? The
answer is that we work at a higher level or layer. This idea of layering occurs in many areas
in computers. TYPES OF COMMUNICATION – TCP vs UDP Computers
communicating with each other on the internet use either TCP (Transmission
Control Protocol) or UDP (User Datagram Protocol). Java has built-in classes that allow you to
work with either protocol so you do not need to know all the details. You will be working at a higher level. However,
to know which classes to use, you should know the general difference between
the two. TCP
– This protocol reserves a connection line between both computers that are
communicating. All data sent (packets)
is assured to have arrived. All data
also arrives in order. In the real
world, our phone networks works like this. UDP
– This protocol does not reserve a connection line between both
computers. It’s a free for all. So individual pieces of data (datagrams)
are sent off from one computer to another. No verification is made to see
that they have arrived. Also, there is
no guarantee that the data arrives in the same order that it was sent. In the real world, our mail system works
like this. In
this course, our focus will be on TCP. JAVA’S CLASSES FOR TCP The
classes we will focus on will be Socket and ServerSocket. To use them, you must use the following
import statement: import java.net.*; We
will also make use of different I/O classes. SOCKETS A
socket is one end-point of a two way connection between two programs. Each program uses a socket for the purpose
of communicating. The socket hides all
the ugly details that one would need to know about how to communicate. We simply use the Socket’s functionality to
do our communication. So, sockets
allow us to work at a higher level or layer. STEPS TO CREATING A CLIENT PROGRAM 1-Open
a socket. The client will create a socket that
connects it to the server. The client
needs to provide the server’s IP address and port number. 2-Open
I/O streams to socket. The socket provides us with I/O
streams to communicate over. This
sounds messy but its quite easy to follow once you do it once. 3-Read
from and write to the socket. (Send
and receive data.) You can now read and write to the
socket. Writing sends the data to the
server. Reading waits for the server
to send something. 4-Clean
up. You need to close the I/O streams
and the sockets. STEPS TO CREATING A SERVER PROGRAM 1-Open
a server socket. Specify the port on which the
server will listen. 2-Wait
for a connection request Once a request occurs, you will
create a normal socket to complete the connection. 3-Open
I/O streams to socket. This is the same as in the client
program. 4-Read
and write to socket. (Send and receive data.) This is the same as in the client
program. 5-Clean
up. This is the same as in the client
program. THE CODE
The
following are examples of a client and server that are communicating. You will have to change the host name in
the client code for you to be able to test it. COMMON ERRORS
|
|