home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / internet / reportserversocket.java < prev    next >
Encoding:
Java Source  |  1996-07-04  |  2.5 KB  |  84 lines

  1.  
  2. /* ReportServerSocket.java by Mark D. LaDue */
  3.  
  4. /* March 2, 1996 */
  5.  
  6. /*  Copyright (c) 1996 Mark D. LaDue
  7.     You may study, use, modify, and distribute this example for any purpose.
  8.     This example is provided WITHOUT WARRANTY either expressed or implied.  */
  9.  
  10. /*  This Java Application sets up a simple ServerSocket to receive
  11.      data from the Java applet DoMyWork.java */
  12.  
  13. import java.applet.Applet;
  14. import java.awt.*;
  15. import java.io.*;
  16. import java.net.*;
  17.  
  18. class ReportServerSocket{
  19.  
  20.     public static void main(String args[]) {
  21.  
  22.         ServerSocket server;
  23.         Socket socker;
  24.         InputStream innerStream;
  25. //      OutputStream outerStream;
  26.         String home = new String("www.math.gatech.edu");
  27.         int port = 9000;
  28.         byte by[] = new byte[4096];
  29.         int numberbytes;
  30.         String reply;
  31.  
  32.         if (args.length != 1) {
  33.           System.out.println("Command: java ReportServerSocket <port number>");
  34.             return;
  35.         }
  36.  
  37.         System.out.println("ReportServerSocket Session Starting");
  38.         System.out.println("*Factor is the smallest prime factor of Integer*");
  39.         port = Integer.parseInt(args[0]);
  40.  
  41. //    Create the ServerSocket
  42.         try {
  43.             server = new ServerSocket(port);
  44.             }
  45.         catch (IOException ioe) {
  46.             System.out.println("Unable to open port " + port);
  47.             return;
  48.         }
  49.  
  50. //  Listen for anyone sending reults back to the applet
  51.         while (true) {
  52.             try {
  53.                 socker = server.accept();
  54.                 innerStream = socker.getInputStream();
  55.             }
  56.             catch (IOException ioe) {
  57.                 System.out.println("Accept failed at port " + port);
  58.                 return;
  59.             }
  60.             try {
  61.                 numberbytes = innerStream.read(by, 0, 4096);
  62.             }
  63.             catch (IOException ioe) {
  64.                 System.out.println("Read failed at port " + port);
  65.                 return;
  66.             }
  67.             reply = new String(by, 0, 0, numberbytes);
  68.             System.out.println("Host Name / IP Address \t" + "Date" + 
  69.                                "\t\t\t\t" + "Integer  \t" + "Factor");
  70.             System.out.println(reply);
  71.  
  72. //  We could send a message back, but we won't right now
  73.             try {
  74.                 socker.close();
  75.             }
  76.             catch (IOException ioe) {
  77.                  System.out.println("Unable to close port " + port);
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84.