19
Introduction The Client The Server The Knock Knock Protocol References T HE KNOCK KNOCK P ROTOCOL Muhammad Adil Raja Roaming Researchers, R . cbna December 2, 2015 Muhammad Adil Raja Roaming Researchers, R . cbna The Knock Knock Protocol

The Knock Knock Protocol

Embed Size (px)

Citation preview

Page 1: The Knock Knock Protocol

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

Page 2: The 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

Page 3: The 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

Page 4: The 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

Page 5: The 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

Page 6: The 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

Page 7: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

INTRODUCTION

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 8: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE CLIENT I

/∗∗ Copyr ight ( 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 i n source and b inary forms , w i th or w i thou t∗ mod i f i ca t i on , are permi t ted provided t h a t the f o l l o w i n g cond i t i ons∗ are met :∗∗ − R e d i s t r i b u t i o n s o f source code must r e t a i n the above copy r i gh t∗ not ice , t h i s l i s t o f cond i t i ons and the f o l l o w i n g d i sc l a ime r .∗∗ − R e d i s t r i b u t i o n s i n b inary form must reproduce the above copy r i gh t∗ not ice , t h i s l i s t o f cond i t i ons and the f o l l o w i n g d i sc l a ime r i n the∗ documentation and / or o ther ma te r i a l s prov ided wi th the d i s t r i b u t i o n .∗∗ − Nei ther 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 der ived∗ from t h i s sof tware w i thou t s p e c i f i c p r i o r w r i t t e n permiss ion .∗∗ 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

Page 9: The 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 . i o .∗ ;import java . net .∗ ;

public class KnockKnockClient {public s t a t i c void main ( S t r i n g [ ] args ) throws IOExcept ion {

i f ( args . leng th != 2) {System . e r r . p r i n t l n (

" Usage : java EchoCl ient <host name> < po r t number>" ) ;System . e x i t ( 1 ) ;

}

S t r i n g hostName = args [ 0 ] ;i n t portNumber = In tege r . pa rse In t ( args [ 1 ] ) ;

t ry (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 i n = new BufferedReader (

new InputStreamReader ( kkSocket . get InputStream ( ) ) ) ;

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 10: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE CLIENT III) {

BufferedReader s td In =new BufferedReader (new InputStreamReader ( System . i n ) ) ;

S t r i n g fromServer ;S t r i n g fromUser ;

while ( ( f romServer = i n . readLine ( ) ) != nul l ) {System . out . p r i n t l n ( " Server : " + fromServer ) ;i f ( f romServer . equals ( " Bye . " ) )

break ;

fromUser = s td In . readLine ( ) ;i f ( fromUser != nul l ) {

System . out . p r i n t l n ( " C l i e n t : " + fromUser ) ;out . p r i n t l n ( fromUser ) ;

}}

} catch ( UnknownHostException e ) {System . e r r . p r i n t l n ( "Don ’ t know about host " + hostName ) ;System . e x i t ( 1 ) ;

} catch ( IOExcept ion e ) {System . e r r . p r i n t l n ( " Couldn ’ t get I /O f o r the connect ion to " +

hostName ) ;System . e x i t ( 1 ) ;

}}

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 11: The 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

Page 12: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE SERVER I

/∗∗ Copyr ight ( 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 i n source and b inary forms , w i th or w i thou t∗ mod i f i ca t i on , are permi t ted provided t h a t the f o l l o w i n g cond i t i ons∗ are met :∗∗ − R e d i s t r i b u t i o n s o f source code must r e t a i n the above copy r i gh t∗ not ice , t h i s l i s t o f cond i t i ons and the f o l l o w i n g d i sc l a ime r .∗∗ − R e d i s t r i b u t i o n s i n b inary form must reproduce the above copy r i gh t∗ not ice , t h i s l i s t o f cond i t i ons and the f o l l o w i n g d i sc l a ime r i n the∗ documentation and / or o ther ma te r i a l s prov ided wi th the d i s t r i b u t i o n .∗∗ − Nei ther 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 der ived∗ from t h i s sof tware w i thou t s p e c i f i c p r i o r w r i t t e n permiss ion .∗∗ 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

Page 13: The 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 . i o .∗ ;

public class KnockKnockServer {public s t a t i c void main ( S t r i n g [ ] args ) throws IOExcept ion {

i f ( args . leng th != 1) {System . e r r . p r i n t l n ( " Usage : java KnockKnockServer < po r t number>" ) ;System . e x i t ( 1 ) ;

}

i n t portNumber = In tege r . pa rse In t ( args [ 0 ] ) ;

t ry (ServerSocket serverSocket = new ServerSocket ( portNumber ) ;Socket c l i en t Socke t = 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 ( c l i en t Socke t . getOutputStream ( ) , true ) ;BufferedReader i n = new BufferedReader (

new InputStreamReader ( c l i en tSo cke t . get InputStream ( ) ) ) ;

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 14: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE SERVER III

) {

S t r i n g inpu tL ine , ou tpu tL ine ;

/ / I n i t i a t e conversat ion wi th c l i e n tKnockKnockProtocol kkp = new KnockKnockProtocol ( ) ;ou tpu tL ine = kkp . processInput ( nul l ) ;out . p r i n t l n ( ou tpu tL ine ) ;

while ( ( i npu tL ine = i n . readLine ( ) ) != nul l ) {ou tpu tL ine = kkp . processInput ( i npu tL ine ) ;out . p r i n t l n ( ou tpu tL ine ) ;i f ( ou tpu tL ine . equals ( " Bye . " ) )

break ;}

} catch ( IOExcept ion e ) {System . out . p r i n t l n ( " Except ion caught when t r y i n g to l i s t e n on po r t "

+ portNumber + " or l i s t e n i n g f o r a connect ion " ) ;System . out . p r i n t l n ( e . getMessage ( ) ) ;

}}

}

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 15: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE KNOCK KNOCK PROTOCOL I

/∗∗ Copyr ight ( 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 i n source and b inary forms , w i th or w i thou t∗ mod i f i ca t i on , are permi t ted provided t h a t the f o l l o w i n g cond i t i ons∗ are met :∗∗ − R e d i s t r i b u t i o n s o f source code must r e t a i n the above copy r i gh t∗ not ice , t h i s l i s t o f cond i t i ons and the f o l l o w i n g d i sc l a ime r .∗∗ − R e d i s t r i b u t i o n s i n b inary form must reproduce the above copy r i gh t∗ not ice , t h i s l i s t o f cond i t i ons and the f o l l o w i n g d i sc l a ime r i n the∗ documentation and / or o ther ma te r i a l s prov ided wi th the d i s t r i b u t i o n .∗∗ − Nei ther 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 der ived∗ from t h i s sof tware w i thou t s p e c i f i c p r i o r w r i t t e n permiss ion .∗∗ 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

Page 16: The 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 . i o .∗ ;

public class KnockKnockProtocol {private s t a t i c f i n a l i n t WAITING = 0;private s t a t i c f i n a l i n t SENTKNOCKKNOCK = 1;private s t a t i c f i n a l i n t SENTCLUE = 2;private s t a t i c f i n a l i n t ANOTHER = 3;

private s t a t i c f i n a l i n t NUMJOKES = 5;

private i n t s ta te = WAITING ;private i n t cur rentJoke = 0;

private S t r i n g [ ] c lues = { " Turn ip " , " L i t t l e Old Lady " , " Atch " , "Who" , "Who" } ;private S t r i n g [ ] answers = { " Turn ip the heat , i t ’ s co ld i n here ! " ,

" I d idn ’ t know you could yodel ! " ," Bless you ! " ," I s there an owl i n here? " ," I s there an echo i n here? " } ;

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 17: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE KNOCK KNOCK PROTOCOL III

public S t r i n g processInput ( S t r i n g the Inpu t ) {S t r i n g theOutput = nul l ;

i f ( s t a t e == WAITING) {theOutput = " Knock ! Knock ! " ;s t a t e = SENTKNOCKKNOCK;

} else i f ( s t a t e == SENTKNOCKKNOCK) {i f ( t he Inpu t . equalsIgnoreCase ( "Who ’ s there ? " ) ) {

theOutput = c lues [ cur rentJoke ] ;s t a t e = SENTCLUE;

} else {theOutput = "You ’ re supposed to say \ "Who ’ s there ? \ " ! " +

" Try again . Knock ! Knock ! " ;}

} else i f ( s t a t e == SENTCLUE) {i f ( t he Inpu t . equalsIgnoreCase ( c lues [ cur rentJoke ] + " who? " ) ) {

theOutput = answers [ cur rentJoke ] + " Want another? ( y / n ) " ;s t a t e = ANOTHER;

} else {theOutput = "You ’ re supposed to say \ " " +

c lues [ cur rentJoke ] +" who ? \ " " +" ! Try again . Knock ! Knock ! " ;

s t a t e = SENTKNOCKKNOCK;}

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 18: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

THE KNOCK KNOCK PROTOCOL IV

} else i f ( s t a t e == ANOTHER) {i f ( t he Inpu t . equalsIgnoreCase ( " y " ) ) {

theOutput = " Knock ! Knock ! " ;i f ( cur rentJoke == (NUMJOKES − 1 ) )

cur rentJoke = 0;else

cur rentJoke ++;s ta t e = SENTKNOCKKNOCK;

} else {theOutput = " Bye . " ;s t a t e = WAITING ;

}}return theOutput ;

}}

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol

Page 19: The Knock Knock Protocol

Introduction The Client The Server The Knock Knock Protocol References

REFERENCES

The source code has been taken fromhere.This presentation has been produced with LATEX.Berlin, beetle.

Muhammad Adil Raja Roaming Researchers, R©.cbnaThe Knock Knock Protocol