Home > Software engineering >  Create haskel socket client
Create haskel socket client

Time:06-16

I try to connect to created socket by haskell:

-- Logger
  putStrLn "Create socket"
  sock_logger <- socket AF_INET Stream 0     
  putStrLn "Bind socket"           
  connect sock_logger (SockAddrInet 5000 0)    
  putStrLn "Listen socket"  
  listen sock_logger 2                                  
  putStrLn "Logger listening on port 5000..."
  (conn_logger, _) <- accept sock_logger                       
  putStrLn "Logger connection accepted"

Scocket already created by other application. But after connect sock_logger (SockAddrInet 5000 0) i get

helloworld-exe.EXE: Network.Socket.connect: <socket: 440>: failed (Cannot assign requested address (WSAEADDRNOTAVAIL))

How can i connect to socket as client (without creation new socket) All sockets i create with SockAddrInet 5000 0. Can it depends on addlress (maybe need 127.0.0.1 instead localhost?)

CodePudding user response:

Perhaps you're looking for tupleToHostAddress, as in

connect sock_logger (SockAddrInet 5000 (tupleToHostAddress (127, 0, 0, 1)))

CodePudding user response:

Thx Daniel Wagner, it's really need to use tupleToHostAddress (127, 0, 0, 1) and listen ... is wrong. Right version:

  putStrLn "Create socket"
  sock_logger <- socket AF_INET Stream 0     
  putStrLn "Bind socket"           
  connect sock_logger (SockAddrInet 5000 (tupleToHostAddress (127, 0, 0, 1)))                      
  putStrLn "Connected to logger on port 5000..."
  • Related