Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to create Socket in Linux (Part 19/24)

By Ashish Vara September 8, 2016

This tutorial will explain about how to create socket programming in C language. Socket is virtual end point communication over the network between two hosts or processes. Refer to the tutorial Socket in Linux before learning this tutorial. You always open the Google page in your web browser but  do you know how our web browser accesses the Google page form www.google.com?  When you type www.google.com in your web browser, it will open the socket and send the request to server (www.google.com). Server always listens to the request from different hosts. When server receives and accepts the request it sends response back to the host (i.e. Local PC). After establishment of connection, server will send the Google page to our web browser.    

This tutorial assumes you know about socket in Linux and are aware with C programming language. Linux has standard C library of socket in package named <sys/socket.h> header file. Socket creates file descriptor using the socket ( ) system call.

Socket ( ) system call

The system call socket ( ) creates the end point for communication and returns a descriptor.

Synopsis of function is:

               #include <sys/types.h>     

               #include <sys/socket.h>

 

               int socket (int domain, int type, int protocol);

 

The domain argument specifies a communication domain. This argument selects the protocol family which will be used for communication. These protocols are defined in <sys/socket.h>. The following protocols are included in library:

Table Listing Protocol Names Mentioned in socket.h

Fig. 1: Table Listing Protocol Names Mentioned in socket.h

 

The type indicates types of sockets as  follows:

SOCK_STREAM – It is a connection oriented bye stream. Data transmission is reliable and flexible.

SOCK_DGRAM – It supports datagram with connection less protocol. Message transmission is unreliable. 

SOCK_SEQPACKET – Provides a sequenced, reliable, two-way connection- based data transmission path for datagram of fixed maximum length; a consumer is required to read an entire packet with each input system call.

SOCK_RAW – Provides raw network protocol access.

SOCK_RDM – Provides a reliable datagram layer that does not guarantee ordering.              

The default protocol for address family and type will be used if the protocol argument is zero. The specified protocol shall use which is supported by address family if the protocol argument is zero.

The system call socket ( ) returns file descriptor upon success or returns -1 upon error.                   

Let’s see How to create socket in C programming. We need to call extra library for various purpose of configuration. Here <netinet/in.h> header file defines the internet domain address and <netdb.h > header file defines network database operation.

//*************************************socket.c********************************//

#include <stdio.h>

#include <sys/types.h>  

#include <netinet/in.h> 

#include <sys/socket.h> 

#include <netdb.h>      

#include <unistd.h>

#include <stdlib.h>

#include <errno.h>

main()

{

   int result; 

   result = socket (AF_INET, SOCK_STREAM, 0);

   if (result ==  -1 )

   {

      close (result);

      exit (EXIT_FAILURE);

   }

   else

   {

            printf (“Socket Successfully Created!!!n”);  

   }

  

}

//*************************************socket.c********************************//

Save the file in directory and compile it. After successful compilation, run the executable file from command terminal. You may refer to the tutorial How to make first C program in Linux if you are not aware of compilation and execution process.

Here the system call socket ( ) creates the socket file descriptor upon successful execution. AF_INET attribute defines the socket used IPv6 internet protocol and SOCK_STREAM attribute is TCP/IP type of socket with default value indicated by last argument is zero. Upon successful execution of socket ( ), it will return file descriptor and  the following line will be printed on the screen:

Socket Successfully Created!!!

If not successfully created, it will return -1 and indicate the error occurred in creation of socket. Specific error type is returned by errno which is located in errno.h file. Program closes the socket file descriptor and exits from execution of program.


Filed Under: Tutorials

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Diode recovery test Irrm timing.
  • How to make string LEDs?
  • The Analog Gods Hate Me
  • Battery Deep Discharge – IC Workarounds?
  • Safe Current and Power Density Limits in PCB Copper(in A/m² and W/m³) simulation

RSS Electro-Tech-Online.com Discussions

  • Tektronix 2235 channel 1 trace unstable
  • How to make string LEDs?
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz
  • The Analog Gods Hate Me
  • Simple LED Analog Clock Idea

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • Tria modules integrate edge AI processing with multi-core processors
  • pSemi introduces RF switch with 52 dBm PMAX,PEAK and 90-dBm IIP3 linearity
  • XP Power launches 1.3 kW power supply with 58.9 W/cm³ density
  • How to enable Wi-Fi provisioning in ESP32-based IoT products
  • Amphenol RF introduces FAKRA to SMA adapters with 4 GHz operating frequency

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe