Introduction The Client The Server The Knock Knock Protocol References
THE KNOCK KNOCK PROTOCOL
Muhammad Adil Raja
Roaming Researchers, R .
cbna
December 2, 2015
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
OUTLINE
1 INTRODUCTION
2 THE CLIENT
3 THE SERVER
4 THE KNOCK KNOCK PROTOCOL
5 REFERENCES
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
OUTLINE
1 INTRODUCTION
2 THE CLIENT
3 THE SERVER
4 THE KNOCK KNOCK PROTOCOL
5 REFERENCES
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
OUTLINE
1 INTRODUCTION
2 THE CLIENT
3 THE SERVER
4 THE KNOCK KNOCK PROTOCOL
5 REFERENCES
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
OUTLINE
1 INTRODUCTION
2 THE CLIENT
3 THE SERVER
4 THE KNOCK KNOCK PROTOCOL
5 REFERENCES
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
OUTLINE
1 INTRODUCTION
2 THE CLIENT
3 THE SERVER
4 THE KNOCK KNOCK PROTOCOL
5 REFERENCES
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
INTRODUCTION
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE CLIENT I
/∗
∗ Copyright ( c ) 1995, 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE CLIENT II
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
∗/
import java . io . ∗ ;
import java . net . ∗ ;
public class KnockKnockClient {
public static void main ( String [ ] args ) throws IOException {
i f ( args . length != 2) {
System . err . p r i n t l n (
"Usage : java EchoClient <host name> <port number>" ) ;
System . e x i t ( 1 ) ;
}
String hostName = args [ 0 ] ;
int portNumber = Integer . parseInt ( args [ 1 ] ) ;
try (
Socket kkSocket = new Socket ( hostName , portNumber ) ;
P r i n t W r i t e r out = new P r i n t W r i t e r ( kkSocket . getOutputStream ( ) , true ) ;
BufferedReader in = new BufferedReader (
new InputStreamReader ( kkSocket . getInputStream ( ) ) ) ;
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE CLIENT III
) {
BufferedReader stdIn =
new BufferedReader (new InputStreamReader ( System . in ) ) ;
String fromServer ;
String fromUser ;
while ( ( fromServer = in . readLine ( ) ) != null ) {
System . out . p r i n t l n ( " Server : " + fromServer ) ;
i f ( fromServer . equals ( "Bye . " ) )
break ;
fromUser = stdIn . readLine ( ) ;
i f ( fromUser != null ) {
System . out . p r i n t l n ( " Client : " + fromUser ) ;
out . p r i n t l n ( fromUser ) ;
}
}
} catch ( UnknownHostException e ) {
System . err . p r i n t l n ( "Don ’ t know about host " + hostName ) ;
System . e x i t ( 1 ) ;
} catch ( IOException e ) {
System . err . p r i n t l n ( " Couldn ’ t get I /O f o r the connection to " +
hostName ) ;
System . e x i t ( 1 ) ;
}
}
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE CLIENT IV
}
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE SERVER I
/∗
∗ Copyright ( c ) 1995, 2014, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE SERVER II
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
∗/
import java . net . ∗ ;
import java . io . ∗ ;
public class KnockKnockServer {
public static void main ( String [ ] args ) throws IOException {
i f ( args . length != 1) {
System . err . p r i n t l n ( "Usage : java KnockKnockServer <port number>" ) ;
System . e x i t ( 1 ) ;
}
int portNumber = Integer . parseInt ( args [ 0 ] ) ;
try (
ServerSocket serverSocket = new ServerSocket ( portNumber ) ;
Socket clientSocket = serverSocket . accept ( ) ;
P r i n t W r i t e r out =
new P r i n t W r i t e r ( clientSocket . getOutputStream ( ) , true ) ;
BufferedReader in = new BufferedReader (
new InputStreamReader ( clientSocket . getInputStream ( ) ) ) ;
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE SERVER III
) {
String inputLine , outputLine ;
/ / I n i t i a t e conversation with c l i e n t
KnockKnockProtocol kkp = new KnockKnockProtocol ( ) ;
outputLine = kkp . processInput ( null ) ;
out . p r i n t l n ( outputLine ) ;
while ( ( inputLine = in . readLine ( ) ) != null ) {
outputLine = kkp . processInput ( inputLine ) ;
out . p r i n t l n ( outputLine ) ;
i f ( outputLine . equals ( "Bye . " ) )
break ;
}
} catch ( IOException e ) {
System . out . p r i n t l n ( " Exception caught when t r y i n g to l i s t e n on port "
+ portNumber + " or l i s t e n i n g f o r a connection " ) ;
System . out . p r i n t l n ( e . getMessage ( ) ) ;
}
}
}
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE KNOCK KNOCK PROTOCOL I
/∗
∗ Copyright ( c ) 1995, 2008, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE KNOCK KNOCK PROTOCOL II
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
∗/
import java . net . ∗ ;
import java . io . ∗ ;
public class KnockKnockProtocol {
private static f i n a l int WAITING = 0;
private static f i n a l int SENTKNOCKKNOCK = 1;
private static f i n a l int SENTCLUE = 2;
private static f i n a l int ANOTHER = 3;
private static f i n a l int NUMJOKES = 5;
private int state = WAITING ;
private int currentJoke = 0;
private String [ ] clues = { " Turnip " , " L i t t l e Old Lady " , " Atch " , "Who" , "Who" } ;
private String [ ] answers = { " Turnip the heat , i t ’ s cold in here ! " ,
" I didn ’ t know you could yodel ! " ,
" Bless you ! " ,
" Is there an owl in here?" ,
" Is there an echo in here?" } ;
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE KNOCK KNOCK PROTOCOL III
public String processInput ( String theInput ) {
String theOutput = null ;
i f ( state == WAITING) {
theOutput = " Knock ! Knock ! " ;
state = SENTKNOCKKNOCK;
} else i f ( state == SENTKNOCKKNOCK) {
i f ( theInput . equalsIgnoreCase ( "Who ’ s there?" ) ) {
theOutput = clues [ currentJoke ] ;
state = SENTCLUE;
} else {
theOutput = "You ’ re supposed to say  "Who ’ s there ?  " ! " +
" Try again . Knock ! Knock ! " ;
}
} else i f ( state == SENTCLUE) {
i f ( theInput . equalsIgnoreCase ( clues [ currentJoke ] + " who?" ) ) {
theOutput = answers [ currentJoke ] + " Want another? ( y / n ) " ;
state = ANOTHER;
} else {
theOutput = "You ’ re supposed to say  " " +
clues [ currentJoke ] +
" who?  " " +
" ! Try again . Knock ! Knock ! " ;
state = SENTKNOCKKNOCK;
}
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
THE KNOCK KNOCK PROTOCOL IV
} else i f ( state == ANOTHER) {
i f ( theInput . equalsIgnoreCase ( " y " ) ) {
theOutput = " Knock ! Knock ! " ;
i f ( currentJoke == (NUMJOKES − 1))
currentJoke = 0;
else
currentJoke ++;
state = SENTKNOCKKNOCK;
} else {
theOutput = "Bye . " ;
state = WAITING ;
}
}
return theOutput ;
}
}
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
Introduction The Client The Server The Knock Knock Protocol References
REFERENCES
The source code has been taken from
here.
This presentation has been produced with LATEX.
Berlin, beetle.
Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol

The Knock Knock Protocol

  • 1.
    Introduction The ClientThe Server The Knock Knock Protocol References THE KNOCK KNOCK PROTOCOL Muhammad Adil Raja Roaming Researchers, R . cbna December 2, 2015 Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 2.
    Introduction The ClientThe Server The Knock Knock Protocol References OUTLINE 1 INTRODUCTION 2 THE CLIENT 3 THE SERVER 4 THE KNOCK KNOCK PROTOCOL 5 REFERENCES Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 3.
    Introduction The ClientThe Server The Knock Knock Protocol References OUTLINE 1 INTRODUCTION 2 THE CLIENT 3 THE SERVER 4 THE KNOCK KNOCK PROTOCOL 5 REFERENCES Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 4.
    Introduction The ClientThe Server The Knock Knock Protocol References OUTLINE 1 INTRODUCTION 2 THE CLIENT 3 THE SERVER 4 THE KNOCK KNOCK PROTOCOL 5 REFERENCES Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 5.
    Introduction The ClientThe Server The Knock Knock Protocol References OUTLINE 1 INTRODUCTION 2 THE CLIENT 3 THE SERVER 4 THE KNOCK KNOCK PROTOCOL 5 REFERENCES Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 6.
    Introduction The ClientThe Server The Knock Knock Protocol References OUTLINE 1 INTRODUCTION 2 THE CLIENT 3 THE SERVER 4 THE KNOCK KNOCK PROTOCOL 5 REFERENCES Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 7.
    Introduction The ClientThe Server The Knock Knock Protocol References INTRODUCTION Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 8.
    Introduction The ClientThe Server The Knock Knock Protocol References THE CLIENT I /∗ ∗ Copyright ( c ) 1995, 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved . ∗ ∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without ∗ modification , are permitted provided that the f o l l o w i n g conditions ∗ are met : ∗ ∗ − Redistributions of source code must r e t a i n the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer . ∗ ∗ − Redistributions in binary form must reproduce the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the ∗ documentation and / or other materials provided with the d i s t r i b u t i o n . ∗ ∗ − Neither the name of Oracle or the names of i t s ∗ c o n t r i b u t o r s may be used to endorse or promote products derived ∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission . ∗ ∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR ∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL , ∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 9.
    Introduction The ClientThe Server The Knock Knock Protocol References THE CLIENT II ∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING ∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ∗/ import java . io . ∗ ; import java . net . ∗ ; public class KnockKnockClient { public static void main ( String [ ] args ) throws IOException { i f ( args . length != 2) { System . err . p r i n t l n ( "Usage : java EchoClient <host name> <port number>" ) ; System . e x i t ( 1 ) ; } String hostName = args [ 0 ] ; int portNumber = Integer . parseInt ( args [ 1 ] ) ; try ( Socket kkSocket = new Socket ( hostName , portNumber ) ; P r i n t W r i t e r out = new P r i n t W r i t e r ( kkSocket . getOutputStream ( ) , true ) ; BufferedReader in = new BufferedReader ( new InputStreamReader ( kkSocket . getInputStream ( ) ) ) ; Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 10.
    Introduction The ClientThe Server The Knock Knock Protocol References THE CLIENT III ) { BufferedReader stdIn = new BufferedReader (new InputStreamReader ( System . in ) ) ; String fromServer ; String fromUser ; while ( ( fromServer = in . readLine ( ) ) != null ) { System . out . p r i n t l n ( " Server : " + fromServer ) ; i f ( fromServer . equals ( "Bye . " ) ) break ; fromUser = stdIn . readLine ( ) ; i f ( fromUser != null ) { System . out . p r i n t l n ( " Client : " + fromUser ) ; out . p r i n t l n ( fromUser ) ; } } } catch ( UnknownHostException e ) { System . err . p r i n t l n ( "Don ’ t know about host " + hostName ) ; System . e x i t ( 1 ) ; } catch ( IOException e ) { System . err . p r i n t l n ( " Couldn ’ t get I /O f o r the connection to " + hostName ) ; System . e x i t ( 1 ) ; } } Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 11.
    Introduction The ClientThe Server The Knock Knock Protocol References THE CLIENT IV } Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 12.
    Introduction The ClientThe Server The Knock Knock Protocol References THE SERVER I /∗ ∗ Copyright ( c ) 1995, 2014, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved . ∗ ∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without ∗ modification , are permitted provided that the f o l l o w i n g conditions ∗ are met : ∗ ∗ − Redistributions of source code must r e t a i n the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer . ∗ ∗ − Redistributions in binary form must reproduce the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the ∗ documentation and / or other materials provided with the d i s t r i b u t i o n . ∗ ∗ − Neither the name of Oracle or the names of i t s ∗ c o n t r i b u t o r s may be used to endorse or promote products derived ∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission . ∗ ∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR ∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL , ∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 13.
    Introduction The ClientThe Server The Knock Knock Protocol References THE SERVER II ∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING ∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ∗/ import java . net . ∗ ; import java . io . ∗ ; public class KnockKnockServer { public static void main ( String [ ] args ) throws IOException { i f ( args . length != 1) { System . err . p r i n t l n ( "Usage : java KnockKnockServer <port number>" ) ; System . e x i t ( 1 ) ; } int portNumber = Integer . parseInt ( args [ 0 ] ) ; try ( ServerSocket serverSocket = new ServerSocket ( portNumber ) ; Socket clientSocket = serverSocket . accept ( ) ; P r i n t W r i t e r out = new P r i n t W r i t e r ( clientSocket . getOutputStream ( ) , true ) ; BufferedReader in = new BufferedReader ( new InputStreamReader ( clientSocket . getInputStream ( ) ) ) ; Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 14.
    Introduction The ClientThe Server The Knock Knock Protocol References THE SERVER III ) { String inputLine , outputLine ; / / I n i t i a t e conversation with c l i e n t KnockKnockProtocol kkp = new KnockKnockProtocol ( ) ; outputLine = kkp . processInput ( null ) ; out . p r i n t l n ( outputLine ) ; while ( ( inputLine = in . readLine ( ) ) != null ) { outputLine = kkp . processInput ( inputLine ) ; out . p r i n t l n ( outputLine ) ; i f ( outputLine . equals ( "Bye . " ) ) break ; } } catch ( IOException e ) { System . out . p r i n t l n ( " Exception caught when t r y i n g to l i s t e n on port " + portNumber + " or l i s t e n i n g f o r a connection " ) ; System . out . p r i n t l n ( e . getMessage ( ) ) ; } } } Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 15.
    Introduction The ClientThe Server The Knock Knock Protocol References THE KNOCK KNOCK PROTOCOL I /∗ ∗ Copyright ( c ) 1995, 2008, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved . ∗ ∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without ∗ modification , are permitted provided that the f o l l o w i n g conditions ∗ are met : ∗ ∗ − Redistributions of source code must r e t a i n the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer . ∗ ∗ − Redistributions in binary form must reproduce the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the ∗ documentation and / or other materials provided with the d i s t r i b u t i o n . ∗ ∗ − Neither the name of Oracle or the names of i t s ∗ c o n t r i b u t o r s may be used to endorse or promote products derived ∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission . ∗ ∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR ∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL , ∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 16.
    Introduction The ClientThe Server The Knock Knock Protocol References THE KNOCK KNOCK PROTOCOL II ∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING ∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ∗/ import java . net . ∗ ; import java . io . ∗ ; public class KnockKnockProtocol { private static f i n a l int WAITING = 0; private static f i n a l int SENTKNOCKKNOCK = 1; private static f i n a l int SENTCLUE = 2; private static f i n a l int ANOTHER = 3; private static f i n a l int NUMJOKES = 5; private int state = WAITING ; private int currentJoke = 0; private String [ ] clues = { " Turnip " , " L i t t l e Old Lady " , " Atch " , "Who" , "Who" } ; private String [ ] answers = { " Turnip the heat , i t ’ s cold in here ! " , " I didn ’ t know you could yodel ! " , " Bless you ! " , " Is there an owl in here?" , " Is there an echo in here?" } ; Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 17.
    Introduction The ClientThe Server The Knock Knock Protocol References THE KNOCK KNOCK PROTOCOL III public String processInput ( String theInput ) { String theOutput = null ; i f ( state == WAITING) { theOutput = " Knock ! Knock ! " ; state = SENTKNOCKKNOCK; } else i f ( state == SENTKNOCKKNOCK) { i f ( theInput . equalsIgnoreCase ( "Who ’ s there?" ) ) { theOutput = clues [ currentJoke ] ; state = SENTCLUE; } else { theOutput = "You ’ re supposed to say "Who ’ s there ? " ! " + " Try again . Knock ! Knock ! " ; } } else i f ( state == SENTCLUE) { i f ( theInput . equalsIgnoreCase ( clues [ currentJoke ] + " who?" ) ) { theOutput = answers [ currentJoke ] + " Want another? ( y / n ) " ; state = ANOTHER; } else { theOutput = "You ’ re supposed to say " " + clues [ currentJoke ] + " who? " " + " ! Try again . Knock ! Knock ! " ; state = SENTKNOCKKNOCK; } Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 18.
    Introduction The ClientThe Server The Knock Knock Protocol References THE KNOCK KNOCK PROTOCOL IV } else i f ( state == ANOTHER) { i f ( theInput . equalsIgnoreCase ( " y " ) ) { theOutput = " Knock ! Knock ! " ; i f ( currentJoke == (NUMJOKES − 1)) currentJoke = 0; else currentJoke ++; state = SENTKNOCKKNOCK; } else { theOutput = "Bye . " ; state = WAITING ; } } return theOutput ; } } Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol
  • 19.
    Introduction The ClientThe Server The Knock Knock Protocol References REFERENCES The source code has been taken from here. This presentation has been produced with LATEX. Berlin, beetle. Muhammad Adil Raja Roaming Researchers, R . cbnaThe Knock Knock Protocol