DEV Community


Posted on

Socket programming in C : Part-1

Hello there Nice to see you !!!

Today in this article i am going to talk about socket programming in C language.

Before going to start about the our main topic first we have to know about the prerequisites to learn socket programming.

  • Basic understanding of C language .
  • Basic knowledge of a Linux command line , editing text files on a linux system (If you are working on Linux based operating system).
  • Should know Basics of HTML,CSS and Javascript.
  • Basics of Networking term like packet, protocol, protocol suite(TCP/UDP). (We will Discuss basics of networking later).

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??".

So here the list :

  1. Intro to C programming from scratch OR C Programming Tutorial for Beginners .
  2. HTML, CSS, and Javascript for Web Developers
  3. Basic Networking Course for Beginners OR Introduction to networking for complete beginners

Basics of Networking terminology

Packet :

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.
A packet is a container or box that carries data over a TCP/IP network and internetworks.
Explanation : 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.
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.


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.
Explanation : 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.

Networking Protocols:

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. Explanation : 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:

Application layer:


Transport layer:


Internet layer:

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

Data link layer:

ARP, NDP, OSPF, L2TP, PPP, MAC, Ethernet, DSL, ISDN and FDDI. For more detail on Protocols click here. A video on protocols Visit here.

A Introduction to TCP/UDP:

please click here.


Convert host-to-host packet delivery service into a process-to-process communication channel.

Byte Ordering:

●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!!!!

Top comments (2)

ondrejs profile image

Good article! It would be nice to see here more low level stuff like this.

abhishekalbert profile image

Thx Ondrej😊