FileDocCategorySizeDatePackage
HelloServer.javaAPI DocExample1016Sun Mar 28 19:06:00 BST 1999None

HelloServer

public class HelloServer extends Object

Fields Summary
public static final int
defaultPort
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


       
  
    int port = defaultPort;
    
    try {
      port = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
    }
    if (port <= 0 || port >= 65536) port = defaultPort;
    
    try {
      ServerSocket ss = new ServerSocket(port);
      while (true) {
        try {
          Socket s = ss.accept();
          
          String response = "Hello " + s.getInetAddress() + " on port " 
           + s.getPort() + "\r\n";
          response += "This is " + s.getLocalAddress() + " on port " 
           + s.getLocalPort() + "\r\n";
          OutputStream out = s.getOutputStream();
          out.write(response.getBytes());
          out.flush();
          s.close();
        }
        catch (IOException e) {
        }
      }
    }
    catch (IOException e) {
      System.err.println(e);
    }