<div dir="ltr"><div>hGetContents reads a handle until EOF, so you've implement a deadlock. The server is waiting to read a second line, but the client is waiting for the server to close the socket in order to do the first print. Perhaps you meant to use hGetLine instead?</div>
<div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, May 21, 2014 at 12:15 AM, yang.zhao <span dir="ltr"><<a href="mailto:hckjsnzf@gmail.com" target="_blank">hckjsnzf@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">omg, i test it on Windows, it's ok now. GHC is 7.6.3.<br>i really don't konw why this happen..<br>
<br>but there is another question on Windows<div class=""><br><br>main = withSocketsDo $ do<br>    s <- listenOn $ PortNumber $ fromIntegral port<br>
    putStrLn "Listening..."<br>    (h, _, _) <- accept s<br>    putStrLn "After accept"<br>    sline <- hGetLine h<br></div>    putStrLn $ "get line from handle"++sline<br>    hPutStrLn h "xxxx...."<br>

    putStrLn "send first done"<br>    <br>    --second recv<br>    slinea <- hGetLine h<br>    putStrLn $ "get line from handle"++slinea<br><br>    hPutStrLn h "yyyy....."<br>    putStrLn "send second done"<br>

<br>    threadDelay 1000000<br>--<br>cli:<div class=""><br>main = withSocketsDo $ do<br>    h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port<br>    putStrLn "After connect"<br></div>    hPutStrLn h "xxx"<br>

    putStrLn "put first to handle done"<div class=""><br>    bs <- hGetContents h<br>    putStrLn "read from handle done "<br>    putStrLn bs<br><br></div>    --second send<br>    hPutStrLn h "yyy"<br>
    putStrLn "put second to handle done"<br>
    bfs <- hGetContents h<div class=""><br>    putStrLn "read from handle done "<br>    putStrLn bs<br> <br><br></div>the second send and recv is blocking...<br>server's output:<br>Listening...<br>After accept<br>
get line from handlexxx<br>
send first done<br><br>client's output:<br>After connect<br>put first to handle done<br>read from handle done<br>xxx....<br><br><br>..... T_T<br><br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">

2014-05-21 14:10 GMT+08:00 yang.zhao <span dir="ltr"><<a href="mailto:hckjsnzf@gmail.com" target="_blank">hckjsnzf@gmail.com</a>></span>:<div><div class="h5"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr">Here's my new code:<br><br>$ cat serv.hs<div><br>import Control.Concurrent<br>import System.IO<br>import Network<br><br>port :: Int<br>port = 1234<br><br>main :: IO ()<br>main = withSocketsDo $ do<br>
    s <- listenOn $ PortNumber $ fromIntegral port<br></div>
    putStrLn "Listening..."<div><br>    (h, _, _) <- accept s<br></div>    putStrLn "After accept"<br>    sline <- hGetLine h<br>    putStrLn "get line from handle"<div>
<br>    hPutStrLn h sline<br>    putStrLn $ "send "++sline<br>
    threadDelay 1000000<br><br>    hClose h<br>    sClose s<br><br></div>--------<br>$ cat clie.hs<div><br>import System.IO<br>import Network<br><br>port :: Int<br>port = 1234<br><br>main :: IO ()<br>main = withSocketsDo $ do<br>

</div>    h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port<br>
    putStrLn "After connect"<br>    hPutStrLn h "hello"<br>    putStrLn "put hello to handle done"<br>    bs <- hGetContents h<br>    putStrLn "read from handle done"<br>    putStrLn bs<br>


    hClose h<br><br><br>===<br>one terminal, run ./serv, then run ./clie in another terminal. Output is :<br><br>$ ./serv <br>Listening...<br>After accept<br><br>$ ./clie <br>After connect<br>put hello to handle done<br>

read from handle done<br>
<br><br>it seems that client has read from then handle, but donesn't read anything, then block.<br>and server donesn't receive anything, still wait for something...<br><br>ghc version is 7.4.1, because of this?..<br>


</div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-05-21 13:57 GMT+08:00 Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span>:<div><div>
<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora 20 and it worked fine there as well (both with runhaskell or compiled with -O).<div><br></div><div>I might start adding putStrLn statements to the code to see where it's unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in case the issue is a DNS misconfiguration.</div>



<div><br></div></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, May 20, 2014 at 10:51 PM, yang.zhao <span dir="ltr"><<a href="mailto:hckjsnzf@gmail.com" target="_blank">hckjsnzf@gmail.com</a>></span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">thanks for your replay.<br>i run the two program in two different teriminal for sure.<br><br>it works for you?<br>



but why can't run well on my computer.<br><br>make me creazy....<br></div><div class="gmail_extra">
<br><br><div class="gmail_quote">2014-05-21 13:47 GMT+08:00 Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span>:<div><div><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div dir="ltr">How precisely are you trying to run the client? Are you typing it in to the same terminal? If so, then the client is never actually started, because when you type ./cli the input is going to ./serv and not the shell. Try running the client in a separate terminal.<div>





<br></div><div>It works for me here on Mac OS X 10.9.2 with GHC 7.8.2.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Tue, May 20, 2014 at 10:42 PM, yang.zhao <span dir="ltr"><<a href="mailto:hckjsnzf@gmail.com" target="_blank">hckjsnzf@gmail.com</a>></span> wrote:<br>





</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">hi guys,<br>I'm newbie, just begin to learn Haskell.<br>now i write a very simple server and client .<br>





<br>Server:<br>import Control.Concurrent  <br>import System.IO           <br>import Network             <br>
                           <br>port :: Int                <br>port = 1234                <br>                           <br>main :: IO ()              <br>main = withSocketsDo $ do  <br>    s <- listenOn $ PortNumber $ fromIntegral port<br>






    (h, _, _) <- accept s  <br>                           <br>    sline <- hGetLine h    <br>    hPutStrLn h sline      <br>    putStrLn $ "send "++sline<br>    threadDelay 1000000    <br>                           <br>






    hClose h               <br>    sClose s <br><br>Client :<br>import System.IO<br>import Network<br> <br>port :: Int<br>port = 1234<br> <br>main :: IO ()<br>main = withSocketsDo $ do<br>    h <- connectTo "localhost" $ PortNumber $ fromIntegral port<br>






    hPutStrLn h "hello"<br>    bs <- hGetContents h<br>    putStrLn bs<br>    hClose h<br> <br clear="all"><br>And, it doesn't work now . I run ./serv , then run ./cli , they will block all the time. <br>






but,  when i run ./serv, and telnet localhost 1234 in another terminal, it works fine.<br>so i don't know what's the wrong with my code.<br>anybody can tell me about my problem?<br><br>os is Debian 7,  haskell-platform  2012.2.0.0<br>






<br>thanks a lot!!!<span><font color="#888888"><br>-- <br>K.I.S.S.
</font></span></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div></div></div><span><font color="#888888"><br><br clear="all"><br>-- <br>K.I.S.S.
</font></span></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div></div></div><span><font color="#888888"><br><br clear="all"><br>-- <br>K.I.S.S.
</font></span></div>
</blockquote></div></div></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><br>-- <br>K.I.S.S.
</font></span></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>