StreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); String option; while (true) { System.out.println("waiting..."); option = inFromClient.readLine(); switch (option) { case "0": System.out.println("The option is " + option); String username = inFromClient.readLine(); System.out.println("The username is " + username); String password = inFromClient.readLine(); System.out.println("The password is " + password); outToClient.writeBytes("Access Granted\n"); break; case "1": System.out.println("The option is " + option); outToClient.writeBytes("Alice Bob\n"); System.out.println("Alice Bob"); break; case "2": System.out.println("The option is " + option); String receiver = inFromClient.readLine(); System.out.println("The receiver is " + receiver); String message = inFromClient.readLine(); System.out.println("The message is " + message); break; case "3": System.out.println("The option is " + option); outToClient.writeBytes("No message\n"); break; case "4": connectionSocket.close(); break; } if (option.equals("4")) { break; } } } } }
StreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); String option; while (true) { System.out.println("waiting..."); option = inFromClient.readLine(); switch (option) { case "0": System.out.println("The option is " + option); String username = inFromClient.readLine(); System.out.println("The username is " + username); String password = inFromClient.readLine(); System.out.println("The password is " + password); outToClient.writeBytes("Access Granted\n"); break; case "1": System.out.println("The option is " + option); outToClient.writeBytes("Alice Bob\n"); System.out.println("Alice Bob"); break; case "2": System.out.println("The option is " + option); String receiver = inFromClient.readLine(); System.out.println("The receiver is " + receiver); String message = inFromClient.readLine(); System.out.println("The message is " + message); break; case "3": System.out.println("The option is " + option); outToClient.writeBytes("No message\n"); break; case "4": connectionSocket.close(); break; } if (option.equals("4")) { break; } } } } }
Chapter5: Looping
Section: Chapter Questions
Problem 12E
Related questions
Question
Can someone please explain to me ASAP??
import java.io.*;
import java.net.*;
class SimpleTextClient {
public static void main(String argv[]) throws Exception
{
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("127.0.0.1", 8000);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
while (true) {
System.out.println("\nYou are connected to the Server.");
System.out.println("\n0. Log in to the server\n1. Get the user list\n2. Send a message\n3. Get my messages\n4. Exit");
System.out.print("\nEnter your option: ");
String option = inFromUser.readLine();
outToServer.writeBytes(option + "\n");
switch (option) {
case "0":
System.out.print("Enter your username: ");
String username = inFromUser.readLine();
System.out.print("Enter your password: ");
String password = inFromUser.readLine();
outToServer.writeBytes(username + "\n");
outToServer.writeBytes(password + "\n");
String response = inFromServer.readLine();
System.out.println(response);
break;
case "1":
System.out.println("User List:");
String user = inFromServer.readLine();
System.out.println(user);
break;
case "2":
System.out.print("Enter the receiver's username: ");
String receiver = inFromUser.readLine();
System.out.print("Enter your message: ");
String message = inFromUser.readLine();
outToServer.writeBytes(receiver + "\n");
outToServer.writeBytes(message + "\n");
break;
case "3":
System.out.println("Your messages:");
String answer = inFromServer.readLine();
System.out.println(answer);
break;
case "4":
clientSocket.close();
}
if (option.equals("4")) {
break;
}
}
}
}
import java.io.*;
import java.net.*;
class SimpleTextServer {
public static void main(String argv[]) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(8000);
System.out.println("SERVER is running ... ");
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
String option;
while (true) {
System.out.println("waiting...");
option = inFromClient.readLine();
switch (option) {
case "0":
System.out.println("The option is " + option);
String username = inFromClient.readLine();
System.out.println("The username is " + username);
String password = inFromClient.readLine();
System.out.println("The password is " + password);
outToClient.writeBytes("Access Granted\n");
break;
case "1":
System.out.println("The option is " + option);
outToClient.writeBytes("Alice Bob\n");
System.out.println("Alice Bob");
break;
case "2":
System.out.println("The option is " + option);
String receiver = inFromClient.readLine();
System.out.println("The receiver is " + receiver);
String message = inFromClient.readLine();
System.out.println("The message is " + message);
break;
case "3":
System.out.println("The option is " + option);
outToClient.writeBytes("No message\n");
break;
case "4":
connectionSocket.close();
break;
}
if (option.equals("4")) {
break;
}
}
}
}
}
![2. Instruction
Write a server program and a client program. The server manages a number of
client accounts, each having a username and a password. The server program
starts first. When a client process is started, it repeatedly shows the following
menu to the user and asks the user for an option:
0. Connect to the server
1. Get the user list
2. Send a message
3. Get my messages
4. Exit
If the user chooses Option 0, the client program establishes a TCP connection
with the server. Then, it prompts the user for their username and password to
log in. The client program then sends the credentials to the server to verify. If
what the user entered matches the username and password stored at the server,
grant the user access by printing "Access Granted" at the client. Otherwise, print
"Access Denied - Username/Password Incorrect", and keep asking the user for
the credentials until the user gets them correct to successfully log onto the
server.
If the user chooses Option 1, the client program receives the list of usernames
from the server and print them out to the user.
If the user chooses Option 2, the user can leave text message to another user.
The client program sends this text message to the server and the server will store
it.
If the user chooses Option 3, the user can retrieve their own text messages that
were left by other users at the server. The client program will display those
messages to the user.
If the user chooses Option 4, the client program terminates the TCP connection
with the server and exit.
3. Sample Code
We provide sample code for a simple server program and a client program. The
client establishes a TCP connection with the server, and sends a message to the
server, which will send a capitalized message back to the client for display.
Essentially, it gives you a programming template that you can follow to write
your own networking program.
4. Testing
Use the following scenario for testing. The server maintains two client accounts:
One is for Alice and the password is "1234"; the other is Bob and the
password is "5678". Alice logs onto the server first. She gets the user list from the
server and sees Bob. She sends a text message to Bob and exit from the client
program. Next, Bob logs onto the server. He then retrieves his text messages left
by Alice, and exits from the client program.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F11ad9986-04b0-4336-8340-a3038312afe8%2F29f68ccc-87c3-4de2-a82f-963b409dbe4c%2F4wv2ira_processed.jpeg&w=3840&q=75)
Transcribed Image Text:2. Instruction
Write a server program and a client program. The server manages a number of
client accounts, each having a username and a password. The server program
starts first. When a client process is started, it repeatedly shows the following
menu to the user and asks the user for an option:
0. Connect to the server
1. Get the user list
2. Send a message
3. Get my messages
4. Exit
If the user chooses Option 0, the client program establishes a TCP connection
with the server. Then, it prompts the user for their username and password to
log in. The client program then sends the credentials to the server to verify. If
what the user entered matches the username and password stored at the server,
grant the user access by printing "Access Granted" at the client. Otherwise, print
"Access Denied - Username/Password Incorrect", and keep asking the user for
the credentials until the user gets them correct to successfully log onto the
server.
If the user chooses Option 1, the client program receives the list of usernames
from the server and print them out to the user.
If the user chooses Option 2, the user can leave text message to another user.
The client program sends this text message to the server and the server will store
it.
If the user chooses Option 3, the user can retrieve their own text messages that
were left by other users at the server. The client program will display those
messages to the user.
If the user chooses Option 4, the client program terminates the TCP connection
with the server and exit.
3. Sample Code
We provide sample code for a simple server program and a client program. The
client establishes a TCP connection with the server, and sends a message to the
server, which will send a capitalized message back to the client for display.
Essentially, it gives you a programming template that you can follow to write
your own networking program.
4. Testing
Use the following scenario for testing. The server maintains two client accounts:
One is for Alice and the password is "1234"; the other is Bob and the
password is "5678". Alice logs onto the server first. She gets the user list from the
server and sees Bob. She sends a text message to Bob and exit from the client
program. Next, Bob logs onto the server. He then retrieves his text messages left
by Alice, and exits from the client program.
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![Microsoft Visual C#](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![Microsoft Visual C#](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT