Java Reference
In-Depth Information
{
socket = server.accept();
PrintWriter output =
new PrintWriter(
socket.getOutputStream(),true);
Date date = new Date();
output.println(date);
//Method toString executed in line above.
socket.close();
}while (true);
}
catch (IOException ioEx)
{
System.out.println(ioEx);
}
}
}
The server simply sends the date and time as a string and then closes the connec-
tion. If we run the client and server in separate command windows and enter local-
host as our host name in the client's GUI, the result should look similar to that
shown in Fig. 2.7. Unfortunately, there is still a potential problem on some systems:
since a low-numbered port (i.e., below 1024) is being used, the user may not have
suffi cient system rights to make use of the port. The solution in such circumstances
is simple: change the port number (in both server and client) to a value above 1024.
(E.g., change the value of DAYTIME_PORT from 13 to 1300.)
Now for an example that checks a range of ports on a specifi ed host and reports
on those ports that are providing a service. This works by the program trying to cre-
ate a socket on each port number in turn. If a socket is created successfully, then
there is an open port; otherwise, an IOException is thrown (and ignored by the
program, which simply provides an empty catch clause). The program creates a
text fi eld for acceptance of the required URL(s) and sets this to an initial default
value. It also provides a text area for the program's output and buttons for checking
the ports and for exiting the program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class PortScanner extends JFrame
implements ActionListener
Search WWH ::




Custom Search