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

  • Home
  • SEARCH
  • 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 7646865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:19:08+00:00 2026-05-31T10:19:08+00:00

import Network import System.IO import Control.Concurrent import Control.Monad(when) import Char serverHandshake :: String serverHandshake

  • 0
import Network
import System.IO
import Control.Concurrent
import Control.Monad(when)
import Char

serverHandshake :: String
serverHandshake = 
 "HTTP/1.1 101 Switching Protocols\r\n\
 \Upgrade: WebSocket\r\n\
 \Connection: Upgrade\r\n\
 \Sec-WebSocket-Accept: *******************\r\n\r\n"

acceptLoop socket = forever $ do
 (h,_,_) <- accept socket
 ------------------------
 hPutStr h serverHandshake
 hSetBuffering h NoBuffering
 forkIO (listenLoop h)  
 where
  forever a = do a; forever a

main = withSocketsDo $ do
 h <- listenOn (PortNumber 8000)
 acceptLoop h
 sClose h
 return ()

listenLoop :: Handle  -> IO ()
listenLoop h = do
 sendFrame h "aleloia"
 msg <- readFrame h
 putStrLn msg
 when (msg /= "quit") (listenLoop h)

readFrame :: Handle -> IO String
readFrame h = readUntil h ""
 where
  readUntil h str = do
   new <- hGetChar h
   if new == chr 0
    then readUntil h ""
    else if new == chr 255
     then return str
     else readUntil h (str ++ [new])

sendFrame :: Handle -> String -> IO ()
sendFrame h s = do
 hPutChar h (chr 0)
 hPutStr h s
 hPutChar h (chr 255)

1) why use “forever a = do a; forever a” when there is “forkIO (listenLoop h)”
I read it as forever start forking a new listenLoop for 1 incoming connection? In other words start forking new processes until my pc crashes? If there would be a forever loop I expect it to be in the main somewhere?

2) – – – and * * * is some not yet implemented RFC stuff I could not find on the internet, are there haskell batteries for this?

ref: http://www.fatvat.co.uk/2010/01/web-sockets-and-haskell.html

  • 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-31T10:19:09+00:00Added an answer on May 31, 2026 at 10:19 am

    Presumably accept is a blocking function call, so the use of forever isn’t a fork-bomb. As for why you do this, well, I think it’s fairly standard practice for a program that listens on a socket to accept as many connections as are requested of it.

    I’m not 100% sure whether there are “Haskell batteries” for exactly the missing parts of your code, but there are definitely several packages on Hackage with websocket in their name or description; you should check those out and then post a more specific question.

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

Sidebar

Related Questions

import Network.Socket import Control.Monad import Network import System.Environment (getArgs) import System.IO import Control.Concurrent (forkIO)
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
import wx class bucky(wx.Frame): # Creating the outer window/frame def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'Karls Network Tool',
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
I'm trying to figure out how make a basic GET request using Network.HTTP.simpleHTTP that
import sys words = { 1 : 'one', 2 : 'two', 3 : 'three',
import sys sys.path.append('/home/myuser/svn-repos/myproject') from myproject.settings import * But, it says module not found when
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>
import threading event = threading.Event() event.set() print event.wait(1) None event.clear() print event.wait(1) None So

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.