Question: There are two files below: Server.java and client.java The programs are trying to simulate a client and server interaction. To test interaction I will have

There are two files below: Server.java and client.java

The programs are trying to simulate a client and server interaction.

To test interaction I will have a a separate users. txt file args[0] and a folder containing a few small files args[1].

The connection opens by running server.java first then client.java.

Server will ask for a username then password from the client. Client responds and if username and passwords are in the users.txt file args[0] of server, then client will be successfully logged in.

***What I need help with:

Once client is logged in

***2) The client will then enter the command LIST

The server returns the contents of the directory args[1].

Need help in showing the contents of the folder .

I need help in achiving the command LIST. In the server.java program how do I list the contents of the folder called after the client requests it .? ???

Server responds with codes listed below(+, -, #, !)

+ =Success

- =Error

! =Logged in

Sample users.txt file:

user1 pass1

MKL foo

user3 pass3

An example of what the client server interaction looks like now. Order of operations when running program:

S: (listening for connection)

C: (opens connection)

S: Hello from Server

C: User MKL

S: +MKL valid, send password

C: PASS foo

S: ! logged in

***

C: LIST

S: +ServerFolder

Small.File

Large.File

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Server {

public static void main(String args[]) throws IOException {

String username[] = new String[50];

String password[] = new String[50];

int count = 0;

if (args.length < 1) {

System.out.println("Usage java Server users.txt");

System.exit(0);

}

Scanner filereader = new Scanner(new File(args[0]));

while (filereader.hasNext()) {

String creds[] = filereader.nextLine().split(" ");

username[count] = creds[0].trim();

password[count] = creds[creds.length - 1].trim();

count++;

}

int port_number = 50001;

ServerSocket s1 = new ServerSocket(port_number); // socket for server same port number(50001)

System.out.println("S: (listening for connection)");

Socket ss = s1.accept(); // create another socket to accept number from client

System.out.println("S: Hello from Server");

DataInputStream in = new DataInputStream(ss.getInputStream());

DataOutputStream out = new DataOutputStream(ss.getOutputStream());

String uname = null;

while (true) {

uname = in.readUTF().trim();

if (checkUserExist(username, uname, count)) {

out.writeUTF("+");

System.out.println(String.format("S: +%s valid, send password", uname));

break;

} else {

out.writeUTF("-");

System.out.println(String.format("S: -%s Invalid , send again ", uname));

}

}

String pass = null;

while (true) {

pass = in.readUTF().trim();

if (checkPassword(username, password, uname, pass, count)) {

System.out.println("S: ! logged in");

out.writeUTF("!");

break;

} else {

out.writeUTF("-");

System.out.println("S: - Invalid Password");

}

}

s1.close();

filereader.close();

}

/**

* Function for checking if user exists

*

* @param uname

* @param u

* @param count

* @return

*/

public static boolean checkUserExist(String uname[], String u, int count) {

for (int i = 0; i < count; i++) {

if (uname[i].equals(u))

return true;

}

return false;

}

/**

* function for checking user password

*

* @param u

* @param p

* @param user

* @param pass

* @param count

* @return

*/

public static boolean checkPassword(String u[], String[] p, String user, String pass, int count) {

for (int i = 0; i < count; i++) {

if (u[i].equals(user)) {

if (p[i].equals(pass))

return true;

else

return false;

}

}

return false;

}

}

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Client {

public static void main(String args[]) throws UnknownHostException, IOException

{

int port_number = 50001;

String host = "localhost";

Scanner sc = new Scanner(System.in); // accept input from user

Socket s = new Socket(host, port_number);// create a socket, using ip address of local machine b/c run on same

// machine

System.out.println("C: (opens connection)");

DataOutputStream out = new DataOutputStream(s.getOutputStream());

DataInputStream in = new DataInputStream(s.getInputStream());

while (true) {

System.out.print("C: User ");

String uname = sc.next();

out.writeUTF(uname);

if (in.readUTF().trim().equals("+")) {

break;

}

}

while (true) {

System.out.print("C: Pass ");

String pass = sc.next();

out.writeUTF(pass);

if (in.readUTF().trim().equals("!"))

break;

}

s.close();

sc.close();

}

}

//users.txt user1 pass1 MKL foo user3 pass3

//Run instruction

compile both program

javac Server.java

java Client.java

open another terminal/cmd

run server first

java Server.java users.txt

in another terminal

java Client.java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!