FileDocCategorySizeDatePackage
HelloServer.javaAPI DocExample930Fri Feb 03 16:23:02 GMT 2006None

HelloServer

public class HelloServer extends Object

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

  
    int port = 2345;
    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("US-ASCII"));
        out.flush();
        s.close();
      }
      catch (IOException ex) {
        // This is an error on one connection. Maybe the client crashed.
        // Maybe it broke the connection prematurely. Whatever happened, 
        // itâ