Question: Below is a program to pass a number to a server, double it and return the number. Instead of passing a number between the client
Below is a program to pass a number to a server, double it and return the number. Instead of passing a number between the client and server, I want to do the following:
What I need help doing is sending a user name to server, server checking to see if valid by reading from separate users.txt file, responding to client with codes listed below, if valid server asks for password and then checks txt file to see if valid. If not valid username or password send appropriate code and message.
Create both classes in same package
Need to first run the server, then run the client
Server will then respond with a code listed below(+, -, #, !)
+ =Success
- =Error
! =Logged in
The server will read from a separate users.txt file (args 0) containing user names and passwords that it will check if valid from clients input:
user1 pass1
MKL foo
user3 pass3
An example of what I want the client server interaction to look like. 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
Import java.util.Scanner;
public class client{
public static void main(String args[]) throws UnknownHostException,
{
int number, temp;
Scanner sc= new Scanner(System.in); //accept input from user
Socket s = new Socket(my ip address, port number);// create a socket, using ip address of local machine b/c run on same machine port#50001
Scanner sc1= new Scanner(s.getInputStream()); //used to get output from server
System.out.println(Enter a number);// accept num from user
number= sc.nextInt(); //pass number
PrintStream p = new PrintStream(s.getOutputStream()); // create printsteam object
p.println(number); // pass number to server through printstream object
temp= sc1.nextInt(); //get number from server and store
System.out.println(temp);
}}
Import java.io.IOException;
Import java.net.ServerSocket;
Import java.net.Socket;
Import java.util.Scanner;
public class server{
public static void main(String args[]) throws IOException{
int number,temp;
ServerSocket s1 = new ServerSocket(port number); // socket for server same port number(50001)
Socket ss = s1.accept(); // create another socket to accept number from client
Scanner sc= new Scanner(ss.getImputStream());// accept number from socket
number= sc.nextInt();// store number
temp=number*2;// double number
PrintStream p = new PrintStream(ss.GetOutputStream());// pass number to client
p.println(temp);// print
}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
