Write a Java Program. Add chat functionality. -The concurrent directory server p
ID: 3864840 • Letter: W
Question
Write a Java Program. Add chat functionality.
-The concurrent directory server program runs waiting for a connection from a client.
-The clients and the server should (in general) run on different machines.
-Each client can send three different messages to the directory server -- JOIN, LEAVE and LIST. The first two are for joining and leaving the list of peers who are online and are willing to chat. The last is to retrieve the list of online peers. The server responds to these messages with the appropriate actions. Note that the server needs to be concurrent because multiple clients should be able to talk to it simultaneously.
-The LIST message should return all the information (IP address, listening port number of the peer etc) so that a peer can be contacted directly.
-A client should be able to invite the peer for a chat by opening a TCP connection directly.
-You can design the protocol for the chat. A peer should be able to initiate a chat connection, exchange chat messages and either peer should be able to close the connection. The peers need not be concurrent.
-This is meant to be a skeletal program, and you probably will need to impose some limitations; that is fine, just state them clearly.
-Close sockets when the program quits
-you should use the socket number 22316
- I have done most of the code, I just need help implementing the chat function. Also My program was having a problem when a client tries to LEAVE, I think I may have the close socket part in the wrong part.
thank you
The Code, GameServer.java
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.PrintStream;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class GameServer {
//Port number assigned for connection, 22316 for me
int portNum;
//Server socket object to open socket for server class
private static ServerSocket serverSocket = null;
//Variable to make server on/Off
boolean listening;
// This server can accept up to maxClientsCount clients' connections.
private static final int maxClientsCount = 10;
private static final ClientHandler[] threads = new ClientHandler[maxClientsCount];
//Concurrent Map to hold client address and the respective socket
private static ConcurrentMap<InetAddress,Socket> clients = null;
// add a concurrent map data structure to store <hostname, socket for that name>,whenever a client is added , it is assigned
// a thread and a hashmap location
//thread leaves, thread set to null , socket closes
//Constructor to initialize Concurrent server class
public GameServer(int port, int maxThreads) {
portNum = port;
listening = true;
try
{
serverSocket = new ServerSocket(portNum);
} catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(-1);
}
clients = new ConcurrentHashMap<InetAddress, Socket>();
for (int i = 0; i < maxClientsCount; i++)
threads[i] = null;
}
public void runServer() {
while (listening) {
try {
//SOCKET FROM CLIENT
Socket clientSocket = serverSocket.accept();
int i = 0;
for (i = 0; i < maxClientsCount; i++) {
if (threads[i] == null) {
// New thread, for client
ClientHandler newThread = new ClientHandler(clientSocket, clients);
//separate thread for new connections
threads[i] = newThread;
threads[i].setThread(newThread);
threads[i].start();
break;
}
}
// If too many clients connected >10
if (i == maxClientsCount) {
PrintStream os = new PrintStream(clientSocket.getOutputStream());
os.println("Server too busy. Try later.");
os.close();
clientSocket.close();
}
} catch (IOException e)
{
System.out.println(e);
}
}
}
}
class ClientHandler extends Thread {
PrintWriter out = null;
BufferedReader in = null;
//CLIENT SOCKET
private Socket clientSocket = null;
//Thread pointing to itself
private ClientHandler thread;
//Concurrent Hash map of connected clients
private ConcurrentMap<InetAddress,Socket> connectedClients = null;
public ClientHandler(Socket _clientSocket, ConcurrentMap<InetAddress,Socket> clients) {
this.clientSocket = _clientSocket;
this.connectedClients = clients;
}
// Make a thread
public void setThread(ClientHandler _thread){
this.thread = _thread;
System.out.println("Thread created for client");
}
public void run() {
try{
// Input steam, output stream for client
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(true){
String input = in.readLine();
if(input != null) {
System.out.println("Received " + input + " from "+clientSocket.getInetAddress().toString().substring(1));
if (input.matches("JOIN")) {
//IF JOIN from client
connectedClients.put(clientSocket.getInetAddress(), clientSocket);
if (connectedClients.containsKey(clientSocket.getInetAddress()))
out.println("Client Successfully connected to server");
else
out.println("Unable to connect client to server due to unknown reasons");
} else if (input.matches("LEAVE")) {
//IF LEAVE from client
//remove client, null client, close socket
if (connectedClients.containsKey(clientSocket.getInetAddress())) {
connectedClients.remove(clientSocket.getInetAddress());
try {
this.out.close();
this.in.close();
clientSocket.close();
this.thread = null;
}
catch (IOException ex) {
System.out.println("Error closing socket");
}
} else {
out.println("You have not joined the list");
}
} else if (input.matches("LIST")) {
// IF LIST from client
if (connectedClients.containsKey(clientSocket.getInetAddress())) {
StringBuilder response = new StringBuilder("Clients Connected: ");
int num = 1;
//append IP address of clients to this string
for (InetAddress address : connectedClients.keySet()) {
response.append(" "+num+". "+address.toString().substring(1));
num++;
}
out.println(response.toString());
} else
out.println("You have not joined the list. Cannot display list.");
} else {
String usage = " Illegal Input. " +
" Usage as follows: " +
" JOIN : Join the server " +
" LIST : List the connected clients " +
" LEAVE : Leave the server";
out.println(usage);
}
}
}
}
catch(IOException e){
System.out.println(e.getMessage());
System.exit(-1);}
}
public static void main(String[] args) throws Exception {
System.out.println("waiting for connections");
}
}
Explanation / Answer
Client side code