Socket programming in c linux


Article background

This article is one of the best in explaining socket programming in linux, applications of socket programming. If you are already familiar with socket programming concepts and APIs, visit project titled for a realistic application.

Online courses related to this article


Course description

Learn IPC socket programming concepts & applications in Linux with projects and frequently asked interview questions.



Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs



Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs

Article contents


What is a socket?

A socket is defined as a link for two-way communication for two programs executed on the network. A socket consists of a combination of protocol type, IP address, and port number.

The BSD sockets APIs are written in the C programming language. Most other programming languages provide similar interfaces, typically written as a wrapper library based on the C APIs.

We will be discussing Berkeley Sockets in this article. A local program called as client can communicate with a remote program called as server program by exchanging data with TCP/IP by knowing the combination of protocol type, IP address, and port number.

Socket header files

The Berkeley socket interface is defined in several header files explained below.

  • sys/socket.h: Contains declarations for most of the functions that operate on sockets. It has the core socket functions and data structures. Always include the sys/types.h header file before this header file.
  • netdb.h: Contains functions for translating protocol names and host names into numeric addresses. It searches local data as well as name services.
  • netinet/in.h: Contains constants and structures defined by the Internet system. Several macros are defined for manipulating Internet addresses. Among these are INADDR_ANY, which indicates that no specific local address is required, and INADDR_NONE, which generally indicates an error in address manipulation functions. Refer to bind for more information on binding a local address to the socket. Always include the sys/types.h header file before this header file.
  • arpa/inet.h: Contains declarations for the network address resolution functions. Always include the netinet/in.h header file before this header file.
  • net/if.h: Contains structures that define the network interface and provide a packet transport mechanism. This header file is useful only for low-level programming of the network interface. Always include the sys/types.h and sys/socket.h header files before this header file.
  • Client program functions sequence

    To Read Full Article

    Start any course from the following
    Share