TCP Server and Client in OCaml

Aryan Godara
7 min readAug 13, 2023

--

In this tutorial, I’ll show you how to write a TCP Client and a TCP Server in OCaml using socket programming concepts. So let’s hop in.

OCaml: Functional Programming Language

This technical article explores socket programming in OCaml to build a TCP server and a TCP client. Leveraging the LWT library, we construct robust network applications capable of handling asynchronous I/O operations with ease. TCP’s connection-based nature makes it ideal for most scenarios. We’ll cover socket creation, addressing, and binding for robust network communication. LWT, a powerful tool for concurrent programming, allows us to write non-blocking code for handling multiple tasks simultaneously.

Unlike UDP, TCP establishes a connection between the client and the server before data transmission begins. In TCP communication, a three-way handshake process is used to establish this connection, ensuring reliable and ordered delivery of data. The client initiates the handshake by sending a SYN packet, the server responds with a SYN-ACK packet, and the client acknowledges with an ACK packet, thus establishing a connection. Unlike UDP, where datagrams are sent without connection establishment, TCP ensures a reliable data stream by acknowledging received segments and retransmitting any lost segments. This connection-oriented nature of TCP allows for error detection, flow control, and reordering of data. Both UDP and TCP are fundamental communication protocols, each serving distinct purposes in facilitating data transport between clients and servers. In this context, we will leverage the Ubuntu operating system, providing a versatile environment for developing TCP client-server programs.

Implementation of TCP Client-Server Program:

TCP Server:

  1. Create a TCP socket: Create a TCP socket using the socket API. This socket will be used to listen for incoming connections from clients.
  2. Bind the socket to the server’s address: Bind the socket to a specific IP address and port number on the server. This allows the server to listen for incoming connection requests on that address and port.
  3. Listen for incoming connections: Start listening for incoming connections on the bound socket. The server will wait for clients to establish connections.
  4. Accept client connections: When a client initiates a connection, the server uses the accept function to accept the connection request. This function returns a new socket that represents the established connection between the server and the client.
  5. Receive and process data: Once a connection is established, the server can use the new socket to receive data sent by the client. The server can then process the received data as needed.
  6. Formulate a response: Based on the received data, the server can formulate a response that it intends to send back to the client.
  7. Send the response: The server uses the connection socket to return the formulated response to the client.
  8. Close the connection: After sending the response, the server can close the connection socket to indicate that the data transmission is complete.
  9. Return to Step 4: The server can loop back to step 4 to continue waiting for new incoming connections. This allows the server to handle multiple clients sequentially.

These steps outline the process of creating a TCP server that listens for incoming connections, receives data from clients, processes that data, sends responses back to the clients, and then continues waiting for more connections. Just like with the UDP example, the exact implementation of these steps will depend on the programming language and libraries you are using to develop your TCP server.

TCP Client:

Use telnet to send requests to the TCP client. It’s very simple really, as we’ll see in the upcoming sections.

The Code :-

Your project directory will look like this. {{WORKDIR}} is the project root.

{{WORKDIR}}/
tcpServer/
_build/
bin/
dune
server.ml
main.ml
...

{{WORKDIR}}/tcpServer/bin/main.ml

{{WORKDIR}}/tcpServer/bin/server.ml

Running the Code :-

# Open two terminal windows
# 1. Inside {{WORKDIR}}/udpServer/
# 2. To run telnet, maybe in the home directory
# 1. Inside the first terminal (for the Server):-
$ dune build # Compiles code into a binary
$ ./_build/default/bin/main.exe # Launches the Server
# 2. Inside the first terminal (for the Client):-
$ telnet localhost 9000. # Open connection on port 9000
# Messages that can be sent from the client :-
# "read" : Prints the current value of the counter.
# "inc" : Increments the value of the counter by 1.
# "_" (anything else) : Server returns "Unknown Command" response.

The Server responds accordingly to the response received by the Client.

LWT (Light Weight Threads) :-

When developing software, handling various I/O operations becomes a common task. Interacting with the kernel through system calls, reading input from users via keyboards or mice, communicating with graphical servers to build user interfaces, and connecting with other computers over networks are among the numerous resources software may need to interact with.

As this list of interactions grows, managing them all together can become increasingly challenging. Two common approaches have been proposed to address this problem:

1. Using a main loop and integrating all components into it: While this approach can work, it can make writing asynchronous sequential code very complex. For instance, graphical user interfaces may freeze and fail to redraw themselves due to blocking code portions.

