[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[al8] 011 - Simple client-server



SwikiCodeRobot @> 788

"client server exchange"
| server client receive msg |
server := Lua socket tcp.
server
    setoption: #reuseaddr to: true;
    settimeout: 5;
    bind: $* port: 9797;
    listen: 1;
    yourself.
receive := [ | packet tuple |
    packet := ''.
    tuple := client receive: 8192.
    [tuple first size > 0] whileTrue: [
        packet := packet ,tuple first.
        tuple := client receive: 8192
    ].
    (tuple first size = 0 and: [tuple last size > 0]) ifTrue: [
        packet := packet ,tuple last.
    ].
    packet notEmpty ifTrue: [ packet ]
].
client := Lua socket tcp.
client
    settimeout: 5;
    connect: '127.0.0.1' port: 9797;
    settimeout: 0;
    yourself.
server accept send: #helloThere.
Lua socket sleep: 0.1.
msg := receive value.
self note: 'msg=',msg asLiteral.
server close.
client close.
msg = #helloThere ifFalse: [
    self error: 'Wrong data transferred ' ,msg asLiteral
].