TCP/IP

| TCPLISTEN (Run with SSX)

| prog is started with this command 'R TCPLISTEN 8887'

| This prog will establish a listening socket for the port specified in the command line.

| It will accept any connection requests and run the tcpserver prog to handle message transfers.


[] $port | string from command line containing port number

~ $port port | convert to integer

listen port lsok | listen to port set socket id to lsok

if lsok eq 0

'Listen failed on port ' $s

+ $port $s

$s []

stop

else

'Listening on port ' $s

+ $port $s

$s []

endif

loop

accept lsok sok | accept the connection socket if available

if sok eq 0 | if not available go to loop

delay 1000

go loop

endif

'Connection on port ' $s

+ $port $s

$s []

'R tcpserver ' $s | set up server command

~ sok $sok | convert integer to string

+ $sok $s | add socket id to string

ssx $s | command runtime to start tcpserver

go loop | wait for more connection requests



| TCPSERVER (Run with SSX)

| This prog will communicate over the connected socket


[] $sok | socket id from tcplisten prog

~ $sok sok | convert to integer

'Symsyn host ...' $s

$s [sok] | send welcome message to connecting socket

loop

50 #$s | set maximun message size for next read to 50 characters

[sok] $s | receive message if available

if $s eq $nul | if not available delay and go to loop

if ioresult ne 0 | ioresult is not 0 if connection has been terminated

close sok | close socket

stop

endif

delay 1000

go loop

endif

$s [] | send received message to console

go loop | go to loop for next message



| TCPCONNECT (Run with SSX)

| prog is started with this command 'R TCPCONNECT 8887'

| This prog will connect to the port specified in the command line

| LocalHost address (127.0.0.1) is used in this example


[] $port | port number in command

~ $port port | convert to integer

'127.0.0.1' $addr | address string

connect $addr port sok | connect to address and port set up socket

if sok eq 0 | if sok = 0, socket failure

'Connect failed on port ' $s

+ $port $s

$s []

stop

endif

loop

50 #$s | set maximun receive size to 50 characters

[sok] $s | read message into string

if $s eq $nul | if no message available, delay and retry

delay 500

go loop

endif

$s [] | display message on console

'Client responding...' [sok] | send message to host

delay 3000

close sok | close connection