Java Multi-threaded phone server A Client will: Request a connection with the se
ID: 3674097 • Letter: J
Question
Java Multi-threaded phone server
A Client will:
Request a connection with the server
Send three types of messages through established connection:
STORE messages containing name and phone number
GET messages containing requests for phone number
REMOVE messages containing requests to remove a number from the list
Server will:
Accept requested connections and support multiple connections
Maintain an appropriate data structure (a list) that holds data received from a client. The data stored on server is a list of names and phone numbers which is empty at startup. It is not persistent.
Process messages received from the client:
If message is STORE with a pair of name and number, server will have to update the list of phone numbers.
If message is GET with a request (a name as a content), server has to send to the client the phone number of this name stored in the list
If message is REMOVE with a name, server has to remove this name and number associated with this name from the list.
Client request commands and its format:
STORE Name Phone number
GET Name
REMOVE Name Server response code and meaning:
Data message:
200
Phone number Acknowledgement message:
100 OK
Error message:
300 " Not Found"
Message not understood:
400 "Bad request"
The structure of the Phone server is shown below:
import java.io.*;
import java.util.*;
import java.net.*;
public class PhoneServer {
// The port number on which the server will be listening
private static int port = 2014;
// The server socket.
private static ServerSocket listener = null;
// The client socket.
private static Socket clientSocket = null;
public static void main(String[] args)throws Exception
{
/** Open a server socket on the specified port number(2014)
and monitor the port for connection requests. When a
connection request is received, create a client request
thread, passing to its constructor a reference to the
Socket object that represents the established connection
with the client.
*/
}
}
class ClientThread extends Thread {
Socket socket; //constructor
public ClientThread(Socket socket) { this.socket = socket; }
//implement the run method
public void run() { handleConnection(socket);
} //implement the handleConnection method here.
}
Explanation / Answer
import java.io.*;
import java.util.*;
import java.net.*;
public class PhoneServer { // The port number on which the server will be listening
private static int port = 2014; // The server socket.
private static ServerSocket listener = null; // The client socket.
private static Socket clientSocket = null;
public static String word = "You bet";
public static void main(String[] args)throws Exception {
boolean listening = true;
try {
listener = new ServerSocket(port);
while (listening) {
new ClientThread(listener.accept()).start();
}
}
catch (Exception e)
{
System.out.println("I/O failure: " + e.getMessage());
}
listener.close();
/** Open a server socket on the specified port number(2014) and monitor the port for connection requests . When a connection request is received, create a client request thread, passing to its constructor a reference to the Socket object that represents the established connection with the client.
*/
}
}
class Contact
{
private String phoneNumber;
private String name;
public Contact(String n, String num) {
this.name = n;
this.phoneNumber = num;
}
public String getName() {
return this.name;
}
public String getNumber() {
return this.phoneNumber;
}
}
class ClientThread extends Thread {
Socket socket; //constructor
public ClientThread(Socket socket) {
this.socket = socket; //Establishes connection
}
// implement the run method
public void run()
{
handleConnection(socket);
}
//implement the handleConnection method here.
public void handleConnection(Socket socket)
{
List<Contact> contacts = new ArrayList<Contact>();
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String fromClient = null;
Boolean quit = false;
out.println("100 OK");
while (!in.ready()) {
}
while ((fromClient = in.readLine()) != null && !quit) {
System.out.println("Received: '" + fromClient + "' "); // Read one line and output it
String[] commands = fromClient.split(" ");
if(commands[0].equals("quit")) {
quit = true;
} else if(commands[0].equals("STORE")) {
contacts.add(new Contact(commands[1], commands[2]));
out.println("100 OK");
} else if(commands[0].equals("GET")) {
for(Contact c : contacts) {
if(c.getName().equals(commands[1])) {
out.println("200 " + c.getNumber());
break;
}
}
} else if(commands[0].equals("REMOVE")) {
for(Contact c : contacts) {
if(c.getName().equals(commands[1])) {
out.println("100 OK");
contacts.remove(c);
break;
}
}
} else {
out.println("400 "Bad request"");
}
}
in.close();
} catch (IOException e) {
//
}
//contact.add(new Contact(32, "OM"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.