Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:27:27+00:00 2026-05-25T02:27:27+00:00

I have played a bit with the Network library, the simple form where the

  • 0

I have played a bit with the Network library, the “simple” form where the use is fairly easy: a combination of ‘listenOn’, ‘connectTo’, ‘accept’ and we have something working. Now I am trying to use the “real” thing, meaning Network.Socket and Network.Socket.ByteString because I’d like to send files between a client and a server. But it is not as easy to use as the higher level interface and I’m looking for some code examples and/or how-tos. Particularly, the code dealing with ‘getAddrInfo’, ‘AddrInfo’ and ‘SockAddr’ I don’t understand very deeply.
So, where can I get this type of resource?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:27:27+00:00Added an answer on May 25, 2026 at 2:27 am

    I can just show an own example:

    import Network.Socket
      ( SocketType(..), AddrInfo(..), AddrInfoFlag(..), Family(..), socket, sClose, 
        defaultHints, withSocketsDo, connect, getAddrInfo, defaultProtocol )
    import Network.Socket.ByteString( sendAll, recv )
    import qualified Data.ByteString.Char8 as BC( readFile, hPut, null )
    
    sendFile :: String -> Int -> FilePath -> IO Bool
    sendFile server port filename = withSocketsDo $ do
      addrinfos <- getAddrInfo desiredAddr (Just server) (Just . show $ port)
      if null addrinfos
        then do
          return False
        else do
          datafile <- BC.readFile filename
          let serveraddr = head addrinfos
          sock <- socket (addrFamily serveraddr) Stream defaultProtocol
          connect sock (addrAddress serveraddr)
          sendAll sock datafile
          sClose sock
          return True
    

    I don’t test (just cut from my code file) so maybe fails in the imports. On the server I use:

    readerThread :: MyQueue -> Socket -> IO ()
    readerThread queue serverSock = do
      (connsock, clientaddr) <- accept serverSock
      sClose serverSock
      putStrLn $ "> connected reader " ++ show clientaddr
      talk connsock
      sClose connsock
      putStrLn $ "> closed reader " ++ show clientaddr
      return ()
        where
          talk conn = do 
            msg <- recv conn 2048
            putStr $ "* get from " ++ show conn ++ "\n"
            myQueueWrite queue msg
            unless (B.null msg) $ do 
              talk conn
    
    createInputPort :: ChildLocks -> MyQueue -> AddrInfo -> IO PortNumber
    createInputPort children obuffer serverAddr = withSocketsDo $ do
      serverSock <- socket AF_INET Stream defaultProtocol
      bindSocket serverSock (addrAddress serverAddr)
      listen serverSock 1
    
      lock <- newChildLock children
      _ <- forkIO $ readerThread obuffer serverSock `finally` endChildLock lock
      socketPort serverSock
    

    Y use a own queue (TCHAN) to comunicate between network reader an consumer thread, and childLocks to wait at the end with something like waitForChildren children construct that I found on stackoverflow.

    UPDATE: the waitForChildren is on post Comparing Haskell threads to kernel threads – is my benchmark viable?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple web service sitting on our internal network. I used SOAPUI
I have a home network which I access remotely quite a bit. But I'm
Intrigued by this question , I have played a bit with parallel assignment with
Just a curiosity, I have played around with prototyping a bit, but it seems
I have played with the idea of using a wiki (MediaWiki) to centralize all
I have played around with smart phone development (windows ce), and it seemed pretty
I have played for a while writing XPath but am unable to come up
I have played around with Scala for a while now, and I know that
While I have played with parts of Qt in the past I am thinking
Using asp.net 3.5 Gridview control, visual studio 2008. I have played with all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.