You are on page 1of 20

LAB ASSIGNMENT-3

19BCT0015
CH.THARUN TEJ

1. Ip adress specifying if its IPv4 or v6. If v4 then specify class,


netid,hosted
CODE:
#include <iostream>
#include <string>
#include <sstream>
#include <arpa/inet.h>
using std::string;
using std::cout;
using std::endl;
using std::stringstream;
using namespace std;

char findClass(char str[])


{

char arr[4];
int i = 0;
while (str[i] != '.')
{
arr[i] = str[i];
i++;
}
i--;

int ip = 0, j = 1;
while (i >= 0)
{
ip = ip + (str[i] - '0') * j;
j = j * 10;
i--;
}

if (ip >=1 && ip <= 126)


return 'A';

else if (ip >= 128 && ip <= 191)


return 'B';

else if (ip >= 192 && ip <= 223)


return 'C';

else if (ip >= 224 && ip <= 239)


return 'D';

else
return 'E';
}
void separate(char str[], char ipClass)
{

char network[12], host[12];


for (int k = 0; k < 12; k++)
network[k] = host[k] = '\0';

if (ipClass == 'A')
{
int i = 0, j = 0;
while (str[j] != '.')
network[i++] = str[j++];
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}

else if (ipClass == 'B')


{
int i = 0, j = 0, dotCount = 0;

while (dotCount < 2)


{
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}
i = 0;
j++;

while (str[j] != '\0')


host[i++] = str[j++];

printf("Network ID is %s\n", network);


printf("Host ID is %s\n", host);
}

else if (ipClass == 'C')


{
int i = 0, j = 0, dotCount = 0;

while (dotCount < 3)


{
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}

i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];

printf("Network ID is %s\n", network);


printf("Host ID is %s\n", host);
}

else
printf("In this Class, IP address is not"
" divided into Network and Host ID\n");
}

bool is_ipv4_address(const string& str);


bool is_ipv6_address(const string& str);
bool is_valid_domain_name(const string& str);

int main(int argc, char* argv[])


{
char address[100];
printf("Enter the IP address you want to check : ");
cin >> address;
if(is_ipv4_address(address))
{
printf("This is an IPv4 address.");
char ipClass = findClass(address);
printf("Given IP address belongs to Class %c\n",ipClass);
separate(address, ipClass);

}
else if(is_ipv6_address(address))
{
printf("This is an IPv6 address.");
}
else
{
printf("This is an invalid address, that is , neither IPv4 nor IPv6");
}
}

bool is_ipv4_address(const string& str)


{
struct sockaddr_in sa;
return inet_pton(AF_INET, str.c_str(), &(sa.sin_addr))!=0;
}
bool is_ipv6_address(const string& str)
{
struct sockaddr_in6 sa;
return inet_pton(AF_INET6, str.c_str(), &(sa.sin6_addr))!=0;
}

OUTPUT:
2. To carry out Subnetting for entered IP Adddress

Code:
import java.io.*;
import java.util.*;
public class Subnet {

static String sq = "";


public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int x=0;
String s,s1,sx;
System.out.print("ENTER IP: ");
s= br.readLine();
s1 = s.substring(0,3);
s1 = s1.replace(".","");
int a = Integer.parseInt(s1);

if(a>=0 && a<=127){


String dm = "255.0.0.0";
x=1;
System.out.println("CLASS A");
System.out.println("DEFAULT MASS : " +dm);
}else if (a>=128 && a<=191){
String dm = "255.255.0.0";
x=2;
System.out.println("CLASS B");
System.out.println("DEFAULT MASS : " +dm);
}else if(a>=192 && a<=223){
String dm = "255.255.255.0";
x=3;
System.out.println("CLASS C");
System.out.println("DEFAULT MASK : " +dm);
}
else System.out.println("OUT OF RANGE");
assert x != 0 : "PROGRAM CANNOT RUN FURTHER ";
if(x==1){
sx = s.substring(0,3);
sx = sx.concat(".0.0.0");
sx = sx.replace("...","..");
sx = sx.replace("..",".");
System.out.println("BITWISE ADDING: " +sx);
}else if(x==2){
sx = s.substring(0,7);
sx = sx.concat(".0.0");
sx = sx.replace("...","..");
sx = sx.replace("..",".");
System.out.println("BITWISE ADDING: " +sx);
}else if(x==3){
sx = s.substring(0,11);
sx = sx.concat(".0");
sx = sx.replace("...","..");
sx = sx.replace("..",".");
System.out.println("BITWISE ADDING: " +sx);

}
System.out.print("ENTER SUBNET VALUE(1,2,3,4,5,6,7,8): ");
int z = Integer.parseInt(br.readLine());
convert(x,z);
}

static void convert(int x,int z){


int count=0;
while(z!=0){
sq = sq.concat("1");
z--;
count++;
}

while(count!=8){
sq = sq.concat("0");
count++;
}

if(x==1){
System.out.println("11111111." + sq + ".00000000.00000000");
}
if(x==2){
System.out.println("11111111.11111111." + sq + ".00000000");
}
if(x==3){
System.out.println("11111111.11111111.11111111." + sq + "");
}
}
}
3.Make Server client socket program for 1)TCP 2)UDP

1.TCP
SERVER CODE:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
void *connection_handler(void *);

int main(int argc , char *argv[])


{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection


puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//Accept and incoming connection


puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;

if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*)


new_sock) < 0)
{
perror("could not create thread");
return 1;
}

//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}

if (client_sock < 0)
{
perror("accept failed");
return 1;
}

return 0;
}

void *connection_handler(void *socket_desc)


{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];

//Send some messages to the client


message = "Greetings! I am your connection handler\n";
write(sock , message , strlen(message));

message = "Now type something and i shall repeat what you type \n";
write(sock , message , strlen(message));

//Receive a message from client


while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}

//Free the socket pointer


free(socket_desc);

return 0;
}

Client code:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc , char *argv[])


{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");

server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server


if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}

puts("Connected\n");

//keep communicating with server


while(1)
{
printf("Enter message : ");
scanf("%s" , message);

//Send some data


if( send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}

//Receive a reply from the server


if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}

puts("Server reply :");


puts(server_reply);
}

close(sock);
return 0;
}
3. UDP CLIENT SERVER PROGRAM
SERVER CODE:

#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#define PORT 5000
#define MAXLINE 1000

// Driver code
int main()
{
char buffer[100];
char *message = "Hello Client";
int listenfd, len;
struct sockaddr_in servaddr, cliaddr;
bzero(&servaddr, sizeof(servaddr));

// Create a UDP Socket


listenfd = socket(AF_INET, SOCK_DGRAM, 0);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;

// bind server address to socket descriptor


bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

//receive the datagram


len = sizeof(cliaddr);
int n = recvfrom(listenfd, buffer, sizeof(buffer),
0, (struct sockaddr*)&cliaddr,&len); //receive message from server
buffer[n] = '\0';
puts(buffer);

// send the response


sendto(listenfd, message, MAXLINE, 0,
(struct sockaddr*)&cliaddr, sizeof(cliaddr));
}

CLIENT CODE:
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>

#define PORT 5000


#define MAXLINE 1000

// Driver code
int main()
{
char buffer[100];
char *message = "Hello Server";
int sockfd, n;
struct sockaddr_in servaddr;

// clear servaddr
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;

// create datagram socket


sockfd = socket(AF_INET, SOCK_DGRAM, 0);

// connect to server
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("\n Error : Connect Failed \n");
exit(0);
}

// request to send datagram


// no need to specify server address in sendto
// connect stores the peers IP and port
sendto(sockfd, message, MAXLINE, 0, (struct sockaddr*)NULL, sizeof(servaddr));

// waiting for response


recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)NULL, NULL);
puts(buffer);

// close the descriptor


close(sockfd);
}

You might also like