<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SanjayRV</title>
    <description>The latest articles on DEV Community by SanjayRV (@sanjayrv).</description>
    <link>https://dev.to/sanjayrv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F348448%2F811bb5dc-8940-4df4-8c4d-26ab61f0b117.png</url>
      <title>DEV Community: SanjayRV</title>
      <link>https://dev.to/sanjayrv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanjayrv"/>
    <language>en</language>
    <item>
      <title>A Beginners Guide to Socket Programming in C</title>
      <dc:creator>SanjayRV</dc:creator>
      <pubDate>Wed, 28 Apr 2021 10:50:04 +0000</pubDate>
      <link>https://dev.to/sanjayrv/a-beginners-guide-to-socket-programming-in-c-5an5</link>
      <guid>https://dev.to/sanjayrv/a-beginners-guide-to-socket-programming-in-c-5an5</guid>
      <description>&lt;p&gt;Hi all👋🏽 This is my first blog. I never had thoughts about writing blogs. I then came across why beginners should start blogging as they learn new things &lt;a href="https://blog.kieranroberts.dev/why-its-awesome-for-new-developers-to-blog-as-they-learn" rel="noopener noreferrer"&gt;here&lt;/a&gt;, so here we go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Trivia
&lt;/h3&gt;

&lt;p&gt;My career began 6 months ago as a backend developer working on node.js and express👨‍💻. I was thoroughly enjoying this phase of learning and coding. Eventually the project got over and in the new project I was to work on socket programming. &lt;/p&gt;

&lt;p&gt;I was more or less as clueless and sweating as this guy xD.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fin11be2w15zpbt5sgyvc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fin11be2w15zpbt5sgyvc.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
But this was only in the beginning. Let's us go through the concepts of socket programming to feel better📈&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A decent C programming experience.&lt;/li&gt;
&lt;li&gt;Basic understanding of networking concepts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without wasting any time, let us start.&lt;/p&gt;

&lt;h3&gt;
  
  
  Socket Programming in C
&lt;/h3&gt;

&lt;h4&gt;
  
  
  What are Sockets?
&lt;/h4&gt;

&lt;p&gt;Sockets allow communication between two different processes on the same or different machines.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is socket programming?
&lt;/h4&gt;

