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 cc88fa3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions mcferkincaht.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build-type: Simple

executable program1
build-depends: base == 4.5.*
, network >= 2.3
main-is: Server.hs
ghc-options:
-Wall -Werror
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
33 changes: 29 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,29 @@ 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
-- listen on TCP port 4242
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
-- accept one connection and handle it
conn <- accept sock
runConn conn
mainLoop sock

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

0 comments on commit cc88fa3

Please sign in to comment.