C Socket Programming Introduction 🎯

beginner
6 min

C Socket Programming Introduction 🎯

Welcome to our deep dive into C Socket Programming! This tutorial is designed for both beginners and intermediates, so let's get started.

What is Socket Programming? 📝

Socket programming is a method of communication between two devices over a network, using sockets. It's fundamental in network programming and allows C programs to communicate with other devices over the internet.

Why Use Socket Programming? 💡

  • Enables C programs to communicate over a network
  • Provides a robust solution for building network applications
  • Offers low-level control over network communication

Understanding Sockets 🎯

A socket is an endpoint of a two-way communication link between two programs running on the network. Sockets can be used to send and receive data over this link.

Socket Types 📝

  1. Stream Socket: Used for reliable, character-stream I/O. It's connection-oriented and provides error-free data transmission.
  2. Datagram Socket: Used for sending and receiving datagrams. It's connection-less and provides unreliable data transmission.

Setting Up a Simple Socket Program 🎯

Let's create a simple server and client program to get familiar with sockets.

Server Code

c
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int main() { // Create a socket int server_socket = socket(AF_INET, SOCK_STREAM, 0); // Configure the address and port for the server struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8888); server_addr.sin_addr.s_addr = INADDR_ANY; // Bind the socket to the address and port bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)); // Listen for incoming connections listen(server_socket, 3); // Accept an incoming connection int client_socket = accept(server_socket, NULL, NULL); // Receive a message from the client char buffer[1024]; recv(client_socket, buffer, sizeof(buffer), 0); // Print the received message printf("Received message: %s\n", buffer); return 0; }

Client Code

c
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int main() { // Create a socket int client_socket = socket(AF_INET, SOCK_STREAM, 0); // Configure the address and port for the server struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8888); // Convert IPv4 address into a binary format char server_ip[] = "127.0.0.1"; inet_pton(AF_INET, server_ip, &server_addr.sin_addr); // Connect to the server connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)); // Send a message to the server char message[] = "Hello, Server!"; send(client_socket, message, sizeof(message), 0); return 0; }

Quiz 🎯

Quick Quiz
Question 1 of 1

What is Socket Programming?


This is just the beginning of our socket programming journey! In the next lessons, we'll dive deeper into advanced topics, so stay tuned. Happy coding! 💡