What are the five errors that make this program malfunction? import java.io.*; i
ID: 3685223 • Letter: W
Question
What are the five errors that make this program malfunction?
import java.io.*;
import java.net.*;
public class NewClient
{
static Socket s;
static DataInputStream reader;
static DataOutputStream writer;
static String userN = “user”;
static String userP = “password”;
static int permissions;
static final String IP =
“Localhost”;
static final int PORT = 9999;
public static void main
(String[] args)
{
try
{
s = new Socket(IP,PORT);
reader = new
DataInputStream
(s.getInputStream());
writer = new
DataOutputStream
(s.getOutputStream());
permissions = getLogin();
}
catch(Exception ex){}
}
public int getLogin()
{
writer.writeUTF(userN);
writer.writeUTF(userP);
int perm = reader.read();
return perm;
}
}
import java.io.*;
import java.net.*;
public class NewServer
{
static ServerSocket s;
static Socket cl;
static DataInputStream reader;
static DataOutputStream writer;
static String userN;
static String userP;
static final int P_FULL = 1054;
static final int P_GUEST = 1051;
static final int PORT = 9876;
public static void main
(String[] args)
{
try
{
s = new ServerSocket
(PORT);
cl = s.accept();
reader = new
DataInputStream
(cl.getInputStream());
writer = new
DataOutputStream
(cl.getOutputStream());
readUser();
}
catch (Exception ex){}
}
public void readUser()
{
userN = reader.readLine();
userP = reader.readLine();
int perm;
if (userN.equals(“user”))
perm = P_FULL;
if (userN.equals(“guest”))
perm = P_GUEST;
writer.writeInt(perm);
}
}
Explanation / Answer
Here the errors are listed as comments:
import java.io.*;
import java.net.*;
public class NewServer
{
static ServerSocket s;
static Socket cl;
static DataInputStream reader;
static DataOutputStream writer;
static String userN;
static String userP;
static final int P_FULL = 1054;
static final int P_GUEST = 1051;
static final int PORT = 9876;
public static void main
(String[] args)
{
try
{
s = new ServerSocket
(PORT);
cl = s.accept();
reader = new
DataInputStream
(cl.getInputStream());
writer = new
DataOutputStream
(cl.getOutputStream());
readUser();
}
catch (Exception ex){}
}
public static void readUser() throws IOException
//Non-static method. Every method which is being called without objects, should be declared static.
//When using the objects of IO classes, like DataInputStream, DataOutputStream, there is a
//chance for exception to occur. So, exceptions should be handled.
{
userN = reader.readLine();
userP = reader.readLine();
int perm = 0;
//perm is initialized inside 2 different conditions, so if either of the condition meets,
//the perm variable will be left uninitialized and you are trying to write the same using write.
if (userN.equals("user"))
perm = P_FULL;
if (userN.equals("guest"))
perm = P_GUEST;
writer.writeInt(perm);
}
}
import java.io.*;
import java.net.*;
public class NewClient
{
static Socket s;
static DataInputStream reader;
static DataOutputStream writer;
static String userN = "user";
static String userP = "password";
static int permissions;
static final String IP =
"Localhost";
static final int PORT = 9999;
public static void main (String[] args)
{
try
{
s = new Socket(IP,PORT);
reader = new
DataInputStream
(s.getInputStream());
writer = new
DataOutputStream
(s.getOutputStream());
permissions = getLogin();
}
catch(Exception ex){}
}
public static int getLogin() throws IOException
//Non-static method. Every method which is being called without objects, should be declared static.
//When using the objects of IO classes, like DataInputStream, DataOutputStream, there is a
//chance for exception to occur. So, exceptions should be handled.
{
writer.writeUTF(userN);
writer.writeUTF(userP);
int perm = reader.read();
return perm;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.