ASSIGNMENT-7
WAP to implement UDP client server communication using socket
programming.
SERVER CODE:-
#include <stdio.h> // For standard input/output functions
#include <stdlib.h> // For standard library functions (e.g., exit)
#include <string.h> // For string manipulation functions
#include <unistd.h> // For close function
#include <arpa/inet.h> // For socket-related functions and definitions
#define PORT 8080 // Port number where the server will listen for messages
int main() {
int server_fd;
char buffer[1024];
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
const char *response = "Hello from UDP server";
// Create a UDP socket
if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Define the server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET; // IPv4 family
server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any network interface
server_addr.sin_port = htons(PORT); // Convert port number to network byte
order
// Bind the socket to the server address
if (bind(server_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("UDP Server is listening on port %d\n", PORT);
// Receive data from a client
int n = recvfrom(server_fd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr,
&addr_len);
buffer[n] = '\0'; // Null-terminate the received data
printf("Message from client: %s\n", buffer);
// Send a response to the client
sendto(server_fd, response, strlen(response), 0, (const struct sockaddr
*)&client_addr, addr_len);
printf("Response sent to client\n");
// Close the socket
close(server_fd);
return 0;
}
CLIENT SERVER:-
#include <stdio.h> // For standard input/output functions
#include <stdlib.h> // For standard library functions (e.g., exit)
#include <string.h> // For string manipulation functions
#include <unistd.h> // For close function
#include <arpa/inet.h> // For socket-related functions and definitions
#define PORT 8080 // Port number to connect to the server
int main() {
int client_fd;
char buffer[1024];
struct sockaddr_in server_addr;
const char *message = "Hello from UDP client";
// Create a UDP socket
if ((client_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Define the server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET; // IPv4 family
server_addr.sin_port = htons(PORT); // Convert port number to network byte
order
server_addr.sin_addr.s_addr = INADDR_ANY; // Connect to localhost
// Send a message to the server
sendto(client_fd, message, strlen(message), 0, (const struct sockaddr
*)&server_addr, sizeof(server_addr));
printf("Message sent to server\n");
// Receive a response from the server
socklen_t addr_len = sizeof(server_addr);
int n = recvfrom(client_fd, buffer, sizeof(buffer), 0, (struct sockaddr *)&server_addr,
&addr_len);
buffer[n] = '\0'; // Null-terminate the received data
printf("Message from server: %s\n", buffer);
// Close the socket
close(client_fd);
return 0;
}
Explanation of Functions and Headers
1. #include <arpa/inet.h>: Provides definitions for networking functions and
structures.
2. socket(): Creates an endpoint for communication.
3. bind(): Binds a socket to a specific IP and port.
4. recvfrom(): Receives a message from a client and records the client's address.
5. sendto(): Sends a response to a specified address.
6. close(): Closes the socket.
SERVER TERMINAL
UDP Server is listening on port 8080
Message from client: Hello from UDP client
Response sent to client
CLIENT SERVER
Message sent to server
Message from server: Hello from UDP server