Skip to content

Commit

Permalink
Starts a server on the default port of 6667
Browse files Browse the repository at this point in the history
When connecting to the server it send "Hi" and then closes the
connection. We thought this could be a good starting point.

issue #3
Dave & Amos
  • Loading branch information
adkron committed Dec 21, 2012
1 parent dd604b3 commit f01122a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 3 additions & 1 deletion mcferkincaht.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ build-type: Simple

executable program1
build-depends: base == 4.5.*
main-is: Server.hs
, network >= 2.3
main-is: Main.hs
ghc-options:
-Wall -Werror
hs-source-dirs: src
Expand All @@ -28,3 +29,4 @@ test-suite specs
, mtl == 2.1.*
, hspec >= 1.3
, hspec-expectations >= 0.3
, network >= 2.3
2 changes: 1 addition & 1 deletion spec/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ spec = do
describe "The Server" $ do
describe "default port" $ do
it "is 6667" $ do
let testObject = server
let testObject = mcferkinServer
(port testObject) `shouldBe` 6667
4 changes: 2 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import Server

main :: IO ()
main = do
let myServer = server
print $ show myServer
let server = mcferkinServer
startServer server
31 changes: 27 additions & 4 deletions src/Server.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Server ( Server, port, server ) where
module Server ( Server, port, mcferkinServer, startServer ) where
import Network.Socket

type Port = Int
type Port = PortNumber

defaultPort :: Port
defaultPort = 6667
Expand All @@ -9,5 +10,27 @@ data Server = Server {
port :: Port
} deriving ( Show )

server :: Server
server = Server{ port=defaultPort }
mcferkinServer :: Server
mcferkinServer = Server{ port=defaultPort }

startServer :: Server -> IO ()
startServer server = do
-- create socket
sock <- socket AF_INET Stream 0
-- make socket immediately reusable - eases debugging.
setSocketOption sock ReuseAddr 1
bindSocket sock (SockAddrInet (port server) iNADDR_ANY)
-- allow a maximum of 2 outstanding connections
listen sock 2
mainLoop sock

mainLoop :: Socket -> IO ()
mainLoop sock = do
conn <- accept sock
runConn conn
mainLoop sock

runConn :: (Socket, SockAddr) -> IO ()
runConn (sock, _) = do
_ <- send sock "Hi!\n"
sClose sock

0 comments on commit f01122a

Please sign in to comment.