0% found this document useful (0 votes)
21 views4 pages

22bps1059 Lab8

The document describes a client-server program that validates IP addresses. The client sends an IP address to the server, which uses the address to determine the class, network ID, host ID, subnet mask, and block addresses.

Uploaded by

Jane Doe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

22bps1059 Lab8

The document describes a client-server program that validates IP addresses. The client sends an IP address to the server, which uses the address to determine the class, network ID, host ID, subnet mask, and block addresses.

Uploaded by

Jane Doe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB 8 – 22bps1059

client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_PORT 8080

int main() {
int sockfd;
struct sockaddr_in server_addr;
char ip_address[16];

// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);

// Set server address


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Replace with server
IP

// Connect to server
connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));

printf("Enter IP address to validate: ");


fgets(ip_address, sizeof(ip_address), stdin);
ip_address[strcspn(ip_address, "\n")] = 0; // Remove newline character

// Send IP address to server


send(sockfd, ip_address, strlen(ip_address), 0);

close(sockfd);
return 0;
}

Server

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_PORT 8080


void print_ip_info(struct in_addr ip) {
unsigned char *ip_bytes = (unsigned char *)&ip.s_addr;
printf("IP Address: %s\n", inet_ntoa(ip));
printf("Class: ");
if (ip_bytes[0] < 128) {
printf("A\n");
} else if (ip_bytes[0] < 192) {
printf("B\n");
} else if (ip_bytes[0] < 224) {
printf("C\n");
} else {
printf("it is for reasearch purpose so we can not provide given values");
}
printf("Network ID: %u.%u.%u.0\n", ip_bytes[0], ip_bytes[1], ip_bytes[2]);
printf("Host ID: %u.%u.%u.%u\n", ip_bytes[3], 0, 0, 0);
printf("Subnet Mask: ");
if (ip_bytes[0] < 128) {
printf("255.0.0.0\n");
} else if (ip_bytes[0] < 192) {
printf("255.255.0.0\n");
} else if (ip_bytes[0] < 224) {
printf("255.255.255.0\n");
} else {
printf("Reserved\n");
}
printf("Block Addresses: ");
if (ip_bytes[0] < 128) {
printf("1.0.0.0 to 126.0.0.0\n");
} else if (ip_bytes[0] < 192) {
printf("128.0.0.0 to 191.255.0.0\n");
} else if (ip_bytes[0] < 224) {
printf("192.0.0.0 to 223.255.255.0\n");
} else {
printf("Reserved\n");
}
printf("Broadcast ID: %u.%u.%u.255\n", ip_bytes[0], ip_bytes[1],
ip_bytes[2]);
}

int main() {
int sockfd, newsockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;
char buffer[16];
struct in_addr ip;
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);

// Set server address


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;

// Bind socket to address


bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));

// Listen for connections


listen(sockfd, 5);

client_len = sizeof(client_addr);
newsockfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_len);

// Receive IP address from client


recv(newsockfd, buffer, sizeof(buffer), 0);

// Convert IP address string to in_addr structure


if (inet_pton(AF_INET, buffer, &ip) <= 0) {
perror("Invalid IP address format");
exit(EXIT_FAILURE);
}

// Print IP information
print_ip_info(ip);

close(newsockfd);
close(sockfd);
return 0;
}

You might also like