Calss Socket :
public Socket(int portNumber) throws Exception{
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
java.net.Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.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.”))
// break;
}
}catch(Exception error) {
throw new Exception(“Errore creazione del Socket Server”);
}
}
Class KnockKnockProtocol :
public class KnockKnockProtocol {
public static String lastFile ;
public static String timestampLastFile ;
public static String lastError ;
private static final int WAITING = 0;
private static final int SENTKNOCKKNOCK = 1;
private int state = WAITING;
private String[] clues = { “Test Service”, “Ultima elaborazione”, “Help” ,”Autore”};
private String[] answers = { “Test Service è il fantastico servizio 🙂 !!!!!!!!”,
” “,
“1) UniIT Service ? 2) Ultima elaborazione ? 3) Help ? “,
“Morpheus”,
“” };
public String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
theOutput = “Knock! Knock!”;
state = SENTKNOCKKNOCK;
} else if (state == SENTKNOCKKNOCK) {
if (theInput.trim().equalsIgnoreCase(“Chi sei ?”)) {
theOutput = clues[0];
} if (theInput.trim().equalsIgnoreCase(clues[0] + ” ?”)) {
theOutput = answers[0] ;
} if (theInput.trim().equalsIgnoreCase(clues[1] + ” ?”)) {
theOutput = lastFile + ” Date : ” +timestampLastFile ;
} if (theInput.trim().equalsIgnoreCase(clues[2] + ” ?”)) {
theOutput = answers[2] ;
}if (theInput.trim().equalsIgnoreCase(clues[3] + ” ?”)) {
theOutput = answers[3] ;
}if (theInput.trim().equalsIgnoreCase(“Bye”)) {
theOutput = “Bye.”;
state = WAITING;
}
}
return theOutput;
}
}
Client to test Server Socket :
Class KnockKnockClient
public class KnockKnockClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println( “Usage: java EchoClient <host name> <port number>”);
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
java.net.Socket kkSocket = new java.net.Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println(“Server: ” + fromServer);
if (fromServer.equals(“Bye.”))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println(“Client: ” + fromUser);
out.println(fromUser);
}
}
} catch (UnknownHostException e) {
System.err.println(“Don’t know about host ” + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println(“Couldn’t get I/O for the connection to ” +
hostName);
System.exit(1);
}
}
}