C Programming: Working with struct sockaddr šŸŽÆ

beginner
9 min

C Programming: Working with struct sockaddr šŸŽÆ

Welcome to this comprehensive guide on struct sockaddr in C programming! This tutorial is designed for both beginners and intermediates, and we'll cover the topic from the ground up. Let's get started!

Understanding struct sockaddr šŸ“

struct sockaddr is a fundamental structure in C programming, used for sockets communication. It provides a common format for different types of sockets, allowing for versatility in network programming.

c
struct sockaddr { sa_family_t sa_family; // Address family (AF_INET, AF_INET6, etc.) char sa_data[14]; // Actual address data };

šŸ’” Pro Tip: sa_family_t is a type that represents the address family.

Creating a socket address šŸ“

To create a socket address, we first need to define the address family and then fill the address data.

c
#include <arpa/inet.h> struct sockaddr_in serv_addr; // Define a socket address for IPv4 serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); serv_addr.sin_port = htons(8080);

šŸ’” Pro Tip: inet_addr() converts a string representation of an IP address to a binary format, and htons() converts the port number to network byte order.

Example: Server and Client Communication šŸ“

Let's create a simple server-client example using struct sockaddr.

Server Code šŸ“

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #define PORT 8080 int main() { int server_fd, new_socket, valread; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); char buffer[1024] = {0}; char *hello = "Hello from server"; // Create a 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); } // Prepare the sockaddr_in structure address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); // 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 a connection from a client if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("accept"); exit(EXIT_FAILURE); } // Read and print the message from the client valread = read(new_socket, buffer, 1024); printf("Message received: %s\n", buffer); // Send the response to the client send(new_socket, hello, strlen(hello), 0); printf("Hello sent to client\n"); return 0; }

Client Code šŸ“

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #define PORT 8080 int main() { int sock = 0; struct sockaddr_in serv_addr; char *message = "Hello from client"; // Create a socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket creation error"); exit(EXIT_FAILURE); } // Prepare the sockaddr_in structure serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // 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 the message to the server send(sock, message, strlen(message), 0); printf("Message sent to server\n"); // Receive the response from the server char response[1024] = {0}; recv(sock, response, 1024, 0); printf("Server response: %s\n", response); return 0; }

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is `struct sockaddr` used for in C programming?

That's it for this tutorial! We've covered the basics of working with struct sockaddr in C programming, including creating a socket address and a simple server-client example. Happy coding! šŸš€