&lt;p&gt;Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket (node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Client-Server Communication?
&lt;/h4&gt;

&lt;p&gt;Client/Server communication involves two components, namely a client and a server. There are usually multiple clients in communication with a single server. The clients send requests to the server and the server responds to the client requests.&lt;/p&gt;

&lt;p&gt;Client is sometimes on and initiates requests to the server whenever interested. It needs to know the address of the server.&lt;/p&gt;

&lt;p&gt;Server is always on and services requests from many clients. It doesn’t initiate contact with any clients.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to use sockets?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Set up a socket.&lt;/li&gt;
&lt;li&gt;Send and Receive the packets.&lt;/li&gt;
&lt;li&gt;Close the socket. &lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Typical Client Program Using TCP
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Set up a Socket (Prepare to communicate)

&lt;ol&gt;
&lt;li&gt;Create a socket
&lt;/li&gt;
&lt;li&gt;Determine server IP address and port number &lt;/li&gt;
&lt;li&gt;Initiate the connection to the server &lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Send and receive packets (Exchange data with the server) 

&lt;ol&gt;
&lt;li&gt;Write data (i.e., request) to the socket &lt;/li&gt;
&lt;li&gt;Read data (i.e., response) from the socket &lt;/li&gt;
&lt;li&gt;Do stuff with the data (e.g., display a Web page) &lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Close the socket.&lt;/li&gt;

&lt;/ol&gt;

&lt;h4&gt;
  
  
  Typical Server Program Using TCP
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Set up a Socket (Prepare to communicate)

&lt;ol&gt;
&lt;li&gt;Create a socket s_listen (i.e., the listening socket) &lt;/li&gt;
&lt;li&gt;Associate server’s IP address and port no. with the socket &lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Wait to hear from a client 

&lt;ol&gt;
&lt;li&gt;Indicate how many connections can be pending on the socket &lt;/li&gt;
&lt;li&gt;Accept an incoming connection from a client, create a new socket s_new for the client.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Send and receive packets (Exchange data with the client over the new socket s_new)

&lt;ol&gt;
&lt;li&gt;Read data (i.e., client request) from the socket &lt;/li&gt;
&lt;li&gt;Handle the request &lt;/li&gt;
&lt;li&gt;Write data (i.e., server response) to the socket &lt;/li&gt;
&lt;li&gt;Close the socket s_new &lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Repeat 2.2-3.4 with the next connection request&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpajiq494s9d648dsacwb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpajiq494s9d648dsacwb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let us see how each step is done.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. Socket creation
&lt;/h3&gt;

&lt;p&gt;Both client and server need to setup the socket&lt;br&gt;
&lt;code&gt;int socket (domain, type, protocol);&lt;/code&gt;&lt;br&gt;
Returns a socket descriptor on success, -1 on failure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;domain&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;AF_INET for Ipv4&lt;/li&gt;
&lt;li&gt;AF_INET6 for Ipv6&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;type&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;SOCK_STREAM for TCP&lt;/li&gt;
&lt;li&gt;SOCK_DGRAM for UDP&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;protocol&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;0&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;int sockfd = socket (AF_INET, SOCK_STREAM, 0);&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Bind
&lt;/h3&gt;

&lt;p&gt;Only the server needs to bind.&lt;br&gt;
&lt;code&gt;int bind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sockfd is a file descriptor socket() that is returned.&lt;/li&gt;
&lt;li&gt;my_addr
struck sockaddr_in for IPv4
```c
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;struct sockaddr_in { &lt;br&gt;
short sin_family; // e.g. AF_INET &lt;br&gt;
unsigned short sin_port; // e.g. htons(3490)‏ &lt;br&gt;
struct in_addr sin_addr; // see struct in_addr below &lt;br&gt;
char sin_zero[8]; // zero this if you want to &lt;br&gt;
}; &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    struct in_addr
    ```c


struct in_addr { 
unsigned long s_addr; // load with inet_aton()‏ 
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;addrlen is the size of the address structure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example for bind()&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;sockaddr_in&lt;/span&gt; &lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;br&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sockfd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;br&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;sockfd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;creating&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;/span&amp;gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;br&gt;
    &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
&lt;span class="n"&gt;bzero&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// zero structure out &lt;/span&gt;&lt;br&gt;
&lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sin_family&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// match the socket() call&lt;/span&gt;&lt;br&gt;
&lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sin_port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;htons&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// specify port to listen on&lt;/span&gt;&lt;br&gt;
&lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sin_addr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;s_addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;htonl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;INADDR_ANY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//allow the server to accept a client connection on any interface&lt;/span&gt;&lt;br&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sockfd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;sockaddr&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;saddr&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;binding&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;/span&amp;gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;br&gt;
     &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  

&lt;ol&gt;
&lt;li&gt;Listen
&lt;/li&gt;
&lt;/ol&gt;
&lt;/h3&gt;


&lt;p&gt;Only the server needs to listen&lt;br&gt;
&lt;code&gt;int listen (int sockfd, int backlog)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;backlog&lt;/strong&gt; specifies the maximum number of pending connections the kernel should queue for the socket.
Listen returns 0 if OK, -1 on error&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Accept
&lt;/h3&gt;

&lt;p&gt;Only the server can accept the incoming client connections.&lt;br&gt;
&lt;code&gt;int accept (int sockfd, struct sockaddr *fromaddr, socklen_t *addrlen)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;fromaddr&lt;/strong&gt; is a pointer to store the client address&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;addrlen&lt;/strong&gt; is a pointer to store the returned size of addr.
accept() takes the first connection off the queue for sockfd and create a new socket (the return value) for communicating with the client.
accept() return a new socket descriptor if OK, -1 on error&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Connect
&lt;/h3&gt;

&lt;p&gt;The client need not bind, listen or accept. All client needs to do is to just connect to the server.&lt;br&gt;
&lt;code&gt;int connect (int sockfd, struct sockaddr *toaddr, socklen_t addrlen)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;toaddr&lt;/strong&gt; contains the IP address and port number of the serve&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;addrlen&lt;/strong&gt; is length of the socket address structure
connect() returns 0 if connection is successful and -1 on error&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Sending and receiving the data
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ssize_t read(int sockfd, void *buffer, size_t len)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Read up to n bytes from sockfd into buffer&lt;/li&gt;
&lt;li&gt;Returns the number of bytes read on success (0 indicates end of file), -1 on error.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Example &lt;code&gt;read (sockfd, buffer, sizeof(buffer));&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ssize_t write(int sockfd, const void *buffer, size_t n)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Write up to n bytes from buffer to sockfd&lt;/li&gt;
&lt;li&gt;Returns the number of bytes written on success, -1 on error.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Example &lt;code&gt;write (sockfd, “Hello”, strlen(“Hello”));&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Close
&lt;/h3&gt;

&lt;p&gt;Don't forget to close the socket descriptor after all the effort we've put.&lt;br&gt;
&lt;code&gt;int close(int sockfd)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqngbkdjmva0amiy2dpx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqngbkdjmva0amiy2dpx.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If I could learn socket programming, you can too!&lt;br&gt;
Happy Programming!!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>c</category>
      <category>socket</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
