{"id":294,"date":"2024-07-28T21:37:17","date_gmt":"2024-07-28T16:07:17","guid":{"rendered":"https:\/\/dctn.in\/?p=294"},"modified":"2024-07-28T21:37:17","modified_gmt":"2024-07-28T16:07:17","slug":"tod-service-time-of-the-day","status":"publish","type":"post","link":"https:\/\/dctn.in\/index.php\/blog\/tod-service-time-of-the-day\/","title":{"rendered":"TOD Service &#8211; Time of the Day"},"content":{"rendered":"\n<p><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">Writing a basic Time-of-the-Day (TOD) server is a good experiment for learning basic network programming concepts. A TOD server listens for incoming network connections and responds with the current date and time.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to Implement a TOD Service on a Server<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a Socket<\/strong>: Set up a TCP socket to listen for incoming connections.<\/li>\n\n\n\n<li><strong>Bind the Socket<\/strong>: Bind the socket to a specific IP address and port.<\/li>\n\n\n\n<li><strong>Listen for Connections<\/strong>: Set the socket to listen for incoming connection requests.<\/li>\n\n\n\n<li><strong>Accept Connections<\/strong>: Accept incoming client connections and handle them.<\/li>\n\n\n\n<li><strong>Send the Time<\/strong>: Send the current date and time to the connected client.<\/li>\n\n\n\n<li><strong>Close the Connection<\/strong>: Close the connection after sending the data.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Code snippet<\/h3>\n\n\n\n<p>complete example of a simple ToD service written in C can be found at below <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;string.h>\n#include &lt;unistd.h>\n#include &lt;arpa\/inet.h>\n#include &lt;time.h>\n\n#define PORT 45000  \/\/ The port number on which the ToD service will listen\n#define BUFFER_SIZE 128  \/\/ variable size buffer\n\nint main() {\n    int server_fd, client_fd;\n    struct sockaddr_in server_addr, client_addr;\n    socklen_t client_addr_len = sizeof(client_addr);\n    char buffer&#91;BUFFER_SIZE];\n    time_t current_time;\n    struct tm *local_time;\n    int bytes_sent;\n\n    \/\/ Create a socket\n    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {\n        perror(\"socket\");\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ Prepare the sockaddr_in structure\n    server_addr.sin_family = AF_INET;\n    server_addr.sin_addr.s_addr = INADDR_ANY;  \/\/ Bind to all available interfaces including lo interface \n    server_addr.sin_port = htons(PORT);  \/\/ Port number in network byte order\n\n    \/\/ Bind the socket to the address and port\n    if (bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) == -1) {\n        perror(\"bind\");\n        close(server_fd);\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ Listen for incoming connections\n    if (listen(server_fd, 5) == -1) {\n        perror(\"listen\");\n        close(server_fd);\n        exit(EXIT_FAILURE);\n    }\n\n    printf(\"Server listening for CLIENT request on port %d...\\n\", PORT);\n\n    while (1) {\n        \/\/ Accept an incoming connection\n        if ((client_fd = accept(server_fd, (struct sockaddr *)&amp;client_addr, &amp;client_addr_len)) == -1) {\n            perror(\"accept\");\n            close(server_fd);\n            exit(EXIT_FAILURE);\n        }\n\n        \/\/ Get the current time\n        current_time = time(NULL);\n        local_time = localtime(&amp;current_time);\n\n        \/\/ copy the formatted time into buffer\n        snprintf(buffer, BUFFER_SIZE, \"Current time: %s\", asctime(local_time));\n\n        \/\/ Send copied buffer to the client\n        bytes_sent = send(client_fd, buffer, strlen(buffer), 0);\n        if (bytes_sent == -1) {\n            perror(\"send\");\n        }\n\n        \/\/ current client requested connection closed\n        close(client_fd);\n    }\n\n    \/\/ server socket is closed\n    close(server_fd);\n    return 0;\n}\n<\/code><\/code><\/pre>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"143\" src=\"https:\/\/dctn.in\/wp-content\/uploads\/2024\/07\/timer_output.gif\" alt=\"\" class=\"wp-image-298 size-full\"\/><\/figure><div class=\"wp-block-media-text__content\">\n<p>Tick Tick Tick &#8230;<\/p>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">simplify above code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Socket Creation<\/strong>: <code>server_fd = socket(AF_INET, SOCK_STREAM, 0); <\/code>Creates a TCP socket. <br>Wondering why we didn&#8217;t choose UDP sockets ? <\/li>\n\n\n\n<li><strong>Binding<\/strong>: <code>server_addr.sin_family = AF_INET;<\/code><br>               <code>server_addr.sin_addr.s_addr = INADDR_ANY; <\/code><br>        <code>    server_addr.sin_port = htons(PORT); <\/code><br><br>             <code>bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)); <\/code><br>Binds the socket to all available interfaces on the specified port.<\/li>\n\n\n\n<li><strong>Listening<\/strong>: <code>listen(server_fd, 5); <\/code>Sets the socket to listen for incoming connections, with a backlog queue size of 5 <br>use below command on Linux to know more about listen system call<br>man listen<\/li>\n\n\n\n<li><strong>Accepting Connections<\/strong>: <code>client_fd = accept(server_fd, (struct sockaddr *)&amp;client_addr, &amp;client_addr_len); <\/code><br>Accepts an incoming connection from a client.<\/li>\n\n\n\n<li><strong>Sending the Time<\/strong>: <code>current_time = time(NULL); <\/code><br><code>local_time = localtime(&amp;current_time); <\/code><br><code>snprintf(buffer, BUFFER_SIZE, \"Current time: %s\", asctime(local_time)); send(client_fd, buffer, strlen(buffer), 0); <\/code><br>Retrieves the current time, formats it, and sends it to the client.<\/li>\n\n\n\n<li><strong>Closing Connections<\/strong>: <code>close(client_fd); close(server_fd); <\/code><br>Closes the client connection and server socket.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">run services on server <\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Compile the Code<\/strong>  <code>gcc -o tod_daemon tod_server.c<\/code><\/li>\n\n\n\n<li><strong>Run the Server<\/strong>: <code>.\/tod<\/code>_<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">daemon<\/mark> use &#8220;netstat -tulpen&#8221; command to check\/verify whether process is listening on the port or not  <\/li>\n\n\n\n<li><strong>Connect to the Server<\/strong>: Send client request to fetch the ToD and You should see the current date and time displayed by the server.<\/li>\n<\/ol>\n\n\n\n<p>Please write your own TOD client in C language to test the ToD service.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing a basic Time-of-the-Day (TOD) server is a good experiment for learning basic network programming concepts. A TOD server listens for incoming network connections and responds with the current date and time. Steps to Implement a TOD Service on a Server Code snippet complete example of a simple ToD service written in C can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-blog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":4,"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions\/299"}],"wp:attachment":[{"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dctn.in\/index.php\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}