Skip to content

Instantly share code, notes, and snippets.

@AryanGodara
Created July 21, 2023 21:04
Show Gist options
  • Save AryanGodara/91a4dd610d9fb12e62e213db8b8b7b73 to your computer and use it in GitHub Desktop.
Save AryanGodara/91a4dd610d9fb12e62e213db8b8b7b73 to your computer and use it in GitHub Desktop.
udpServer/bin/main.ml
let () =
print_endline "Launching UDP Server...";
let () = Logs.set_reporter (Logs.format_reporter ()) in
let () = Logs.set_level (Some Logs.Info) in
let server_socket = Server.create_socket () in
Lwt_main.run Server.(create_server server_socket)
(*
Let's go through the code line by line:
1. `let () =`: This is a self-executing function (unit expression) that is used to perform actions at the program's start-up.
2. `let () = Logs.set_reporter (Logs.format_reporter ()) in`: This sets up the logging mechanism using the Logs library. It uses the `format_reporter` to specify the format in which the log messages will be displayed.
3. `let () = Logs.set_level (Some Logs.Info) in`: This line sets the log level to `Logs.Info`, which means that only log messages with an "Info" level or higher will be displayed. Lower level log messages like "Debug" will be ignored.
4. `let server_socket = Server.create_socket () in`: This line creates a UDP socket by calling the `create_socket` function from the `Server` module and assigns it to the `server_socket` variable.
7. `Lwt_main.run Server.(create_server server_socket)`: This line initiates the Lwt event loop using `Lwt_main.run`. It calls the `create_server` function from the `Server` module, passing the `server_socket` as an argument. This starts the UDP server, and the program will continue running until the server is terminated or an error occurs.
In summary, this code sets up a UDP server by creating a UDP socket, setting up the logging mechanism, and then starting the server to listen for incoming datagrams. The server will continue running in an event loop, processing requests and sending responses to clients until it is terminated or encounters an error.
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment