Skip to main content

Posts

Showing posts from December, 2024

System Calls in Network Programming

What Are System Calls in Network Programming? System calls are low-level functions that interact directly with the kernel, enabling user applications to communicate with hardware or perform networking tasks like creating sockets, sending/receiving data, and managing connections.Let's break down the key network programming functions and explain the parameters used in each. 1. socket() Creates an endpoint for communication. int socket ( int domain, int type, int protocol) ; domain – Specifies the protocol family (addressing type). AF_INET – IPv4 AF_INET6 – IPv6 AF_UNIX – Local socket (inter-process communication) type – Defines the communication semantics. SOCK_STREAM – TCP (connection-oriented) SOCK_DGRAM – UDP (connectionless) protocol – Protocol to be used. 0 – Automatically selects the default protocol (e.g., TCP for SOCK_STREAM ). IPPROTO_TCP – TCP IPPROTO_UDP – UDP Return Value: A file descriptor for the socket. On error, returns -1 . 2. bind() Assigns an address...

Simple TCP/IP Client Server Example

Socket Programming in C Socket programming is a method used in network communication that allows data to be sent and received between devices. It is a critical concept in computer networking and is widely used to develop client-server applications. In this blog post, we will explore the fundamentals of socket programming in C, focusing on TCP (Transmission Control Protocol) for reliable, connection-oriented communication. What is Socket Programming? Socket programming enables communication between two nodes on a network. A server listens for incoming client requests, and the client connects to the server to facilitate data exchange. Sockets provide a communication channel between two processes, either on the same machine or different machines connected via a network. Types of Sockets Stream Sockets (SOCK_STREAM): Uses TCP for communication. Provides reliable, connection-oriented communication. Data is transmitted in order and without loss. Datagram Sockets (SOCK_DGRAM): Uses UDP (User ...

Client Server Communication using UDP

Here's a simple UDP client-server program in C. Unlike TCP, UDP is connectionless, meaning the server doesn't need to accept connections – it just waits for data and responds. UDP Server ( udp_server.c ): #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> int main() {     int sockfd;     struct sockaddr_in server, client;     char buffer[1024];     socklen_t len = sizeof(client);     // Create UDP socket     sockfd = socket(AF_INET, SOCK_DGRAM, 0);     // Server address configuration     server.sin_family = AF_INET;     server.sin_addr.s_addr = INADDR_ANY;     server.sin_port = htons(8080);     // Bind the socket to the server address     bind(sockfd, (struct sockaddr *)&server, sizeof(server));     printf("Waiting for data...\n");     // Receive data from client ...

Client Server Program for Primality Checking

Below is an example of a simple client-server program in C. The client sends a number to the server, and the server determines whether the number is prime or not and sends the result back to the client Server Code #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <stdbool.h> #define PORT 8080 bool is_prime(int num) {     if (num <= 1) return false;     for (int i = 2; i * i <= num; i++) {         if (num % i == 0) return false;     }     return true; } int main() {     int server_fd, new_socket;     struct sockaddr_in address;     int addrlen = sizeof(address);     char buffer[1024] = {0};     char response[1024];     // Create socket file descriptor     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {         perror("Socket fail...

Concurrent Time Server application using UDP

Below is a simple implementation of a Concurrent Time Server application using UDP in C. This program consists of two parts: the server and the client. Server Code The server listens for time requests and sends its system time back to the client. // Server Code #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 int main() {     int sockfd;     struct sockaddr_in server_addr, client_addr;     char buffer[BUFFER_SIZE];     socklen_t addr_len = sizeof(client_addr);     // Create socket     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {         perror("Socket creation failed");         exit(EXIT_FAILURE);     }     memset(&server_addr, 0, sizeof(server_addr));     memset(&client_addr, 0, sizeof(clien...