Writing Data to a Client


try {
  ServerSocket ss = new ServerSocket(port);
  while (true) {
    try {
      Socket s = ss.accept();
      Writer out = new OutputStreamWriter(s.getOutputStream());
      out = new BufferedWriter(out);
      out.write("Hello " + s.getInetAddress() + " on port " 
       + s.getPort() + "\r\n");
      out.write("This is " + s.getLocalAddress() + " on port " 
       + s.getLocalPort() + "\r\n");
      out.flush();
      s.close();
    }
    catch (IOException ex) {
      // skip this connection; continue with the next
    }
  }
}
catch (IOException ex) { 
  System.err.println(ex); 
}

Previous | Next | Top | Cafe con Leche

Copyright 2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 13, 2003