-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTTTClient.hs
54 lines (40 loc) · 1.29 KB
/
TTTClient.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Network.Socket
import Network.BSD
import System.IO
import TTTServer
import TicTacToe
playGame :: HostName -> String -> IO ()
playGame hostname port = do
p <- getPlayerFromConnection hostname port
playersTurn p
return ()
playersTurn :: Player -> IO ()
playersTurn p = do
m1 <- hGetLine $ handle p
putStrLn $ "did reveive msg from server: " ++ m1
col <- getLine
hPutStrLn (handle p) col
m2 <- hGetLine $ handle p
putStrLn $ "did reveive msg from server: " ++ m2
row <- getLine
hPutStrLn (handle p) row
playersTurn p
return ()
getPlayerFromConnection :: HostName -> String -> IO Player
getPlayerFromConnection hostname port = do
-- | create the server's address using the input parameters
addrinfos <- getAddrInfo Nothing (Just hostname) (Just port)
let serveraddr = head addrinfos
putStrLn $ "did create server address: " ++ show serveraddr
-- | create a TCP socket for the incoming data
sock <- socket (addrFamily serveraddr) Stream defaultProtocol
-- | configure socket
setSocketOption sock KeepAlive 1
-- | connect to server
connect sock (addrAddress serveraddr)
-- | turn socket into handle
h <- socketToHandle sock ReadWriteMode
hSetBuffering h LineBuffering
info <- hGetLine h
putStrLn $ "initial info: " ++ info
return $ Player h Cross