Client-Server

Download Report

Transcript Client-Server

CSC111H
Client-Server: An Introduction
Dennis Burford
[email protected]
1
Internet Communication
• Communication on the Internet is the transfer of data from
one machine to another.
– downloading of web pages
– email
– chat rooms.
• Transfer of this information is unreliable – it can get lost
along the way.
• So, data to be transferred is broken into pieces called
packets (Don’t put all your eggs in one basket!)
2
Internet Communication
• If a computer is expecting a packet, and it doesn’t arrive, it
asks for just that packet to be re-sent (rather than the whole
message).
• Also, a packet may be corrupted by noise on the network:
We need a way of checking its contents (a checksum).
• A packet may pass through many other machines before it
reaches its destination (like the many stops of an airmail
letter).
3
Identifying a Machine
• Telling the packets where to go…
• Normal Mail is given a destination in the form of a
postal address.
• Similarly, every machine on the Internet is uniquely
identified by its IP (Internet Protocol) address.
4
Identifying a Machine
• IP addresses exist in 2 forms:
– DNS (Domain Name Service) form, e.g.
casper2.cs.uct.ac.za
marsite.cs.uct.ac.za (my machine)
scilab.uct.ac.za (the scilab server).
– “Dotted Quad” form.Four numbers separated by dots, e.g.
casper2-> 137.158.128.58
marsite-> 137.158.133.27
scilab-> 137.158.128.57
• Use “ping” on DOS prompt
5
TCP and Sockets
• TCP/IP (Transmission Control Protocol/Internet Protocol): a
reliable direct connection between 2 programs on different
machines.
• A TCP connection between 2 programs is like a phone
connection:
– data can move reliably in both directions simultaneously
– before communication can take place, a connection has to be made
(one must call the other)
– both parties need something besides the connection (a telephone: a
device to hear and speak).
• In TCP connections, the “telephone” is called a socket.
6
TCP and Sockets
• A socket object is needed to allow programs to communicate
across a connection
• In Java, there is an existing class called Socket, defined in
java.net.
• The constructor takes 2 arguments:
– an IP address (...of the machine being contacted)
– a port number (...of the program we want to talk to)
7
Port Numbers
• Network programs offering a service can “listen” on a
particular port number on a computer. The port identifies
which service is involved.
• Some default port numbers:
– FTP (data connection)
– telnet
– http (WWW protocol)
20
23
80
• The combination of a port number and IP address identifies
the program (like the number of a room in a building).
8
Client-Server
• The client-server model is a particular approach to
communicating, used in Internet applications.
• In this model an application consists of 2 programs
running:
– One program runs as a server (providing a service).
– The other program runs as a client (requesting the service).
• The client requests a service as follows:
– Creates a Socket object, with the machine address and
appropriate port number of the server
– Sends a message to the server
– Waits for a response from the server
9
Protocols
• Each client-server combination has its own rules on how the
conversation should work - a protocol.
• A Protocol can be thought of as the rules of conduct and the
format of the communication.
• Real world example:
– Ordering a McDonald’s meal (in English)
10
Protocols
• Web clients and servers
– Protocol called HTTP (Hyper-Text Transport Protocol).
• A client program (such as Internet Explorer) requests an html file from
a web server (such as Apache). The command is GET <filename>
• A blank line indicates the end of the request.
• The server then sends information about the server and the file (such
as when it was last modified), followed by a blank line.
• The server then sends the HTML code to the client, which displays it.
• The server breaks the connection when it has sent all the HTML.
• Email clients and servers
– Protocol called SMTP (Simple Mail Transfer Protocol).
11
Chat Program
Fred
Sally
Chat
Server
Client
-:
Client
-:
John
Client
-:
12
Chat Program
Fred
connect
connect
Sally
Chat
Server
-:
connect
-:
John
-:
13
Chat Program
Fred
Sally
Chat
Server
-:
-:
John
-:
14
Chat Program
Fred
Sally
Chat
Server
-: Hello everyone
-:
John
-:
15
Chat Program
Fred
Sally
Chat
Server
-: Hello everyone
-:
John
-:
16
Chat Program
Fred
Sally
Chat
Server
-: Hello everyone
-:
John
-:
17
Chat Program
Fred
Sally
Fred> Hello everyone
Chat
Server
-: Hello everyone
Fred> Hello everyone
-:
John
Fred> Hello everyone
-:
18
Chat Program
Fred
Sally
Fred> Hello everyone
Chat
Server
-:
Fred> Hello everyone
-:
John
Fred> Hello everyone
-:
19
Understanding the Files for the
Project
• Most of the java files which you have been given are
interfaces.
• For example: The ChatProgram interface has one method
specified, called acceptMessage.
• Your program will implement ChatProgram, because it has
to be able to accept messages from the server.
public interface ChatProgram
{
public boolean acceptMessage(String msg);
}
20
The ChatServer
• The server part of the program has been written for you
(ChatServer.java).
• ChatServer implements ChatServerInterface. This
means that it has methods called login, logout and
putMessage.
• To connect to the server, we use the ConnectionToServer
class, which implements ChatClientInterface.
• You are required to write the client-side of the chat
program.
21
ChatServer Interface
public interface ChatServerInterface extends java.rmi.Remote
{
boolean login(String id, ChatClientInterface client)
throws java.rmi.RemoteException;
boolean logout(ChatClientInterface client)
throws java.rmi.RemoteException;
boolean putMessage(String msg, ChatClientInterface client)
throws java.rmi.RemoteException;
}
22
ChatClientInterface
public interface ChatClientInterface extends java.rmi.Remote
{
boolean putBroadCastMessage(String msg)
throws java.rmi.RemoteException;
public void addPerson(String msg)
throws java.rmi.RemoteException;
public void removePerson(String msg)
throws java.rmi.RemoteException;
}
23
The Client-Side
•
•
The client which you write must react to 2 events. Both
use a ConnectionToServer object.
How to create a ConnectionToServer object:
– The constructor takes 3 parameters:
hostName (IP number of computer you want to connect to)
nickName (name to be used for chatting)
program (an instance of ChatProgram which you have written).
•
From the server to the client:
•
•
The ConnectionToServer object ensures that
acceptMessage of ChatProgram is called whenever
something arrives at the server
From the client to the server:
– Invoke the sendMessage method of ConnectionToServer
24