Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import Network.Socket hiding (send, sendTo, recv, recvFrom)
- import Network.Socket.ByteString
- import Text.ProtocolBuffers.Header (uFromString)
- import Text.ProtocolBuffers.WireMessage (messagePut)
- import Data.ByteString as BS
- import Data.ByteString.Lazy as LBS
- import Data.Sequence (fromList)
- import AddressBookProtos.Person
- import AddressBookProtos.Person.PhoneNumber
- import AddressBookProtos.Person.PhoneType
- opensocket :: HostName -> String -> IO (Socket, AddrInfo)
- opensocket hostname port =
- do -- Look up the hostname and port. Either raises an exception
- -- or returns a nonempty list. First element in that list
- -- is supposed to be the best option.
- addrinfos <- getAddrInfo Nothing (Just hostname) (Just port)
- let serveraddr = Prelude.head addrinfos
- -- Establish a socket for communication
- sock <- socket (addrFamily serveraddr) Datagram defaultProtocol
- -- Save off the socket, and server address in a handle
- return (sock, serveraddr)
- john :: Person
- john = Person {
- AddressBookProtos.Person.id = 1234,
- name = uFromString "John Doe",
- email = Just $ uFromString "jdoe@example.com",
- phone = fromList [
- PhoneNumber {
- number = uFromString "555-4321",
- type' = Just HOME
- }
- ]
- }
- johnLBS :: LBS.ByteString
- johnLBS = messagePut john
- lazyToStrictBS :: LBS.ByteString -> BS.ByteString
- lazyToStrictBS x = BS.concat $ LBS.toChunks x
- main::IO()
- main = do (sock, addr) <- opensocket "localhost" "4567"
- _ <- sendTo sock (lazyToStrictBS johnLBS) (addrAddress addr)
- return ()
Add Comment
Please, Sign In to add comment