Socket programming in C : Part-1
Tags: socket, c, networking, programming
Published: 2018-12-09 06:01:55

---
title: Socket programming in C : Part-1
published: true
description: Network programming plays a vital part in development of peer-to-peer transmission of any kind message in any form. Whenever there is a need to  communicate between client and the server, socket programming plays an important role.
tags: #socket #c #networking #programming 
---

<p>Hello there Nice to see you !!!</p> 
<p>Today in this article i am going to talk about socket programming in C language.</p>
<p> Before going to start about the our main topic first we have to know about the prerequisites to learn socket programming.
<ul>
<li>Basic understanding of C language .</li>
<li>Basic knowledge of a Linux command line , editing text files on a linux system (If you are working on Linux based operating system).</li>
<li>Should know Basics of HTML,CSS and Javascript.</li>
<li>Basics of Networking term like packet, protocol, protocol suite(TCP/UDP).
(<em>We will Discuss basics of networking later</em>).</li>
</ul></p>
<p> If you're not familiar with these don't be panic here a list of some good resources from where you can learn all you need to learn to start this topic or to move forward on this article . But if you know then it's good then you can direct start learning  from "What is Socket??".</p>
<p> So here the list :<br />
<ol>
<li><a href="https://handmadehero.org/watch"> Intro to C programming from scratch </a> OR <a href="https://www.youtube.com/watch?v=KJgsSFOSQv0">C Programming Tutorial for Beginners </a>.</li>
<li> <a href="https://www.coursera.org/learn/html-css-javascript-for-web-developers">HTML, CSS, and Javascript for Web Developers</a> </li>

<li><a href="http://www.steves-internet-guide.com/basic-networking-course/">Basic Networking Course for Beginners</a> OR <a href="https://www.udemy.com/introduction-to-networking-for-complete-beginners/">Introduction to networking for complete beginners</a></li>
</ol>

<p>
<h1>Basics of Networking terminology</h1><hr><br />

<h3> Packet :</h3><hr> <br />A packet is the unit of data that is routed between an origin and a destination on the Internet or any other packet-switched network.<br />
OR<br />
 A packet is a container or box that carries data over a TCP/IP network and internetworks. <br />

<em> Explanation </em>:
Whenever a node on a network sends some data over the network, it passes the data frame to the switch, and later to the router. The router, after looking at the destination IP addresses, encapsulates the data and routes it toward the recipient. This encapsulated data is the packet that is forwarded over the network.<br />

Packets contain two distinct types of information to reach the destination completely and correctly, namely control information and the data it is carrying. The control information includes source destination addresses, sequencing format, error detection and correction mechanisms, all of which help to ensure the optimal delivery of data. The control information usually resides in the header and trailer, encapsulating the user data in between them.<br />



<h3>Protocols:</h3><hr><br />Network protocols are formal standards and policies comprised of rules, procedures and formats that define communication between two or more devices over a network. Network protocols govern the end-to-end processes of timely, secure and managed data or network communication.<br />

<em>Explanation :</em>
Network protocols incorporate all the processes, requirements and constraints of initiating and accomplishing communication between computers, servers, routers and other network-enabled devices. Network protocols must be confirmed and installed by the sender and receiver to ensure network/data communication and apply to software and hardware nodes that communicate on a network.

There are several broad types of networking protocols, including:

Network communication protocols: Basic data communication protocols, such as TCP/IP and HTTP.
Network security protocols: Implement security over network communications and include HTTPS, SSL and SFTP.
Network management protocols: Provide network governance and maintenance and include SNMP and ICMP.


<h3>Networking Protocols:</h3><hr>

Internet Protocol suite (IP suite) is the standard network model and communication protocol stack used on the Internet and on most other computer networks. While other networking models exist, the IP suite is overwhelmingly the global standard for computer-to-computer communication.

<em>Explanation : </em>

The IP suite follows a client/server model, in which multiple client programs share the services of a common server program. Protocols in the suite define end-to-end data handling methods for everything from packetizing, addressing and routing to receiving. Broken down into layers the IPS includes the link layer, the internet layer, the transport layer, application layer and the physical layer. Each layer contains a number of protocols for communications. The suite is sometimes just called TCP/IP, because those are the predominant protocols in the model and were the first ones used. However, the IP suite involves many other protocols.

The following is a partial list of protocols in each layer:

<h4>Application layer:</h4>

BGP, DHCP, DNS, FTP, HTTP, IMAP, LDAP, MGCP, NNTP, NTP, POP, ONC/RPC, RTP, RTSP, RIP, SIP, SMTP, SNMP, SSH, Telnet, TLS/SSL and XMPP.

<h4>Transport layer:</h4>

TCP, UDP, DCCP, SCTP and RSVP.

<h4>Internet layer:</h4>

IP, IPv4, IPv6, ICMP, ICMPv6, ECN, IGMP and IPsec.

<h4>Data link layer:</h4>

ARP, NDP, OSPF, L2TP, PPP, MAC, Ethernet, DSL, ISDN and FDDI.

For more detail on Protocols click <a href="https://whatis.techtarget.com/definition/Internet-Protocol-suite-IP-suite">here</a>.
A video on protocols Visit<a href="https://youtu.be/wAIT9q6-OT8"> here</a>.


<p>
<h4> A Introduction to TCP/UDP:</h4>  please click <a href="https://www.linkedin.com/learning/comptia-network-plus-n10-006-cert-prep-4-making-tcp-ip-work/introduction-to-tcp-and-udp">here</a>.</p>

<h1>Demultiplexing:</h1>

Convert host-to-host packet delivery service into a process-to-process communication channel.
<img src="https://thepracticaldev.s3.amazonaws.com/i/x9mjpr2jq79y7in4fqgi.png">

<h1>Byte Ordering:</h1>
●Two types of “Byte ordering”
– Network Byte Order: High-order byte of the number is
stored in memory at the lowest address
– Host Byte Order: Low-order byte of the number is
stored in memory at the lowest address
– Network stack (TCP/IP) expects Network Byte Order
● Conversions:
– htons() - Host to Network Short
– htonl() - Host to Network Long
– ntohs() - Network to Host Short
– ntohl() - Network to Host Long



I think it's enough for this article .In next part we will discuss about what is socket and types of socket etc. 

So see you guys in next article hope it may help you. 

Good luck!! Keep learning!!!And happy coding!!!!