Skip to content

Instantly share code, notes, and snippets.

@AryanGodara
Created July 21, 2023 21:05
Show Gist options
  • Save AryanGodara/9f4187e3ee06a6a9b8ec3ad18a1cf6b1 to your computer and use it in GitHub Desktop.
Save AryanGodara/9f4187e3ee06a6a9b8ec3ad18a1cf6b1 to your computer and use it in GitHub Desktop.
udpServer/bin/server.ml
open Lwt.Infix
(* Shared mutable counter *)
let counter = ref 0
(* This defines a shared mutable counter initialized to 0 using a reference cell. *)
let listen_address = Unix.inet_addr_loopback
(* This sets the listen_address to the loopback IP address (127.0.0.1), indicating
that the server will listen for connections from the local machine only. *)
let port = 9000
(* This sets the port to 9000, the port number on which the server will listen
for incoming UDP packets. *)
let backlog = 10
(* This sets the backlog value to 10, representing the number of pending
connections the server can keep in the queue before accepting them. *)
(* This function handle_message takes a message (msg) as input and
returns a response based on the message. It handles different
commands like "quit" (returns "quit"), "read" (returns the current
value of the shared counter as a string), "inc" (increments the
shared counter and returns a response), and other unknown
commands (returns "Unknown command"). *)
let handle_message msg =
print_endline ("Received message in handle_message: " ^ msg);
match msg with
| "quit" -> "quit"
| "read" -> string_of_int !counter
| "inc" ->
counter := !counter + 1;
"Counter has been incremented"
| _ -> "Unknown command"
(* This function handle_request takes a server socket (server_socket)
as input and recursively listens for incoming UDP packets from
clients. It processes the received messages using the handle_message
function and sends appropriate responses back to the clients. *)
let rec handle_request server_socket =
print_endline "Waiting for request";
let buffer = Bytes.create 1024 in
print_endline "just created buffer";
server_socket >>= fun server_socket ->
print_endline "just got server_socket";
Lwt_unix.recvfrom server_socket buffer 0 1024 []
>>= fun (num_bytes, client_address) ->
print_endline "Received request";
let message = Bytes.sub_string buffer 0 num_bytes in
print_endline ("Received message in handle_request: " ^ message);
let reply = handle_message message in
match reply with
| "quit" ->
print_endline "Quitting Server...";
Lwt_unix.sendto server_socket (Bytes.of_string reply) 0
(String.length reply) [] client_address >>= fun _ ->
Lwt.return ()
| _ ->
print_endline ("Replying with: " ^ reply);
Lwt_unix.sendto server_socket (Bytes.of_string reply) 0
(String.length reply) [] client_address
>>= fun _ ->
print_endline "Reply sent";
handle_request (Lwt.return server_socket)
(* This function create_server takes a server socket (sock) as input
and initiates the handle_request process for that socket. *)
let create_server sock =
print_endline "Creating server";
handle_request sock
(* This function create_socket creates a UDP socket, binds it to the
listen_address and port, and returns the socket. *)
let create_socket () : Lwt_unix.file_descr Lwt.t =
print_endline "Creating socket";
let open Lwt_unix in
let sock = socket PF_INET SOCK_DGRAM 0 in
bind sock @@ ADDR_INET (listen_address, port) >>= fun () -> Lwt.return sock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment