Now that we\'ve got a client set up and a server for it to connect to, we need t
ID: 3669881 • Letter: N
Question
Now that we've got a client set up and a server for it to connect to, we need to send data back and forth. Finish the code for both classes to send numbers between the server and the client. Assume all variables and imports are properly set up. Be sure to finish the code for BOTH classes. If you have trouble, check the Oracle API. public class NewClient {//Variables Created//Main Execution public static void main (String[] args) {//Catch a possible connectivity error try {//Connection set up - establish Input and Output Streams.//Note: This is only one possible way to send data, reader = new DataInputStream (socket.getInputStream ()); Writer = new DataOutputStream (socket.getOutputStream ());//Pseudocode: random number generated.//Send the random number to the server.//"WRITE YOUR CODE BELOW THIS LINE"//Receive the random number from, the server.//""WRITE YOUR CODE BELOW THIS LINE""//Inform, the user of the error System out printn("Error connecting to server.");}}}Explanation / Answer
Here is the changes in your code:
public class NewClient {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
//get the localhost IP address, if server is running on some other IP, you need to use that
InetAddress host = InetAddress.getLocalHost();
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
//establish socket connection to server
socket = new Socket(host.getHostName(), 9876);
//write to socket using ObjectOutputStream
reader = new DataInputStream(socket.getInputStream());
writer = new DataInputStream(socket.getOutputStream());
int clientNumber = x;
int serverNumber;
System.out.println("Sending request to Socket Server");
writer.writeObject(x));
//read the server response message
int serverNumber = (Integer) reader.readObject();
System.out.println("serverNumber: " + serverNumber);
//close resources
reader.close();
writer.close();
}
}
Hi , you can take help from my SocketClientExample calss, given below.... this is another way to write serverClient
public class SocketClientExample {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
//get the localhost IP address, if server is running on some other IP, you need to use that
InetAddress host = InetAddress.getLocalHost();
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
//establish socket connection to server
socket = new Socket(host.getHostName(), 9876);
int clientNumber = 4;
//write to socket using ObjectOutputStream
oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("Sending request to Socket Server");
else oos.writeObject(clientNumber);
//read the server response message
ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message: " + message);
//close resources
ois.close();
oos.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.