rendered paste bodyimport java.io.*;
import java.net.*;
class server implements Runnable {
Thread thread;
ServerSocket serverSocket = null;
Socket clientSocket = null;
int connects = 0;
int portti;
boolean running;
public void start(int portti) {
this.portti = portti;
thread = new Thread(this);
thread.start();
running = true;
}
public void run() {
while(running) {
try {
// Create the server socket
serverSocket = new ServerSocket(portti, 5);
while (connects < 5) {
// Wait for a connection
System.out.println("Waiting...");
clientSocket = serverSocket.accept();
//Service the connection
service(clientSocket);
connects++;
}
serverSocket.close();
}
catch (IOException ioe) {
System.out.println("Error in SimpleWebServer: " + ioe);
}
}
}
public void stop() {
connects = 100;
running = false;
}
public static void service(Socket client) throws IOException {
BufferedReader inbound = null;
DataOutputStream outbound = null;
try {
// The streams for input and output
inbound = new BufferedReader(
new InputStreamReader(client.getInputStream()));
outbound = new DataOutputStream(
client.getOutputStream());
// This is the output
StringBuffer buffer = new StringBuffer( "<HTML><HEAD><TITLE>Apassi</TITLE></HEAD><BODY><h2>Tiedostopalvelin</h2><br>");
File[] tiedostot = tiedostoListaus.listaaTiedostot();
for ( int i = 0; i < tiedostot.length; i++ ) {
buffer.append("<A HREF=\"");
buffer.append(tiedostot[i].getName());
buffer.append("\">");
buffer.append(tiedostot[i].getName());
buffer.append(" ");
buffer.append(tiedostot[i].length()/1024);
buffer.append(" kB</A><br>");
}
buffer.append("</BODY></HTML>");
String inputLine;
while ((inputLine = inbound.readLine()) != null) {
// When end of the the reguest, send the output
if ( inputLine.equals("") ) {
outbound.writeBytes(buffer.toString());
break;
}
}
} finally {
// Close everything
System.out.println("Client: " + client);
outbound.close();
inbound.close();
client.close();
}
}
}