Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 5

Set-1

Name: Swayam Smruti Dash


Roll No: 120CS0150

1. Consider the captured file DNS.pcap and answer the followings:

a. To what IP address is the DNS query message sent? Can this


IP address be the default local DNS server?
Ans:
The DNS query message is sent to 18.72.0.3
Yes, it is the default local DNS server IP address

b. Examine the DNS query message. What “Type” of DNS query is it? Does the
query message contain any “answers”?
Ans:
It is a query message of type “PTR”.
It doesnt contain any answers.

c. Examine the DNS response message. What MIT name servers does the response
message provide?
Ans:
The nameservers are:
1. W20NS.MIT.EDU
2. BITSY.MIT.EDU
3. STRAWB.MIT.EDU
d. Does this response message also provide the IP addresses of
the MIT name servers?
Ans:
Yes, they do provide the IP addresses of the MIT nameservers

2. Write a TCP socket program to divide the two numbers using command line
arguments (The IP address of the server should be entered along with the execution
command for executing the program of client machine). Use all the appropriate
socket functions.
Example:
$. /client 127.0.0.1
Input at client : 32 8
Output at server: 4

Ans:
Client:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int client_socket;
struct sockaddr_in server;
int num1, num2, result;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1)
{
printf("Could not create socket");
return 1;
}
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(7000);
if (connect(client_socket, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Connect failed");
return 1;
}
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
num1 = htonl(num1);
num2 = htonl(num2);
send(client_socket, &num1, sizeof(num1), 0);
send(client_socket, &num2, sizeof(num2), 0);
recv(client_socket, &result, sizeof(result), 0);
// result = result;
printf("Result: %d\n", result);
close(client_socket);
return 0;
}

Server:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>
int res(int n1, int n2)
{
if(n1<0 && n2<0) return n1/n2;
//else if((n1<0 && n2<0) || (n1>0 && n2<0)) return (-n1/n2);
else if((n1!=0 && n2!=0 && (n1<n2)) || (n1==0)) return 0;
else if(n1!=0 && n2==0) return 0;
else
return n1/n2;
}

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


{
int server_socket, client_socket;
struct sockaddr_in server, client;
int c;
int num1, num2, result;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1)
{
printf("Could not be able to create socket");
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(7000);
if (bind(server_socket, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Bind failed");
return 1;
}
listen(server_socket, 3);
printf("Waiting for the incoming connections !!!\n");
c = sizeof(struct sockaddr_in);
while ((client_socket = accept(server_socket, (struct sockaddr *)&client,
(socklen_t *)&c)))
{
printf("Connection has been accepted from %s:%d\n",
inet_ntoa(client.sin_addr),ntohs(client.sin_port));
recv(client_socket, &num1, sizeof(num1), 0);
recv(client_socket, &num2, sizeof(num2), 0);
result = res(ntohl(num1), ntohl(num2));
send(client_socket, &result, sizeof(result), 0);
close(client_socket);
printf("Connection is closed\n");
}
if (client_socket < 0)
{
printf("Accept has been failed");
return 1;
}
return 0;
}
Output:

Server:

Client:

You might also like