(* * LANGUAGE : ANS Forth * PROJECT : Forth Environments * DESCRIPTION : Example internetworking usage (based on idea by Skip Carter) * CATEGORY : Sockets * AUTHOR : Skip Carter / Marcel Hendrix * LAST CHANGE : October 20, 1997, mhx; added speedtest * LAST CHANGE : October 16, 1997, mhx *) NEEDS -sockets REVISION -cliserv "ÄÄÄ Client - Server Version 1.01 ÄÄÄ" PRIVATES 5 =: /queue PRIVATE 7 =: /data PRIVATE #2000000 =: /bigsize PRIVATE [DEFINED] _server_ [IF] (* Example server code, accept some data and echo it back with the sign changed * * example usage: 3145 ( port#) server * *) : server ( port# -- ) 0 0 LOCALS| loc sock | CREATE-SERVER TO sock sock /queue LISTEN ( wait for the client to connect ) sock ACCEPT-SOCKET TO loc ( not going to listen for more connections ) sock CLOSE-SOCKET ( send a welcome message to the client ) S" Welcome to Internetworking with Forth!" DUP here ! here 1 cells loc write-socket ( count) loc write-socket ( text) ( read the number of points the remote end is going to send ) loc here 1 CELLS read-socket DROP @ ( #) 0 ?DO loc here 1 CELLS read-socket DROP CR ." read " @ DEC. here @ NEGATE here ! here 1 CELLS loc write-socket ." -- wrote " here @ DEC. LOOP loc close-socket ; : serverspeed ( port# -- ) 0 0 0 0 LOCALS| ^data sz loc sock | CREATE-SERVER TO sock sock /queue LISTEN ( wait for the client to connect ) sock ACCEPT-SOCKET TO loc ( not going to listen for more connections ) sock CLOSE-SOCKET ( send a welcome message to the client ) S" Welcome to the socket speed test with iForth!" DUP here ! here 1 cells loc write-socket ( count) loc write-socket ( text) ( read the number of bytes the remote end is going to send ) loc here 1 CELLS read-socket DROP @ TO sz ( #) sz ALLOCATE ?ALLOCATE TO ^data ?MS >R loc ^data sz read-socket NIP DUP ( bytes) ?MS R> - 1 MAX / CR .KB ." MB/sec, read " .MB ." Mbytes." ^data FREE ?ALLOCATE loc close-socket ; :ABOUT CR ." Try: 3145 server" CR ." 3145 serverspeed" ; [ELSE] [UNDEFINED] _client_ [IF] CR .( Neither client nor server defined) ABORT [THEN] (* Example client code, get message from the server, then write some data to the server and * get its response. * * S" iaehv.iaehv.nl" ( internet address) 3145 ( port#) client * *) : client ( c-addr u port -- ) OPEN-SERVICE LOCAL sock ( read the welcome message from the server ) sock here 1 cells read-socket DROP sock PAD ROT @ read-socket CR TYPE ( tell the server how much data we are going to send ) /data here ! here 1 CELLS sock write-socket ( now send the data, one at a time, and print the returned data ) /data 0 ?DO I 1+ here ! here 1 CELLS sock write-socket CR ." wrote " I 1+ DEC. sock here 1 cells read-socket DROP @ ." -- read " DEC. LOOP sock close-socket ; : clientspeed ( c-addr u port# -- ) OPEN-SERVICE 0 LOCALS| ^data sock | ( read the welcome message from the server ) sock here 1 cells read-socket 2DROP sock here DUP @ read-socket CR TYPE ( tell the server how much data we are going to send ) /bigsize here ! here 1 CELLS sock write-socket /bigsize ALLOCATE ?ALLOCATE TO ^data ( now send the data ) ?MS >R ^data /bigsize sock write-socket /bigsize DUP ?MS R> - 1 MAX / CR .KB ." MB/sec, wrote " .MB ." Mbytes." ^data FREE ?ALLOCATE sock close-socket ; :ABOUT CR .~ Try: S" frunobulax" 3145 client~ CR .~ S" frunobulax" 3145 clientspeed~ ; [THEN] .ABOUT -cliserv CR DEPRIVE (* End of Source *)