I've been looking to learn functional programming on and off for a few years now. Mainly I've looked into Haskell, but I've always gotten stuck. My guess is that I'm just not smart enough for such an academic language. I get stuck with trying to understand certain concepts, and all of a sudden I'm reading about category theory without understanding anything.
But the other day I decided I'd look into dotnet core and the F# language. And for some reason I find it much more accessible.
Maybe it's just that the resources on the web are more accessible for someone like me, who doesn't have a strong background in maths.
Anyways I set out to create a simple lite tcp socket server that would just write something out to the client that connects and then close the connection.
And heres what I ended up with:
module SocketServer
open System
open System.Net
open System.Net.Sockets
open System.Threading.Tasks
let writeToSocket(socket:Socket) =
let stream = new NetworkStream(socket)
let bytes = "Hello World\n"B
stream.Write(bytes, 0, bytes.Length)
stream.Dispose()
socket.Dispose()
let startListening port =
let local = IPAddress.Parse "127.0.0.1"
let listener = new TcpListener(localaddr=local, port=port)
listener.Start()
printfn "%i is the port" port
while true do
Console.WriteLine "Waiting for connection..."
listener.Server.Accept() |> writeToSocket
You can checkout the code above here, I'll keep on hacking on this server and see what I can do with it.
In the mean time if you're interested in looking into F# I highly recommend reading through some or all of the posts on the site [fsharpforfunandprofit.com](https://fsharpforfunandprofit.com/" "_target=blank)
I must warn you though, there aren't that many examples on the web that helps you when you want to mess around with the dotnet core api's with F#. You're pretty much alone with that.