1 CSC111H Client-Server: An Introduction Dennis Burford [email protected].

24
1 CSC111H Client-Server: An Introduction Dennis Burford [email protected]

Transcript of 1 CSC111H Client-Server: An Introduction Dennis Burford [email protected].

Page 1: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

1

CSC111HClient-Server: An Introduction

Dennis Burford

[email protected]

Page 2: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

2

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!)

Page 3: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

3

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).

Page 4: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

4

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.

Page 5: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

5

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

Page 6: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

6

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.

Page 7: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

7

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)

Page 8: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

8

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) 20– telnet 23– http (WWW protocol) 80

• The combination of a port number and IP address identifies the program (like the number of a room in a building).

Page 9: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

9

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

Page 10: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

10

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)

Page 11: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

11

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).

Page 12: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

12

Chat Program

Fred

-:

Sally

-:

John

-:

Chat ServerClient Client

Client

Page 13: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

13

Chat Program

Fred

-:

Sally

-:

John

-:

Chat Server

connect connect

connect

Page 14: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

14

Chat Program

Fred

-:

Sally

-:

John

-:

Chat Server

Page 15: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

15

Chat Program

Fred

-: Hello everyone

Sally

-:

John

-:

Chat Server

Page 16: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

16

Chat Program

Fred

-: Hello everyone

Sally

-:

John

-:

Chat Server

Page 17: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

17

Chat Program

Fred

-: Hello everyone

Sally

-:

John

-:

Chat Server

Page 18: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

18

Chat Program

Fred

-: Hello everyone

Fred> Hello everyone

Fred> Hello everyone

Fred> Hello everyone

Sally

-:

John

-:

Chat Server

Page 19: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

19

Chat Program

Fred

-:

Fred> Hello everyone

Fred> Hello everyone

Fred> Hello everyone

Sally

-:

John

-:

Chat Server

Page 20: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

20

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);

}

Page 21: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

21

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.

Page 22: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

22

ChatServer Interfacepublic 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;

}

Page 23: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

23

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;

}

Page 24: 1 CSC111H Client-Server: An Introduction Dennis Burford dburford@cs.uct.ac.za.

24

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