2. Using preemptive system threads: Although this approach is possible, handling threads correctly can be difficult. Additionally, system threads consume considerable resources, limiting the number of threads that can be launched simultaneously. Hence, this is not a general solution.

Lwt presents a third alternative by introducing promises, which are highly efficient. A promise is essentially a reference that is filled asynchronously. Calling a function that returns a promise does not require creating a new stack, or process, or incurring any significant overhead. It behaves like a regular and fast function call. Promises can be composed elegantly, allowing developers to write highly asynchronous programs.

In the following sections, we will delve into the concepts of Lwt and explore its main modules, providing insights into how Lwt can simplify and enhance the handling of asynchronous operations in software development.

We use two Lwt operators/functions in this program :-

The >>= (bind) operator in the context of Lwt is used to chain together asynchronous computations represented as promises (Lwt monads). It allows you to sequence multiple asynchronous operations, ensuring that subsequent computations depend on the results of the previous ones.

Signature: 'a Lwt.t -> ('a -> 'b Lwt.t) -> 'b Lwt.t

  • The >>= operator takes two arguments:
  1. An asynchronous computation represented as a promise of type 'a Lwt.t.
  2. A function that takes the result of the first computation and returns a new asynchronous computation of type 'b Lwt.t.
  • The result of the >>= operator is a new promise representing the subsequent computation, which depends on the result of the first computation.

In the context of Lwt, Lwt.return is a function that wraps an immediate value into a promise (Lwt monad). It allows you to lift a regular value into the asynchronous world, making it compatible with Lwt-based computations.

Signature: 'a -> 'a Lwt.t

  • The Lwt.return function takes a value of type 'a and returns a promise ('a Lwt.t) representing that value.

These help us write concurrently working code.

We use two more Lwt constructs here :-

In the context of Lwt, Lwt_main.run is a function that runs the Lwt event loop until the given Lwt promise is fulfilled. It is the entry point for starting and executing Lwt-based asynchronous computations.

Signature: 'a Lwt.t -> 'a

  • The Lwt_main.run function takes a promise of type 'a Lwt.t and executes the Lwt event loop until the promise is fulfilled, returning the final value of type 'a.

In the context of Lwt, Lwt.pick is a function that allows you to choose the first completed promise from a list of Lwt promises. It returns a new promise that resolves with the result of the first completed promise.

Signature: 'a Lwt.t list -> 'a Lwt.t

  • The Lwt.pick function takes a list of promises ('a Lwt.t list) and returns a new promise ('a Lwt.t) that resolves with the result of the first completed promise.

The differences between TCP and UDP :-

UDP and TCP are two widely used Internet protocols for data transfer. TCP is a connection-oriented transport layer protocol ensuring secure and reliable data transmission between connected devices. It establishes a connection before data transfer and provides features like flow control and error control. TCP is utilized by protocols like HTTP and FTP, offering dependable data transmission over networks.

On the other hand, UDP is a connectionless transport layer protocol, allowing rapid data transmission without the overhead of connection setup and maintenance. It is commonly used for real-time data transfer scenarios where transmission delays are unacceptable. UDP’s fast transmission speed makes it suitable for protocols like DNS, DHCP, and RIP.

UDP:

- Connectionless transport layer protocol.
- Rapid data transmission without connection setup and maintenance overhead.
- Used for real-time data transfer when transmission delays are not acceptable.
- Fast transmission speed.
- Commonly used in protocols like DNS, DHCP, and RIP.
- Pros: Low startup delay, broadcasting and multicasting support, ability to manage packet borders, quick transactions like DNS lookup, bandwidth use is high.
- Cons: Packet may not reach its intended recipient or may reach twice, lacks congestion and flow control, routers can be irresponsible in case of collision, and packet loss may occur.

TCP:

  • Connection-oriented transport layer protocol.
    - Ensures secure and reliable data transmission between connected devices.
    - Establishes a connection before data transfer.
    - Provides features like flow control and error control.
    - Used by protocols like HTTP and FTP for dependable data transmission.
    - Pros: Support for multiple routing protocols, operating system independence, excellent scalability in client-server architectures.
    - Cons: Lacks support for broadcast or multicast requests, header size may lead to resource wastage, no assurance of packet delivery provided by the transport layer.

This brings us to the end of this article. Please leave a like if you found this helpful, and leave a comment to tell me how I can improve. All reviews are appreciated :)

--

--

Aryan Godara
Aryan Godara

Written by Aryan Godara

Hi, I'm a student at IIT D, India. My passions include all things tech-related, pc gaming cuz Indian parents don't like gaming consoles :( and playing guitar :3