Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

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

By Ashish Vara

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

 

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

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!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers
  • First single-core MPU with MIPI CSI-2 camera interface and audio

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • A circuit that can adjust a resistance and probing a voltage node
  • DC to DC buck converter
  • A circuit that can probe 2 currents and adjust the resistor
  • Microsoft Project 2019 dependencies
  • MOSFET ORing circuit simulation (LTspice)

RSS Electro-Tech-Online.com Discussions

  • Enclosure sought
  • Need help using a common power supply for two devices
  • Fletcher's Law
  • Setting the 18F24K20 to digital.
  • Multistage BJT amplifier
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 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 | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering