haskell - Reading from a Socket while keeping the connection open -


currently have code creates server , client testing purposes.

server code

import network.socket import network.socket.bytestring nsb import network.socket.bytestring.lazy nsbl import data.bytestring.lazy bsl import data.bytestring bs import data.hashmap hm import system.io io  main = withsocketsdo $   sock <- socket af_inet stream 0   setsocketoption sock reuseaddr 1   bindsocket sock (sockaddrinet (frominteger 8585) inaddr_any)   listen sock 100   servloop sock  servloop sock =   client = accept sock   io.putstrln "got connection"   h <- sockettohandle client readwritemode   hsetbuffering h nobuffering   req <- bsl.hget h 1024   io.putstrln "got contents:"   io.putstrln $ show req 

client code

import network.socket import network.bsd import control.monad import system.io io import data.text t import data.bytestring.lazy bsl import data.messagepack mp  main = withsocketsdo $   sock <- socket af_inet stream 0   setsocketoption sock reuseaddr 1   addr <- liftm hostaddresses $ gethostbyname "localhost"   connect sock $ sockaddrinet (frominteger 8585) (prelude.head addr)   handle <- sockettohandle sock readwritemode   replicatem_ 5 $ bsl.hput handle $ mp.pack ("hello host" :: text)   hflush handle   getline   replicatem_ 5 $ bsl.hput handle $ mp.pack ("hello host" :: text)   hflush handle   hclose handle 

the observed behaviour message not sent until hclose handle called on client side. however, keep handle open sending more requests , receiving responses rapidly. going right way? , if so, there way keep handle open read , write socket?

hget keeps trying read until has many bytes asked for, namely 1024. want use hgetnonblocking instead. or like:

do     r1 <- bsl.hget h 1     rrest <- bsl.hgetnonblocking h 1024     return (r1 bs.append rrest) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -