Skip to content

Instantly share code, notes, and snippets.

@mnstrspeed
Last active December 31, 2015 19:19
Show Gist options
  • Save mnstrspeed/8033186 to your computer and use it in GitHub Desktop.
Save mnstrspeed/8033186 to your computer and use it in GitHub Desktop.
module EchoServer
import StdEnv, StdTCP
port :== 1887
Start world
# (console, world) = stdio world
# (ok, listener, world) = openTCP_Listener port world
| not ok = fclose (console <<< "ehhh") world
# (response, listener, world) = receive (fromJust listener) world
# (ip, {sChannel, rChannel}) = response
# console = console <<< toString ip <<< " connected\n"
# (message, rChannel, world) = receive rChannel world
# console = console <<< toString message <<< "\n"
# (sChannel, world) = send (toByteSeq message) sChannel world
# world = closeRChannel rChannel world
# world = closeChannel sChannel world
# world = closeRChannel listener world
= fclose console world
// ------------------------------------------------------------------------------------
module EchoClient
import StdEnv, StdTCP
Start world
# (console, world) = stdio world
# (host, console) = ask "enter host: " console
# (port, console) = ask "enter port: " console
# (ip, world) = lookupIPAddress host world
| isNothing ip = fclose (console <<< "could not find host\n") world
# (status, channel, world) = connectTCP_MT Nothing (fromJust ip, toInt port) world
| status <> TR_Success = fclose (console <<< "could not connect\n") world
# { sChannel, rChannel } = fromJust channel
# (message, console) = ask "> " console
# (sChannel, world) = send (toByteSeq message) sChannel world
# (response, rChannel, world) = receive rChannel world
# console = console <<< "< " <<< toString response <<< "\n"
# world = closeRChannel rChannel world
# world = closeChannel sChannel world
= fclose console world
ask :: String *File -> (String, *File)
ask question console
# (answer, console) = freadline (console <<< question)
= (strip_newline answer, console)
where
strip_newline :: String -> String
strip_newline str = str % (0, (size str) - 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment