Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Multi-threaded phone server A Client will: Request a connection with the se

ID: 3672266 • 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

Comments added


import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author prmsh
*/
public class ClientThread extends Thread {

Socket socket; //constructor

public ClientThread(Socket socket) {
this.socket = socket;
}
//implement the run method

public void run() {
try {
//caling method to handle conenction
handleConnection(socket);
} catch (IOException ex) {
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
}
} //implement the handleConnection method here.

private void handleConnection(Socket clientSocket) throws IOException {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
long time = System.currentTimeMillis();
//outputting file
output.write(("client Accepted").getBytes());
output.close();
input.close();
System.out.println("Coonection accepted:");
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

PhoneServer.java

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 {
//creating new server socket
listener = new ServerSocket(port);
try{
System.out.println("Server accepted");
//accepting server socket
Socket clientSocket = listener.accept();
//calling client
new Thread(new ClientThread(clientSocket)).run();
} catch (IOException e) {
System.out.println("Accept failed: 2014");
System.exit(-1);
}
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote