NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

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

#define PORT 8080
#define BUFFER_SIZE 256

// Function to calculate number of hosts in a subnet
unsigned long calculate_hosts(int cidr) {
return (1UL << (32 - cidr)) - 2; // Subtract network and broadcast addresses
}

// Function to calculate first and last IP addresses of a subnet
void calculate_first_last_ip(char *ip_str, int cidr, char *first_ip, char *last_ip) {
struct in_addr ip;
inet_pton(AF_INET, ip_str, &ip);

unsigned long ip_long = ntohl(ip.s_addr);

unsigned long subnet_mask = (0xFFFFFFFFUL << (32 - cidr));

unsigned long first_ip_long = ip_long & subnet_mask;
unsigned long last_ip_long = ip_long | (~subnet_mask);

struct in_addr first_ip_addr, last_ip_addr;
first_ip_addr.s_addr = htonl(first_ip_long);
last_ip_addr.s_addr = htonl(last_ip_long);

inet_ntop(AF_INET, &first_ip_addr, first_ip, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &last_ip_addr, last_ip, INET_ADDRSTRLEN);
}

// Function to determine IP class and calculate relevant information
void process_ip_address(char *ip_str, char *response) {
char *cidr_str = strchr(ip_str, '/');

if (cidr_str != NULL) {
// Classless (CIDR) IP address
int cidr = atoi(cidr_str + 1);
char ip_only[INET_ADDRSTRLEN];
strncpy(ip_only, ip_str, cidr_str - ip_str);
ip_only[cidr_str - ip_str] = '';

char first_ip[INET_ADDRSTRLEN], last_ip[INET_ADDRSTRLEN];
calculate_first_last_ip(ip_only, cidr, first_ip, last_ip);

sprintf(response, "IP Address: %s is Classless (CIDR)nFirst Address: %snLast Address: %snNumber of Hosts: %lu",
ip_str, first_ip, last_ip, calculate_hosts(cidr));
} else {
// Classful IP address
struct in_addr ip;
inet_pton(AF_INET, ip_str, &ip);

unsigned char first_octet = (unsigned char) (ip.s_addr >> 24);

char ip_class;
if (first_octet < 128) {
ip_class = 'A';
} else if (first_octet < 192) {
ip_class = 'B';
} else if (first_octet < 224) {
ip_class = 'C';
} else {
sprintf(response, "Class D or E, not applicable for host calculations.");
return;
}

char first_ip[INET_ADDRSTRLEN], last_ip[INET_ADDRSTRLEN];
if (ip_class == 'A') {
sprintf(first_ip, "%d.0.0.0", first_octet);
sprintf(last_ip, "%d.255.255.255", first_octet);
sprintf(response, "IP Address: %s is Classful (%c)nFirst Address: %snLast Address: %snNumber of Hosts: %lu",
ip_str, ip_class, first_ip, last_ip, (1UL << 24) - 2);
} else if (ip_class == 'B') {
sprintf(first_ip, "%d.0.0.0", first_octet);
sprintf(last_ip, "%d.255.255.255", first_octet);
sprintf(response, "IP Address: %s is Classful (%c)nFirst Address: %snLast Address: %snNumber of Hosts: %lu",
ip_str, ip_class, first_ip, last_ip, (1UL << 16) - 2);
} else if (ip_class == 'C') {
sprintf(first_ip, "%d.0.0.0", first_octet);
sprintf(last_ip, "%d.255.255.255", first_octet);
sprintf(response, "IP Address: %s is Classful (%c)nFirst Address: %snLast Address: %snNumber of Hosts: %lu",
ip_str, ip_class, first_ip, last_ip, (1UL << 8) - 2);
}
}
}

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char response[BUFFER_SIZE] = {0};

// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Bind socket to port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}

// Listen for connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}

printf("Server listening on port %d...n", PORT);

while (1) {
// Accept incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
continue;
}

printf("Connection accepted from client IP address...n");

// Read data from client
read(new_socket, buffer, BUFFER_SIZE);
printf("Received IP address: %sn", buffer);

// Process IP address and send response back to client
process_ip_address(buffer, response);
send(new_socket, response, strlen(response), 0);

printf("Response sent to client.n");

// Close the socket
close(new_socket);
}

return 0;
}




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

#define PORT 8080
#define BUFFER_SIZE 256

int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[BUFFER_SIZE] = {0};

// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
perror("inet_pton failed");
exit(EXIT_FAILURE);
}

// Connect to server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}

printf("Connected to server...n");

// Get IP address from user
printf("Enter IP address (with or without CIDR notation): ");
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "n")] = 0; // Remove newline character

// Send IP address to server
send(sock, buffer, strlen(buffer), 0);

// Receive response from server
read(sock, buffer, BUFFER_SIZE);
printf("Server response:n%sn", buffer);

// Close the socket
close(sock);

return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.