Title: Programming the Server
1Programming the Server
- What happens on the server when the client tries
to establish a rendezvous ? - The server starts listening to requests on a
ServerSocket - After accepting the request the resulting
connection is attached to another (normal) socket
(same type as clients socket)
2Sockets at the Server Side (1)
- The server should start by creating a server
socket bound to a certain port according to the
protocol of the service. -
- ServerSocket listening
- listening new ServerSocket(5555)
- This will create the socket but the server is
still not listening. To do this we should apply
the following method to the socket - Socket toClient listening.accept()
- This sentence works the following way
- accept() blocks the execution of the program
until a request for a rendezvous from a client is
received. - When the requirement arrives, a tcp connection is
established between the two computers. The client
receives in its socket one end of this link and
the server the other. The server sides socket
(from the Socket class) is chosen conveniently by
the system
3Sockets at the Server Side(2)
- At the server side we can apply the same methods
to the normal socket as we did at the client
side. Particularly we may need to open an input
and an output data stream. - After this, the server should implement the
communication protocol which was established and
published (by any other possible mean). It is
important that both side follow this protocol in
order not to block the communication and/or miss
some data. This mean nothing else than following
the turn taking rules of writing to and reading
from the socket and the format of the data to be
exchanged. - Note that the server socket (and port) at which
the server was originally listening to requests
is not used anymore. This is a design issue.
4 A Date Server
We will program now a date server for a computer
which has no one (for example, a MS-Windows
computer)
3) answer with the date in another socket
4) close the connection
Client
Date server
13
1) Create the server socket 2) start listening
DateServer
DateClient2
5 An Echo Server
We will program now a date server for a computer
which has no one (for example, a MS-Windows
computer)
4) answer with the same
3) Request a line
Client
Date server
7
1) Create the server socket 2) start listening
Do 3 4 until client disconnects or sends a line
with
EchoServer
EchoClient2
6Lets program something rather simple
- The TalkServer waits for someone wishing to
communicate - The TalkClient asks for a host name and tries
the redezvous - After the communication is set up, everything
the client user types in will be transmitted to
the talk server and this will display it on the
screenboard
Bla bla from keyboard
Bla bla
Talk Server
Talk client
7Lets see another example of simple client-server
communication with TCP
Open server socket port 4444 While(true)
accept call open reading from socket
while (true) read line from socket
if (line.equals(bye)) break
write line to screen //end of the
call
snew Socket(args0,4444) open writing to
socket while (true) read line from
keyboard write to socket if
(line.equals(bye)) break
TalkClient
TalkServer
8Sockets File transfer (1)
- We will now develop programs for transmitting
files . - The one receiving the file starts listening for
someone who wants to transmit a file (the server
!!!) - The sender knows where (hostname and port number)
the server is listening and sends a rendezvous
request. - The data transfer is done at the byte level in
order to allow the transfer of non textual files. -
-
9Transmitting files
- The sender tries a
- rendezvous with receiver
- The reciver starts listening for
- Requests to send (upload) files
4) Send bytes
3) Read bytes from file
5) Write bytes in file
Repeat 3,4,5 until all the file is transmitted
See ArchEnviador.java ArchRecibidor.java
10A more intelligent file server
1) Filename from keyboard
2) Request file
4) Send file
3) Read File
5) Write file
Repeat 3,4,5 until all the file is transmitted
See ArchServer.java ArchCliente.java
11Stateless vs. Stateful servers the problem of
reading a remote file by steps. File reading
requests arrive with delay
Request open file XYZ
A CLIENT
A SERVER
?
Answer file XYZ exists and ready
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
12A stateless server means it does not remember
previous requests
Request read bytes 0 to 49 from file XYZ
A CLIENT
A SERVER
?
Answer the content of the bytes
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
13The client must provide all the information again
!
Request read bytes 50 to 99 from file XYZ
A CLIENT
A SERVER
?
Answer the content of the bytes
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
14This may cause a lot of network traffic,
especially if there are many clients
Request read bytes X to X50 from file XYZ
A CLIENT
A SERVER
?
Answer the content of the bytes
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
15Stateful Server it maintains some information
abut what clients did
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
Pointer File Position
0 XYZ 0
1 FILE ZXY 50
Request open file XYZ
A CLIENT
A SERVER
?
Answer file pointer to file XYZ
16The information the client has to pass to the
server is much smaller
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
Pointer File Position
0 XYZ 50
1 FILE ZXY 50
Request 0, read 50
A CLIENT
A SERVER
?
Answer the content
17The information at the server should be updated
with every request
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
Pointer File Position
0 XYZ 100
1 FILE ZXY 50
Request 0, read 50
A CLIENT
A SERVER
?
Answer the content
18It is important to close the file !!!
Open file XYZ read first 50 bytes while (not end
of file XYZ) read next 50 bytes close file
Pointer File Position
0 XYZ 100
1 FILE ZXY 50
Request 0, read 50
A CLIENT
A SERVER
?
Answer the content
19Communications the networked approach
- Every 2 applications which want to start a
communication open an exclusive channel
(hostnames and ports mut be known to each other) - We have n(n-1)/2 channels for n applicatons
- Advantages
- An exclusive channel, no bottlenecks
- Drawbacks
- Every application must be aware of port and host
address. - Management of incomming/outgoing members
20The centralized (star) approach
- Applications send their communications
requirements to a server. This will forward the
message to the final destinatary. - We have at most n communication channels
- Advanatages
- Easier management of the communication
- Drawbacks
- Server saturation.
21Software Arquitecture replicated applications
- In a replicated schema an instance of the
application is started at every place - The applications may differ from each other
(since they are started independently) to support
different roles.
22Software Arquitecture centralized application
- In a centralized schema there is only one
application which sends it outputs to the screen
(window server program) of all computers - It also collects the input from all them
- Net meeting is a famous example of this approach
- Advantages and Drawbacks ?