|
Network Programming ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Java Network Programming Basics - Classes and ObjectsOverview: We use this tutorial to learn more about basic concepts on Java network programming. ![]() Activity 1
import java.net.*; import java.io.*; public class ScanPorts { public static void main(String[] args) { Socket theSocket; String host = "localhost"; if (args.length > 0) { host = args[0]; } for (int i = 0; i < 1024; i++) { try { theSocket = new Socket(host, i); System.out.println("There is a server on port " + i + " of " + host); } catch (UnknownHostException e) { System.err.println(e); break; } catch (IOException e) { System.out.println("*** There is no server on port " + i + " of " + host); } } } } (Q1): What is the function of the import directive in the first two lines?
(Q2): What is the class name of the Java code?
(Q3): List all the functions (or methods) within the class.
(Q4): List all the objects used by the function main.
(Q5): What is the function of new operator in Java language?
(Q6): Guess the program flow within the try-catch block.
Activity 2
public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}
(Q9) What are the class variables?
(Q10) What are the instance variables?
(Q11) What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
System.in - The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.
System.out - The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data) Activity 3
Write a class named IdentifyMyParts and another class named ActivityTwoTest.
(Q12) Send your Java program sources to Steven.
Activity 4
Submission
|