Table of Contents
How to use sockets in Linux
Linux sockets are a way to do inter-process communication (IPC), allowing processes to communicate with each other locally or over a network. They support several communication protocols, the most common being TCP and UDP.
Here’s a basic tutorial to get you started with Linux sockets using TCP, as it's the most widely used protocol for connection-oriented communications.
Prerequisites
- Basic understanding of C programming.
- A Linux environment to compile and run your code.
Step 1: Include Necessary Headers
First, include the necessary headers in your C program:
#include <stdio.h> // Standard input/output definitions
#include <stdlib.h> // Standard library
#include <string.h> // String handling functions
#include <unistd.h> // UNIX standard function definitions
#include <sys/socket.h> // Main sockets header
#include <netinet/in.h> // Internet address family
Step 2: Create a Socket
You'll need to create a socket using the `socket()` function. This function returns an integer that acts as a socket descriptor.
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
- AF_INET
: Address family (IPv4).
- SOCK_STREAM
: Socket type (TCP).
- 0
: Protocol (default protocol for the chosen type, TCP in this case).
Step 3: Bind the Socket
Bind the socket to an address and port on your system using the `bind()` function.
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; // Listen on any IP address
serv_addr.sin_port = htons(12345); // Port number
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(2);
}
Step 4: Listen for Connections
After binding, listen for incoming connections:
listen(sockfd, 5);
The `5` here specifies the maximum size of the queue for pending connections.
Step 5: Accept Connections
Accept a connection with the `accept()` function. This function will block the process until a client connects to the server.
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(3);
}
Step 6: Read and Write to the Socket
Use read()
and write()
functions to receive and send data:
char buffer[256];
memset(buffer, 0, 256);
int n = read(newsockfd, buffer, 255);
if (n < 0) perror("ERROR reading from socket");
printf("Here is the message: %s\n", buffer);
n = write(newsockfd, "I got your message", 18);
if (n < 0) perror("ERROR writing to socket");
Step 7: Close the Socket
Finally, close the socket once you're done with it:
close(newsockfd);
close(sockfd);
Compiling and Running
To compile your C program, use gcc
:
gcc -o server yourprogram.c
Run your server program:
./server
You'll need a client to connect to this server. You can write a similar program that uses `connect()` instead of `bind()` and `listen()`, or you can use tools like `telnet` or `nc` (netcat) to test your server.
This is a very basic introduction to sockets in Linux. Real-world applications may need to handle errors more gracefully, use non-blocking sockets or select/poll for handling multiple connections simultaneously.
🏷️ Author position : Embedded Software Engineer
🔗 Author LinkedIn : LinkedIn profile
Comments