Can you please help me figure out these errors, it is a review of final. Can you
ID: 3548282 • Letter: C
Question
Can you please help me figure out these errors, it is a review of final.
Can you please explain any error that there is here even if you think it is simple.
Thanx.
1. Find the errors in the following network client program segment.
// declare the port and host
private Integernet port;
private Host host
/* set the port to 7777 and the host to the instructors computer in btc 204 at hvcc */
port = new port(77777);
host = Internet.get("btc216@hvcc.edu" );
// create a socket object connected to your host and port
Socket client = host;
Socket client = port;
// create a PrintWriter object connected to your socket object
PrintWriter pw = new PrintWriter(Socket. getOutputStream(), flush());
// write a string message over the socket stream
write("Hello World!");
Explanation / Answer
This is how we do socket programming....
I have coded to send a message "Hello World !" to btc216@hvcc.edu host runnning at port 7777.
In your code u have put port = 77777, which is incorrect since only port upto 9999 is used. Please find the code below...
Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class PortTest {
public static void main(String[] args) {
Integer port;
String host;
/* set the port to 7777 and the host to the instructors computer in btc 204 at hvcc */
port = new Integer(7777);
host = "btc216@hvcc.edu";
try {
// create a socket object connected to your host and port
Socket node = new Socket(host, port);
node.getOutputStream().write("Hello World !".getBytes("UTF-8"));
node.getOutputStream().flush();
// create a PrintWriter object connected to your socket object
PrintWriter ps = new PrintWriter(node.getOutputStream(), true);
// write a string message over the socket stream
ps.write("Hello World!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}