KnockKnockServer.java

Microsoft e SUN
Rispondi
Avatar utente
gdeber
GranGianGnomo
Messaggi: 1547
Iscritto il: 13 set 2001, 10:40
Località: Rivolta d'Adda
Contatta:

KnockKnockServer.java

Messaggio da gdeber » 12 lug 2002, 12:16

import java.net.*;
import java.io.*;
import java.lang.*;


public class KnockKnockServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;

try { serverSocket = new ServerSocket(4444); }

catch (IOException e) {

System.err.println("Could not listen on port: 4444.");
System.exit(1);
}

Socket clientSocket = null;
while (true)
{
try { clientSocket = serverSocket.accept(); }

catch (IOException e) {

System.err.println("Accept failed.");
System.exit(1);

}

ServerThread st = new ServerThread(clientSocket);

new Thread(st).start();

System.out.print ("thread started: number of active thread:");
System.out.println(Thread.activeCount());



} // ciclo infinito!!!



}
}

class KnockKnockProtocol {

public String processInput(String InputLine) {

return (InputLine+"ciao");
}
}

class ServerThread implements Runnable
{
private Socket clientsock;

public ServerThread (Socket ClientSocket)
{
clientsock=ClientSocket;
}


public void run() {

PrintWriter out=null;
BufferedReader in =null;

try{
out = new PrintWriter(clientsock.getOutputStream(), true);
in = new BufferedReader( new InputStreamReader( clientsock.getInputStream()));

String inputLine, outputLine;

KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);

while ((inputLine = in.readLine()) != null) {

outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye.ciao")) break;

}

out.close();
in.close();
clientsock.close();
}

catch (IOException e){
System.err.println("buffer creation failed.");
System.exit(1);
}
}
};

***L'assembler è alla base dell'universo®***

Rispondi