C Socket Creation (socket()) 🎯

beginner
12 min

C Socket Creation (socket()) 🎯

Welcome to our comprehensive guide on C Socket Creation using the socket() function! In this lesson, we'll take a deep dive into the world of networking in C programming, focusing on creating and managing sockets. Let's embark on this exciting journey together. 📝

What are Sockets? 📝

In simple terms, a socket is an endpoint in a network communication system. Sockets allow your C programs to communicate with other devices over a network, such as the internet. They can be used for various purposes, including file transfer, email, and web browsing.

Understanding the socket() Function 💡

The socket() function is a fundamental building block in C network programming. It creates a new socket, which can be used for communication. The function takes three arguments:

  • AF_INET: Address family, which specifies the type of network protocol to use. In this case, we're using IPv4.
  • SOCK_STREAM: Socket type, which specifies the semantics of the communication. In this case, we're using a stream socket, which provides a reliable byte-stream interface.
  • 0: Protocol, which specifies the communication protocol to be used. In our case, we'll let the system choose the best suitable protocol.

Creating a Simple Server 💡

Let's create a simple server that listens for incoming connections.

c
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> int main() { // Create the socket int server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket == -1) { perror("socket creation failed"); exit(EXIT_FAILURE); } // Configure the socket struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = INADDR_ANY; // Bind the socket to the specified address and port if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("bind failed"); exit(EXIT_FAILURE); } // Start listening for incoming connections if (listen(server_socket, 3) == -1) { perror("listen failed"); exit(EXIT_FAILURE); } printf("Server is running on port 8080...\n"); // Accept an incoming connection int client_socket; struct sockaddr_in client_addr; socklen_t client_len = sizeof(client_addr); if ((client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len)) == -1) { perror("accept failed"); exit(EXIT_FAILURE); } // Receive data from the client and print it char buffer[1024]; int bytes_received = recv(client_socket, buffer, sizeof(buffer), 0); if (bytes_received > 0) { printf("Received: %s\n", buffer); } // Close the client socket close(client_socket); // Close the server socket close(server_socket); return 0; }

Creating a Simple Client 💡

Now, let's create a simple client that connects to the server and sends a message.

c
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> int main() { // Create the socket int client_socket = socket(AF_INET, SOCK_STREAM, 0); if (client_socket == -1) { perror("socket creation failed"); exit(EXIT_FAILURE); } // Configure the server address and port struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Connect to the server if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("connection failed"); exit(EXIT_FAILURE); } // Send a message to the server const char* message = "Hello, Server!"; send(client_socket, message, strlen(message), 0); // Receive a reply from the server char buffer[1024]; int bytes_received = recv(client_socket, buffer, sizeof(buffer), 0); if (bytes_received > 0) { printf("Received: %s\n", buffer); } // Close the client socket close(client_socket); return 0; }

Quiz 💡

Quick Quiz
Question 1 of 1

Which function in C programming creates a new socket?

That's it for this lesson! In the next lesson, we'll dive deeper into working with sockets, exploring more advanced topics and real-world examples. Keep coding and enjoy the journey! 🎯