Professional Documents
Culture Documents
Distributed System GROUP 7
Distributed System GROUP 7
GROUP ASSIGNMENT
Submission Date:-31/5/2021
1. Write a Program to Send and Receive a Message using
Connectionless Sockets
package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;
2
//write object to Socket
oos.writeObject("Hi Client "+message);
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the Server Socket object
server.close();
}
package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
3
socket = new Socket(host.getHostName(), 9876);
//write to socket using ObjectOutputStream
oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("Sending request to Socket Server");
if(i==4)oos.writeObject("exit");
else oos.writeObject(""+i);
//read the server response message
ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message: " + message);
//close resources
ois.close();
oos.close();
Thread.sleep(100);
}
}
}
4
5
6
3. How to write a Program for the Connection Server Program to
respond to a Connecting Client
Connection-Oriented – One connection is established between client and server –
Data flows in the communication channel in a continuous stream – Packets arrive
in order – Low probability of packet loss – TCP (Transmission Control Protocol) is
used for connection establishment and data transfer – In Java, this type of
communication is programmed using “Sockets”
• Client uses server address and port number to get connected to server service
7
Output Stream : to write data to destination
• Communication is done between input stream of one socket and the output
stream of the other socket
2) Establish output stream to server //This statement get the output stream to
“clientSocket” at server side which help clients to send data to server PrintWriter
out = new PrintWriter(serverSocket.getOutputStream(), true);
3) Establish input stream to Server //This statement get the input stream to
“clientSocket” at server side which help client to receive data from server
BufferedReader input = new BufferedReader(new
InputStreamReader(serverSocket.getInputStream()));
After Server gets Client Socket and Client gets his own Socket, communication
starts between the two sockets using the input/output streams of sockets.
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
8
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
public static final int port = 8081;
private static class ClientHandler implements Runnable {
private final Socket socket;
// All writes are performed while synchronized on 'os'.
private final PrintWriter os;
// Socket reads do not need to be synchronized as each clients gets its
// own thread.
private final BufferedReader is;
private final Map<String, ClientHandler> clients;
private String clientId;
public ClientHandler(Socket socket, Map<String, ClientHandler> clients)
throws IOException {
this.socket = socket;
this.clients = clients;
this.os = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()));
this.is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
}
@Override
public void run() {
try {
// First line the client sends us is the client ID.
clientId = is.readLine();
clients.put(clientId, this);
for (String line = is.readLine(); line != null; line = is.readLine()) {
int separatorIndex = line.indexOf(':');
if (separatorIndex <= 0) {
9
continue;
}
String toClient = line.substring(0, separatorIndex);
String message = line.substring(separatorIndex + 1);
ClientHandler client = clients.get(toClient);
if (client != null) {
client.sendMessage(clientId, message);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Client " + clientId + " terminated.");
clients.remove(clientId);
try {
socket.close();
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
}
}
public void sendMessage(String from, String message) {
try {
synchronized (os) {
os.println(from + ":" + message);
os.flush();
}
} catch (Exception e) {
// We shutdown this client on any exception.
e.printStackTrace();
try {
socket.close();
10
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException {
final Map<String, ClientHandler> clients = new ConcurrentHashMap<String, Clie
ntHandler>();
ServerSocket ss = new ServerSocket(port);
for (Socket socket = ss.accept(); socket != null; socket = ss.accept()) {
Runnable handler = new ClientHandler(socket, clients);
new Thread(handler).start();
}
}
}
11
REFERENCE
[2] TCP/IP Illustrated, volumes 1-3 by W. Richard Stevens and Gary R. Wright.
[8]http://en.wikipedia.org/wiki/Berkeley_sockets.
12