C Socket Examples 🎯

beginner
5 min

C Socket Examples 🎯

Welcome to our in-depth guide on C Socket Programming! This tutorial is designed for both beginners and intermediate learners who want to understand and implement socket programming in C. Let's dive in!

Understanding Socket Programming 📝

Socket programming is a method of communication between two devices over a network. It allows your C programs to send and receive data over the internet.

Why use Socket Programming? 💡

  • It enables communication between applications running on different hosts.
  • It's versatile and can be used for various network protocols such as TCP, UDP, etc.
  • It's the foundation for many real-world applications like file transfer, email, web browsing, and more!

Getting Started with C Sockets 🎯

To work with sockets in C, we need to include the following header files:

  • socket.h: Contains various socket-related functions
  • netinet/in.h: Contains the sockaddr_in structure
  • arpa/inet.h: Contains the inet_addr() function

Creating a Simple Socket Program 📝

Let's create a simple client-server example to understand the basics.

Server Code

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main() { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); char buffer[1024] = {0}; char* message = "Hello from the server"; // Create socket if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Set options on the socket if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { perror("setsockopt"); exit(EXIT_FAILURE); } // Define the server address address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(8888); // Bind the socket to the specified address and port if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } // Accept an incoming connection if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("accept"); exit(EXIT_FAILURE); } // Receive data from the client read(new_socket, buffer, 1024); printf("Received: %s\n", buffer); // Send a response to the client send(new_socket, message, strlen(message), 0); return 0; }

Client Code

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main() { int sock = 0, valread; struct sockaddr_in serv_addr; char* message = "Hello from the client"; char buffer[1024] = {0}; // Create socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket creation error"); exit(EXIT_FAILURE); } // Define the server address serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(8888); // Convert IPv4 address from text to binary form if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { perror("invalid address/ address not supported"); exit(EXIT_FAILURE); } // Connect to the server if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("connection failed"); exit(EXIT_FAILURE); } // Send data to the server send(sock, message, strlen(message), 0); // Receive a response from the server valread = read(sock, buffer, 1024); printf("Received: %s\n", buffer); return 0; }

Quiz Time 🎯

Quick Quiz
Question 1 of 1

Which header file contains various socket-related functions in C?

Happy learning, and we hope you enjoyed our deep dive into C Socket Programming! Stay tuned for more practical tutorials on CodeYourCraft. 🎯