Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
hskom
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Hörnquist
hskom
Commits
9c5ed1bd
Commit
9c5ed1bd
authored
6 years ago
by
Hugo Hörnquist
Browse files
Options
Downloads
Patches
Plain Diff
Add network.lhs.
parent
500a9382
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
network.lhs
+66
-0
66 additions, 0 deletions
network.lhs
with
66 additions
and
0 deletions
network.lhs
0 → 100644
+
66
−
0
View file @
9c5ed1bd
\begin{code}
{-# LANGUAGE OverloadedStrings #-}
module Network where
\end{code}
\chapter{Testande av nätverk}
\begin{code}
import Network.Simple.TCP
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString as StrictB
import Data.Encoding
import Data.Encoding.ISO88591
import Data.Binary.Put -- runPut
import Data.Int
import Data.ByteString.Lazy.Char8 (pack)
import Data.String
\end{code}
\begin{code}
-- main :: IO ()
-- main = do
-- -- Bytesink
-- let bs = encode ISO88591 "Hörnquist"
\end{code}
telnet kom.lysator.liu.se 4894
A26Hbyers%kajsa.lysator.liu.se
\begin{code}
data Hollerith = Hollerith Int64 B.ByteString
deriving (Show, Eq)
instance IsString Hollerith where
fromString = makeHoll
makeHoll :: String -> Hollerith
makeHoll str = Hollerith (B.length bs) bs
where bs = runPut $ encode ISO88591 str
\end{code}
\begin{code}
fmtHoll :: Hollerith -> B.ByteString
fmtHoll (Hollerith n s) = (pack . show $ n) `B.append` "H" `B.append` s
\end{code}
A limit with network-simple's TCP sockets are that they only accept strict
bytestrings. And apparently this is one of the best ways to go from a lazy
to a strict bytestring.
\begin{code}
unLazy :: B.ByteString -> StrictB.ByteString
unLazy = StrictB.concat . B.toChunks
\end{code}
\begin{code}
komconnect :: IO (Socket)
komconnect = do
(sock, addr) <- connectSock "kom.lysator.liu.se" "4894"
send sock $ unLazy $ "A" `B.append` (fmtHoll "hugo%claptrap")
return sock
\end{code}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment