Hi there,
I need help with my assignment it has to do with Client-server programming and the following is what i have been asked to do for this assignment. So far i have the server reading from the file and the client is able to read the contents of the file which server reads. I would like to know how I should to using the hash map with array list as a data structure to read the messages sent from the client and store them with in the data structure of the specific account. I am looking for some help on how I should be integrating the hash map array list in the code. Following is what my assignment is asking for:
In this assignment, you will implement a multi-user messaging system.
• The server must be implemented in Java
• You will provide two functionally equivalent clients implemented in Python and Java.
The client:
The message client is a program used to send and receive messages. The user invokes the client from command line and passes a number of parameters.
The client can:
• create an account on the server.
• send a message to an account on the server.
• check for messages for an account on the server.
The Java client:
You will implement a single Java executable class, jclient, which will be the message client. Its usage is:
Usage:
java jclient <host> <port> create <name>
Instructs the server running on the <host> at port <port> to create an account with the name <name>. If the account already exists, then the server should print an error, otherwise, the server should prints a confirmation.
java jclient <host> <port> send <name>
Instructs the server running on <host> at <port> to send a message to the account <name>. The client reads from standard input the message. Hint: You may test the send feature using redirection from an input file.
Note: the server can save multiple messages to the same account.
java jclient <host> <port> receive <name>
This retrieves all the messages stored on the server for the account <name>. The messages will be purged on the server after they are retrieved once.
The server:
The server is an executable Java class, jserver. jserver accepts network connection at a specific port, and manages messages for different accounts. The server must be multi-threaded to support simultaneous connections from different clients. The usage for the
server is:
java jserver <port>
The server must utilize the following features of Java:
• Sockets for network communication
• Java collection library for managing messages
• Threads for concurrent processing of messages
• Synchronization to handle concurrent access of common data structures
The following code is what i have done so far:
jServer.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
public class jServer {
public static void main(String[] args) {
try {
/*
* Loading the file content into memory
*/
Vector<String> fileContent = new Vector<String>();
BufferedReader fileReader = new BufferedReader(new FileReader("mydatabase"));
String line = fileReader.readLine();
while(line != null) {
fileContent.add(line);
line = fileReader.readLine();
}
fileReader.close();
/*
* Starts a network server
*/
ServerSocket server = new ServerSocket(2000);
Vector<Connection> clientList = new Vector<Connection>();
while(true) {
Socket conn = server.accept();
Connection client = new Connection(conn, fileContent);
clientList.add(client);
client.start();
}
//server.close();
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Connection.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;
public class Connection extends Thread {
private Socket conn;
private Vector<String> fileContent;
public Connection(Socket socket, Vector<String> fileContent) {
this.conn = socket;
this.fileContent = fileContent;
}
public void run() {
System.out.println("New connection established.");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
PrintWriter writer = new PrintWriter(conn.getOutputStream());
Client: while (true) {
if(Thread.interrupted())
break;
String command = reader.readLine();
System.out.println("> " + command);
String[] commands = command.split(" ");
if (commands[0].equals("read")) {
int lineno = Integer.parseInt(commands[1]);
if (lineno < fileContent.size() && lineno >= 0) {
writer.println(fileContent.get(lineno));
writer.flush();
} else {
writer.println("Invalid line number.");
writer.flush();
}
} else if (commands[0].equals("bye")) {
writer.println("Disconnecting...");
writer.flush();
break Client;
} else {
writer.println("Unknown command \"" + commands[0] + "\"");
writer.flush();
}
}
conn.close();
} catch (IOException e) {
System.out.println("IO Exception occurred: " + e.getMessage());
}
System.out.println("Connection closed.");
}
}
jClient.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class jClient {
public static void main(String[] args) {
try {
Socket conn = new Socket(args[0], Integer.parseInt(args[1]));
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
PrintWriter writer = new PrintWriter(conn.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("> ");
String command = input.readLine();
try {
writer.println(command);
writer.flush();
String reply = reader.readLine();
System.out.println("--> " + reply);
} catch(Exception e) {
System.out.println("Connection is lost.");
break;
}
}
conn.close();
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Thanks in Advance.