Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java help: Write a client program in Java which can interact with the server created below (see code) via the Internet (*), using the application-layer

Java help: image text in transcribed Write a client program in Java which can interact with the server created below (see code) via the Internet (*), using the application-layer protocol above. The client should connect to the server and ask the user to enter user commands of the following form, using the keyboard (console): SUBMIT token, RETRIEVE and QUIT. The user can successively enter as many commands as s/he wants (in a loop), until QUIT is entered by the user, which ends the connection to the server (i.e., QUIT is sent to the server) and ends the client program. Each SUBMIT and RETRIEVE command should be immediately forwarded to the server and the client should then print the response received from the server. Dont forget to test error conditions (e.g., by submitting eleven different tokens to the server). Also, check that a single running server can deal with multiple clients (i.e., two or more running instances of your client program) simultaneously (see code below). Your client should print a log of all its interactions with the server (see example console output below). (*) Server and client(s) need to interact via TCP/IP, but for simplicity and security reasons, clients and the server should actually run on the same machine (localhost). The IP-address used to connect to the server should be 127.0.0.1. Use a port number of your own choice.

MultiThreadedServer.java

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.TreeSet; /*** * Multi Threaded server maintains a list of unique tokens and gives facilities to client to add the tokens * @author Your name * */ public class MultiThreadedServer { static TreeSet lst = new TreeSet(); //shared list of tokens public static void main(String[] args) { ServerSocket server = null; try { // server is listening on port 9999 server = new ServerSocket(9999); server.setReuseAddress(true); while (true) { // socket object to receive incoming client // requests Socket client = server.accept(); //start listening // create a new thread object Handler clientSock = new Handler(client); // This thread will handle the client new Thread(clientSock).start(); } } catch(Exception e) { e.printStackTrace(); } finally { if (server != null) { try { server.close(); } catch (Exception e) { e.printStackTrace(); } } } } /*** * Individual Request handler thread * @author Your name * */ static class Handler extends Thread{ private Socket socket; public Handler(Socket socket){ this.socket=socket; } public void run() { try { //create input and output streams DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); while(true) {//process individual commands until client does not wants to exit String command = dis.readUTF().trim(); if(command.equals("RETRIEVE")) { //if client wants to retrieve the list if(isEmpty()) dos.writeUTF("ERROR"); else dos.writeUTF(lst+""); } if(command.equals("QUIT")) //if clinet wants to quit break; if(command.startsWith("SUBMIT")) { //append query String token[] = command.split(" "); if(contains(token[1]) || size()>=10) dos.writeUTF("ERROR"); else { add(token[1]); dos.writeUTF("OK"); } } } } catch (IOException e) { e.printStackTrace(); } } } //all the method handles concurrent modification on the shared list static synchronized void add(String s) { lst.add(s); } static synchronized void remove(String s) { lst.remove(s); } static synchronized boolean isEmpty() { return lst.isEmpty(); } static synchronized boolean contains(String s) { return lst.contains(s); } static synchronized int size() { return lst.size(); } } 

ClientCode.java

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; /*** * Client class to connect to server running on 9999 * @author Your name * */ public class ClientCode { public static void main(String[] args) throws UnknownHostException, IOException { Socket s = new Socket("localhost",9999); //connect to server System.out.println("Client: connected to server"); DataInputStream dis = null; DataOutputStream dos = null; //create streams dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); Scanner sc = new Scanner(System.in); String command; //read each command and send it to server until user wants to exit do { command = sc.nextLine(); command = command.toUpperCase(); dos.writeUTF(command); if(command.equals("QUIT")) break; System.out.println("Client: sent '"+command+"' to server"); String ans = dis.readUTF(); System.out.println("Client: recieved message from server '"+ans+"'"); }while(true); s.close(); sc.close(); } }

image text in transcribed

image text in transcribed

You are given the following application-layer networking protocol for a simple client/server application. A client can send tokens (words) to the server which adds them to a global tokens list in memory which has a maximum capacity of ten different tokens (there are no duplicates in the list). The tokens list on the server should always be sorted in lexicographical order. There are just three types of requests (SUBMIT, RETRIEVE and QUIT). The protocol in detail: Client request (a message send from client to server): SUBMIT token Server: If the global tokens list already contains the submitted token, don't modify the list, just send message OK to the client. If the token is not yet in the list and the list is not yet full, add the token to the list and send message OK to the client. Otherwise (list is full and token is not yet in the list), don't add anything to the list and respond to the client with message ERROR token stands for any string which doesn't contain whitespace (class Scanner might be useful here). The global tokens list should be kept on the server such that it is sorted in lexicographical order at all times. Choose an appropriate data structure for representing the tokens list. Client request: RETRIEVE Server: Reply with the sorted global list of tokens currently on the server (with the tokens separated by whitespace). If the global tokens list is currently empty, reply with ERROR. Client request: QUIT Server: Ends the connection to that client. No reply. Example interaction: Client: SUBMIT Uvw Server: OK Client: SUBMIT XYZ Server: OK Client: SUBMIT uvw Server: OK Client: SUBMIT ab Server: OK Client: RETRIEVE Server: ab uvw xyz Client: QUIT Sample server console output: Server: waiting for a client to connect Server: waiting for a client to connect Server: clienti connected Server: waiting for a client to connect Server: client2 connected Server: received message from client1 'SUBMIT 200' Server: sent response 'OK' to client1 Server: received message from client1 'SUBMIT dog' Server: sent response 'OK' to client1 Server: received message from client1 'RETRIEVE' Server: sent response 'dog zoo' to clienti Server: received message from client1 'SUBMIT giraffe' Server: sent response 'OK' to clienti Server: received message from client2 'RETRIEVE' Server: sent response 'dog giraffe zoo' to client2 Server: received message from client2 "SUBMIT elephant' Server: sent response 'OK' to client2 Server: received message from client2'SUBMIT rhino' Server: sent response 'OK' to client2 Server: received message from client2 "SUBMIT lion' Server: sent response 'OK' to client2 Server: waiting for a client to connect Server: client3 connected Server: received message from client3 'RETRIEVE' Server: sent response 'dog elephant giraffe lion rhino zoo' to client3 Server: received message from client3 'SUBMIT turkey' Server: sent response 'OK' to client3 Server: received message from clienti 'SBUMIT' Server: sent response 'ERROR - unknown command' to client1 Server: received message from client1 'SUBMIT donkey' Server: sent response 'OK' to clienti Server: received message from client2'SUBMIT leopard' Server: sent response 'OK' to client2 Server: received message from client2 'RETRIEVE' Server: sent response 'dog donkey elephant giraffe leopard lion rhino turkey zoo' to client2 Server: received message from client2 'SUBMIT leopard' Server: sent response 'OK' to client2 Server: received message from client2 'RETRIEVE' Server: sent response 'dog donkey elephant giraffe leopard lion rhino turkey zoo' to client2 Server: received message from client2'SUBMIT hyena' Server: sent response 'OK' to client2 Server: received message from client3 SUBMIT pheasant' Server: sent response 'ERROR - maximum number of tokens reached' to client3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

2. Record these without judgments, criticisms, or comments.

Answered: 1 week ago