Question: The Assignment 1 You will write two programs, a server and a client. The server creates a socket in the Internet domain bound to port
The Assignment
You will write two programs, a server and a client. The server creates a socket in the Internet domain bound to port SERVERPORT a constant you should define in both programs, you may use last digits of your UMID last digits are The server receives requests through this socket, acts on those
requests, and returns the results to the requester. The client will also create a socket in the Internet domain, send requests to the SERVERPORT of a computer specified on the commandline, and receive responses through this socket from a server.
For this assignment, you just need to allow one active client connect to the server. In the next assignment, you should allow multiple clients connect the server at the same time.
Your client and server should operate as follows. Your server begins execution by opening a file that you have created and that contains the address book. Each record in the address book will have four fields, Record ID digits First Name up to characters Last Name up to characters Phone Number characters Your server should read these records into a serverinternal data structure and keep track of the number of records. You may assume that you will
never have to store more than some fixed number, say records. Once the server has initialized
its data structures, it should wait for the connection requests from the clients.
Your client operates by sending an ADD, DELETE, LIST, SHUDOWN, QUIT commands to the
server. You should create a client that is able to send any of the five commands above, and allows a user to specify which of the commands the client should send to the server.
The details of the protocol depend on the command the client sends to the server.
ADD
Add a name and number record to the address book. A client sends the ASCII string ADD
followed by a space, followed by a First Name, followed by a space, followed by a Last Name,
followed by a space, followed by a Phone Number, and followed by the newline character ie
n
When the server receives an ADD command from a client, it will add a new record in the address
book and return the OK message.
A clientserver interaction with the ADD command thus looks like:
c: ADD Jinhua Guo
s: OK
The new Record ID is
DELETE
Delete a record from the address book. A client that sends the ASCII string "DELETE", followed
by a space, followed by a Record ID followed by the newline character ie
The client
should then wait for the server to return a OK message indicating that the record is
successfully deleted or a The Record ID does not exist."
A clientserver interaction with the DELETE command thus looks like:
c: DELETE
s: OK
LIST
List all records in the address book.
A clientserver interaction with the LIST command looks like:
c: LIST
s: OK
The list of records in the book:
Jinhua Guo
John Smith
Mary Miller
SHUTDOWN
The SHUTDOWN command, which is sent from the client to the server, is a single line message
that allows a user to shutdown the server. A user that wants to shutdown the server should send
the ASCII string "SHUTDOWN" followed by the newline character ie
Upon receiving the SHUTDOWN command, the server should return the string OK
terminated with a newline close all open sockets and files, and then terminate.
A clientserver interaction with the SHUTDOWN command looks like:
c: SHUTDOWN
s: OK
QUIT
Terminate only the client. The client exits when it receives the confirmation message from the
server.
A clientserver interaction with the QUIT command looks like:
c: QUIT
s: OK
Note, invalid command or message format error should be returned to the client, if a server receives an invalid command or the command in the wrong format.
Server.java
import java.io;
import java.net.;
public class Server
public static final int SERVERPORT ;
public static void mainString args
ServerSocket myServerice null;
String line;
BufferedReader is;
PrintStream os;
Socket serviceSocket null;
Try to open a server socket
try
myServerice new ServerSocketSERVERPORT;
catch IOException e
System.out.printlne;
Create a socket object from the ServerSocket to listen and accept
connections.
Open input and output streams
while true
try
serviceSocket myServerice.accept;
is new BufferedReader new
InputStreamReaderserviceSocketgetInputStream;
os new PrintStreamserviceSocketgetOutputStream;
As long as we receive data, echo that data back to the client.
while line isreadLine null
System.out.printlnline;
osprintlnline;
close input and output stream and socket
isclose;
osclose;
serviceSocket.close;
catch IOException e
System.out.printlne;
make necessary changes in this code as per the conditions mentioned in the question.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
