Can you please tell me what this code does? I cannot run it to see so if you cou
ID: 3915132 • Letter: C
Question
Can you please tell me what this code does? I cannot run it to see so if you could just explain it I would appreciate it.
import java.net.*;
import java.io.*;
public class MessageClient
{
public static final int PORT = 6100;
public static final String host = "127.0.0.1";
public static void main(String[] args) throws IOException {
Socket sock = null;
if (args.length != 1) {
System.err.println("Usage: java MessageClient <message>");
System.exit(0);
}
try {
/* Comment Here */
sock = new Socket(host, PORT);
PrintWriter pout = new PrintWriter(sock.getOutputStream(),true);
pout.println(args[0]);
/* Comment Here */
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
Message message = (Message) ois.readObject();
System.out.println(message.getCharacterCount());
System.out.println(message.getDigitCount());
}
catch (IOException ioe) {
System.err.println(ioe);
}
catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
}
finally {
sock.close();
Explanation / Answer
This code is one part of a client - server program. This is the client part where it expects that the server is running on local machine on port 6100. You should be having a different code for the server program.
import java.net.*;
import java.io.*;
public class MessageClient
{
public static final int PORT = 6100;
public static final String host = "127.0.0.1";
public static void main(String[] args) throws IOException {
Socket sock = null;
if (args.length != 1) {
System.err.println("Usage: java MessageClient <message>");
System.exit(0);
}
try {
/* Create a socket and get the output stream on which the client can send messages*/
sock = new Socket(host, PORT);
PrintWriter pout = new PrintWriter(sock.getOutputStream(),true);
pout.println(args[0]);
/* Get the input stream of the socket from which client will receive messages from server */
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
Message message = (Message) ois.readObject();
System.out.println(message.getCharacterCount());
System.out.println(message.getDigitCount());
}
catch (IOException ioe) {
System.err.println(ioe);
}
catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
}
finally {
sock.close();
==================================
1. In order to run the program, first the server program should be running so that the client can send messages to it.
===========
2. Once you have your server program running, run this code along with a command line argument. The command line argument is the message you want to send to the server.
If running this code from command prompt, then an example of running this code is
> java MessageClient hello
Here 'hello' is the message we are sending to server. It could be any message (depends on what your server expects)
In eclipse, if you need to pass command line argument, click the drop down array on run program button (green arrow), click Run Configurations and add a configuration for this code, click the second tab 'Arguments' and type hello to pass a message 'hello'
If using any other java IDE , check out how to pass command line argument.
If an command line argument is not passed, the program halts with and error message.
============
3. The client receives a command line message and sends it to server; by creating a Socket object and getting the output stream of the socket. Next the client expects a message from the server and in order to get that message, it gets hold of the input stream of the socket and reads the Message object. The number of characters and digits retrieved from the message (using getters) are printed to the console. (Probably the server responds with the no. of characters and digits for the client message sent initially)
Hope it helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.