Homework problem, not exactly sure what to write in the server side of the code
ID: 3679711 • Letter: H
Question
Homework problem, not exactly sure what to write in the server side of the code for the receiveData method. Any help would be great. Thanks!
Examine the following two classes. Complete the server’s receiveData() method. Do not change anything in the client class. Assume all imports and variables are properly set up.
public class NewClient
{
Socket s;
DataInputStream reader;
DataOutputStream writer;
String userN;
String userP;
int health;
float exp;
boolean sent;
public static void main
(String[] args)
{
try{ sendData(); }
catch(Exception ex){}
}
sendData()
{
//Send username
writer.writeUTF(userN);
//Send password
writer.writeUTF(userP);
//Send health
writer.writeInt(health);
//Send experience
writer.writeFloat(exp);
//Confirm data sent
sent =
reader.readBoolean();
}
}
public class NewServer
{
ServerSocket s;
DataInputStream reader;
DataOutputStream writer;
String userN;
String userP;
int health;
float exp;
public static void main
(String[] args)
{
try { receiveData(); }
catch (Exception ex){}
}
receiveData()
{
//Write your code here!
}
}
public class NewClient
{
Socket s;
DataInputStream reader;
DataOutputStream writer;
String userN;
String userP;
int health;
float exp;
boolean sent;
public static void main
(String[] args)
{
try{ sendData(); }
catch(Exception ex){}
}
sendData()
{
//Send username
writer.writeUTF(userN);
//Send password
writer.writeUTF(userP);
//Send health
writer.writeInt(health);
//Send experience
writer.writeFloat(exp);
//Confirm data sent
sent =
reader.readBoolean();
}
}
public class NewServer
{
ServerSocket s;
DataInputStream reader;
DataOutputStream writer;
String userN;
String userP;
int health;
float exp;
public static void main
(String[] args)
{
try { receiveData(); }
catch (Exception ex){}
}
receiveData()
{
//Write your code here!
}
}
Explanation / Answer
Below is your desired method code implmentation.
receiveData(){
//Receive username
String userN = reader.readUTF();
//Receive password
String userP = reader.readUTF();
//Receive health
String health = reader.readInt();
//Receive experience
String exp = reader.readFloat();
// data sent
writer.writeBoolean(true);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.