Você está na página 1de 29

Client/Server Middleware

RPC, Messaging, Peer to Peer

RPC, Messaging, Peer to Peer


How do clients and servers talk to each other How are the request and response synchronized How are dissimilar data representation on different computer handled How if one of the parties are not available

Peer to Peer communication


Early client server application were implemented using low level, conversational, peer to peer protocols - sockets - TLI - CPIC/APPC - Net Bios - Named Pipes P2P protocols are hard to code and maintain P2P means that 2 sides of communication link use the same protocol interface to conduct conversation

Peer to peer communication


Not popular - solution Provide higher level of abstraction RPC, MOMs, ORBs Use by very demanding user app/system-level system

What is a socket?
Original idea came from UNIX The network is just like a file system Read and write a stream of data to the network through socket A socket is bound to a port number so that the TCP layer can identify the correct application for the data

5 fadzlihasan@unisza.edu.my

What is a socket?
A little background
Socket provides the TCP/IP communication protocol
Introduced in 1981- UNIX BSD 4.2 Later Sun build RPC and NFS over the socket

Is supported by all OS
WinSocks for Windows 3.1 Built into Windows 95, and NT For other OS it is not new

6 fadzlihasan@unisza.edu.my

Communication between processes : using port and socket


socket any port agreed port socket

message client other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 server

Port: The destination of a message Socket: The final point for processes communication

7 fadzlihasan@unisza.edu.my

ports in TCP are also represented by a number in the range 1 65 535 Ports below 1024 are restricted to use by well-known services. For example,
Telnet (port 23) SMTP (port 25) HTTP (port 80) POP3 (port 110)

8 fadzlihasan@unisza.edu.my

Sockets

Figure A

Figure B

Socket Operations
TCP sockets can perform a variety of operations:
Establish a connection to a remote host Send data to a remote host Receive data from a remote host Close a connection

10 fadzlihasan@unisza.edu.my

Socket Operations
There is a special type of socket that provides a service that will bind to a specific port number. Normally used only in servers, this socket can perform the following operations:
Bind to a local port Accept incoming connections from remote hosts Unbind from a local port

11 fadzlihasan@unisza.edu.my

TCP and the Client/Server Paradigm


In network programming, applications that use sockets are divided into clients and servers. A client is software that initiates a connection and sends requests. A server is software that listens for connections and processes requests.

12 fadzlihasan@unisza.edu.my

Network Clients
Network clients initiate connections and control network transactions. The server fulfills the requests of the client but not the other way round. The network client speaks to the server using a network protocol. E.g an HTTP client communicates with an HTTP server using HTTP. Port numbers are used to enable clients to locate server applications. E.g. a web server uses port 80.
13 fadzlihasan@unisza.edu.my

Network Servers
The role of the network server is to bind to a specific port and to listen for new connections. Unlike the client, the server must run continually in the hope that some client will want its services. The server runs indefinitely. Normally, it is automatically started when the host computer of the server is started.

14 fadzlihasan@unisza.edu.my

Network Servers
Some servers can handle only one connection at a time, while others can handle many connections concurrently, through the use of threads. Some protocols (e.g. HTTP/1.0) normally allow only one request per connection. Others, like POP3, support a sequence of requests. Servers answer the client request by sending either a response or an error message.

15 fadzlihasan@unisza.edu.my

Socket Types
In Java, there are 4 main socket types:
ServerSocket Socket DatagramSocket MulticastSocket

Accessible by importing java.net.*;

16 fadzlihasan@unisza.edu.my

TCP Sockets and Java


Java provide the following classes for TCP sockets:
java.net.Socket java.net.ServerSocket

The Socket class should be used when writing client software. The ServerSocket class should be used when writing server software.

17 fadzlihasan@unisza.edu.my

Socket (client)
Textbook page 273

fadzlihasan@unisza.edu.my

18

Socket Class
Socket objects represent client sockets, and is a communication channel between two TCP communications ports belonging to one or two machines.

19 fadzlihasan@unisza.edu.my

Socket Class
There are several constructors for the Socket class. The easiest way to create a socket is shown below:
Socket mySocket; try { mySocket = new Socket("www.aol.com", 80); } catch (Exception e) { }
20 fadzlihasan@unisza.edu.my

Reading from and Writing to TCP Sockets


In Java, once a socket is created, it is connected and ready to read/write by using the socket's input and output streams. Use the methods getInputStream() and getOutputStream() to access those streams.

21 fadzlihasan@unisza.edu.my

Socket socket; InputStreamReader isr; BufferedReader br; PrintStream ps; try { socket = new Socket("www.aol.com",80);

isr = new InputStreamReader(socket.getInputStream()); br = new BufferedReader(isr); ps = new PrintStream(socket.getOutputStream());

} catch (Exception e) { }
22 fadzlihasan@unisza.edu.my

ServerSockets (server)
Textbook page 311
fadzlihasan@unisza.edu.my 23

Server Socket
Server Socket runs on the server and listens for incoming TCP connections. Server socket is bound to a certain port on the server machine. When it is successfully bound to a port, it immediately listens to any attempt of incoming connection When a server detects an attempt of incoming connection, it negotiates the connection between client and server. This creates/opens a regular Socket which handles communication between the client and server
24 fadzlihasan@unisza.edu.my

ServerSocket Class
java.net.ServerSocket represents the server socket ServerSocket object is created on a local port and calls method accept() to listen to incoming connection accept() will block until a connection is detected. Then it returns a Socket object that handles communication with a client

25 fadzlihasan@unisza.edu.my

The easiest way to create a socket to listen at a certain port is shown below:
ServerSocket mySocket; try { mySocket = new ServerSocket(80); } catch (Exception e) { }

26 fadzlihasan@unisza.edu.my

Accepting and Processing Requests from TCP Clients


The most important function of a server socket is to accept client sockets. Once a client socket is obtained, the server can perform all the "real work" of server programming, which involves reading from and writing to the socket to implement a network protocol. Example: a mail server that provides access to stored messages would listen to commands and send back message contents.

27 fadzlihasan@unisza.edu.my

ServerSocket server; BufferedReader reader; PrintWriter writer; server = new ServerSocket(13); while (true) { Socket client = server.accept(); reader = new BufferedReader( new InputStreamReader( client.getInputStream())); writer = new PrintWriter( new OutputStreamWriter( client.getOutputStream())); }

28 fadzlihasan@unisza.edu.my

Take a break!
What are threads?

fadzlihasan@unisza.edu.my

29

Você também pode gostar