Experiment No: 3 Name of The Exp.: Group Communication Aim: Write A Program To Demonstrate Client - Server Chat

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Experiment No: 3

Name of The Exp.: Group communication


Aim: Write a program to demonstrate Client - Server Chat
application
Theory:
Group Communication
Group communication is a paradigm for multi-party communication that is
based on the notion
of groups as a main abstraction. A group is a set of parties that, presumably,
want to exchange
information in a reliable, consistent manner. For example:
• The participants of a message-based conferencing tool may constitute a
group. Ideally, in
order to have meaningful communication, each participant wants to receive all
communicated messages from each other participant. Moreover, if one
message is a
response to another, the original message should be delivered before the
response. (In this
example, if two participants originate messages independently at about the
same time, the
order in which such independent messages are delivered is not important)
• The set of replicas of a fault-tolerant database server may constitute a group.
Consider
update messages to the server. Since the contents of the database depend on
the history of
all update messages received, all updates must be delivered to all replicas.
Furthermore, all
updates must be delivered in the same order. Otherwise, inconsistencies may
arise.
Group Communication Primitives
Group communication is implemented using middleware that provides two
sets of primitives to the
application:
• Multicast primitive (e.g., post): This primitive allows a sender to post a
message to the
entire group.
• Membership primitives (e.g., join, leave, query_membership): These
primitives allow a
process to join or leave a particular group, as well as to query the group for the
list of all
current participants.
Group Communication Semantics
Different approaches to group communication present different delivery
semantics concerning
the post primitive. Several alternatives are possible:
• Best effort delivery: No guarantees are given on message delivery. No
guarantees are
given on the order of messages delivered to the destination. Obviously, this is
not a useful
abstraction.
• Reliable messages delivery: The communication subsystem ensures the
following three
properties:
o If a correct process broadcasts message m, then all correct processes
eventually
deliver m.
o If a correct process delivers message m, then all correct processes eventually
deliver m. (Note that the difference of this property from the previous one is
that this
property should hold even if the sender was not a correct process. This is
important
to ensure consistency even when the sender crashes in the middle of a
broadcast
after some correct receivers have already delivered the message).
o Every process delivers m at most once, and only if was previously broadcast.
• FIFO message delivery: It's a reliable message delivery that also ensures that
messages
from the same source are delivered in the order they were sent. (If a process
broadcasts
messsage m before broadcasting message m` then no correct process delivers
m` unless it
has previously delivered m). FIFO delivery does not guarantee that messages
from different
sources will be delivered in the same order. For example, if process A responds
to a
message from process B, these messages can be delivered in different orders
to processes C
and D who are listening to the conversation. This is because the messages do
not originate
from the same sender. TCP implements FIFO delivery for the special case of a
group of two
participants. (Note that: a. Every byte gets delivered. b. Bytes from the same
sender are
delivered in order.)
• Causal message delivery: It generalizes FIFO message delivery to a scenario
where
messages are delivered in the order of potential causality. In other words, if
message m` could have been a response to m, all correct processes must
deliver m before m`. Causal message delivery delivers messages in the order
they were sent
according to Lamport's happened before relation. Lamport proposed that in
the absence of
global time in a distributed system event A happens before event B is iff:
o Event A precedes event B on the same processor
o Event A is that of sending message m, and event B is that of receving the
same
message m
o Event A is shown to occur before B by transitivity (e.g., A happens before C
which
happens before B)
Totally ordered message delivery: All messages are delivered in the same order
to all destinations
The steps for creating a simple server program are:
1. Open the Server Socket: ServerSocket server = new ServerSocket( PORT );
2. Wait for the Client Request: Socket client = server.accept();
3. Create I/O streams for communicating to the client DataInputStream is =
new
DataInputStream(client.getInputStream()); DataOutputStream os = new
DataOutputStream(client.getOutputStream());
4. Perform communication with client Receive from client: String line =
is.readLine(); Send
to client: os.writeBytes(“Hello\n”);
5. Close socket: client.close();
The steps for creating a simple client program are:
1. Create a Socket Object: Socket client = new Socket(server, port_id);
2. Create I/O streams for communicating with the server. is = new
DataInputStream(client.getInputStream()); os = new
DataOutputStream(client.getOutputStream());
3. Perform I/O or communication with the server: Receive data from the
server: String line =
is.readLine(); Send data to the server: os.writeBytes(“Hello\n”);
4. Close the socket when done: client.close();
Algorithm :
a. Sending of a message m from master process to slaves:
1. Open the Server Socket (The port that the server listens on) : ServerSocket
server =
new ServerSocket( PORT );
2. Set of all names of clients in the chat room.
3. Set of all the print writers for all the clients.
4. Pawns handler threads
5. Constructs a handler thread, squirreling away the socket.
6. Services this thread's client by repeatedly requesting a screen name until a
unique
one has been submitted, then acknowledges the name and registers the
output
stream for the client in a global set, then repeatedly gets inputs and broadcasts
them.
7. Create character streams for the socket.
8. Request a name from this client. Keep requesting until a name is submitted
that is
not already used.
9. Add the socket's print writer to the set of all writers so this client can receive
broadcast messages.
10. Constructs a handler thread, squirreling away the socket.
11. Services this thread's client by repeatedly requesting a screen name until a
unique
one has been submitted, then acknowledges the name and registers the
output
stream for the client in a global set, then repeatedly gets inputs and broadcasts
them.
12. Create character streams for the socket
13. Request a name from this client. Keep requesting until a name is submitted
that is
not already used.
14. Add the socket's print writer to the set of all writers so this client can
receive
broadcast messages.
15. Accept messages from this client and broadcast them. Ignore other clients
that
cannot be broadcasted to.
b) The client follows the Chat Protocol which is as follows.
1. Constructs the client
2. Listener sends the text field contents to the server.
3. Add Listeners
4. Responds to pressing the enter key in the textfield by sending the contents
of
the text field to the server.
5. Prompt for and return the address of the server.
6. Connects to the server then enters the processing loop.
7. Process all messages from server, according to the protocol.
CODE:

Client Side
import java.io.*;
import java.net.*;
public class GossipClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 3000);

BufferedReader keyRead = new BufferedReader(new


InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();


PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();


BufferedReader receiveRead = new BufferedReader(new
InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;


while(true)
{
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
}
}
}
}

Server Side

import java.io.*;
import java.net.*;
public class GossipServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready for chatting");
Socket sock = sersock.accept( );
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new
InputStreamReader(istream));

String receiveMessage, sendMessage;


while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
}
}
}
Output:

Client

Server

Conclusion: Thus we have successfully completed chat application to


demonstrate group communication. This application consist of a chat server
and a chat client. The server accepts connections from the clients and delivers
all the messages from each client to other client.

You might also like