You are on page 1of 17

Rajkiya Engineering College

Kannauj

Practical File
CN Lab(RCS-651)
Session: - 2019-2020

Submitted by: - Submitted To: -


Sumit Sharma Mr. Naveen Tiwari
1883910911 Asst. Professor
Computer Science and Engineering Department of Computer Science
IIIrd Year & Engineering
INDEX

S NO. EXPERIMMENT REMARKS

Running and using services/commands like ping,


1. traceroute, nslookup, arp, telnet, ftp, etc.

2. Server side implementation of TCP client-server model

3. Client side implementation of TCP client server model

4. Socket programming using UDP and TCP.

5. Programming using raw sockets

6. Programming using RPC

Simple DNS Server


7.
Practical: - 01
Aim: - Running and using services/commands like ping, traceroute, nslookup, arp, telnet,
ftp, etc.

Software Requirement: Command Prompt and Packet Tracer.

ping: ping(8) sends an ICMP ECHO_REQUEST packet to the specified host. If the host
responds, you get an ICMP packet back.

Traceroute: Tracert is a command which can show you the path a packet of information takes
from your computer to one you specify. It will list all the routers it passes through until it reaches
its destination, or fails to and is discarded. In addition to this, it will tell you how long each 'hop'
from router to router takes.
nslookup: Displays information from Domain Name System (DNS) name servers.

arp: Using the arp command allows you to display and modify the Address Resolution
Protocol(ARP) cache. An ARP cache is a simple mapping of IP addresses to MAC addresses.

Telnet: Telnet uses the telnet protocol to connect to a remote computer over a network. You can
execute telnet from the command prompt.

ftp : The ftp command uses the File Transfer Protocol(FTP) to transfer files between the local
host and a remote host or between two remote hosts.
Practical: - 02
Objective: Server side implementation of TCP client-server model

#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr

/ Function designed for chat between client and server.


void func(int sockfd)
{
char buff[MAX]; int n;
/ infinite loop for chat
for (;;) {
bzero(buff, MAX);
/ read the message from client and copy it in
buffer read(sockfd, buff, sizeof(buff));
/ print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
/ copy server message in the buffer
while ((buff[n++] = getchar()) != '\n');
/ and send that buffer to client
write(sockfd, buff, sizeof(buff));
/ if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {

printf("Server Exit...\n");
break;
}
}
}
/ Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;

// socket create and verification


sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd == -1) {

printf("socket creation failed...\n");


exit(0);

}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT


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

/ Binding newly created socket to given IP and verification if


((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {

printf("socket bind failed...\n");


exit(0);

}
else
printf("Socket successfully binded..\n");
/ Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);

}
else
printf("Server listening..\n");

len = sizeof(cli);

/ Accept the data packet from client and verification


connfd = accept(sockfd, (SA*)&cli, &len);

if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);

}
else

printf("server acccept the client...\n");

/ Function for chatting between client and


server func(connfd);

/ After chatting close the socket


close(sockfd);

}
Practical: - 03
Aim: Client side implementation of TCP client-server model.

Program:

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;

// socket create and varification


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
Else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT


servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr =
inet_addr("127.0.0.1"); servaddr.sin_port =
htons(PORT);

// connect the client socket to server socket


if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0)
{ printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");

/ function for
chat func(sockfd);

/ close the
socket
close(sockfd);
}
Practical:- 04
Aim: Socket programming using UDP and TCP.

Program:

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0}; char
*hello = "Hello from server";
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0
);
printf("Hello message sent\n");
return 0;
}
Client Program:-

#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include<unistd.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
struct sockaddr_in address; int sock =
0, valread; struct sockaddr_in
serv_addr; char *hello = "Hello
from client"; char buffer[1024]
= {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET, "127.0.0.1",
&serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) ,
0 ); printf("Hello message
sent\n"); valread =read( sock ,
buffer, 1024); printf("%s\n",buffer
); return 0;
}
Practical: - 05

Objective: Programming using raw sockets.

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h> #include <arpa/inet.h>
static int s = -1; static int s1 = -1;
void onexit(int signum)
{
(void)signum; printf("Exiting"); close(s);
close(s1);
}
int main()
{
char buf[1600]; ssize_t recv_size = -1; ssize_t send_size = -1; int i = 0;
struct sockaddr_ll socket_address, socket_address1;
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); s1 = socket(AF_PACKET, SOCK_RAW,
htons(ETH_P_ALL));
if ((s == -1) || (s1 == -1))
{
perror("Socket creation failed"); exit (0);
}
else
printf("Successful Socket Creation");
signal(SIGINT, onexit);
memset(&socket_address, 0, sizeof (socket_address)); socket_address.sll_family =
PF_PACKET; socket_address.sll_ifindex = if_nametoindex("eth0");
socket_address.sll_protocol = htons(ETH_P_ALL);
i = bind(s, (struct sockaddr*)&socket_address, sizeof(socket_address)); if (i == -1)
{
perror("Bind"); exit (0);
}
memset(&socket_address1, 0, sizeof (socket_address1)); socket_address1.sll_family =
PF_PACKET; socket_address1.sll_ifindex = if_nametoindex("eth1");
socket_address1.sll_protocol = htons(ETH_P_ALL);
i = bind(s1, (struct sockaddr*)&socket_address1, sizeof(socket_address1));
if (i == -1)
{
perror("Bind"); exit (0);
}
while (1)
{
memset(&buf, 0, sizeof(buf));
recv_size = recv(s, &buf, sizeof(buf), 0); if (recv_size == -1)
{
perror("Socket receive"); exit (0); }
printf("\n");
for(i=0; i < recv_size; i++)
{
printf("%02x ", buf[i]);
}
send_size = send(s1, &buf, recv_size, 0); if (send_size == -1)
{
perror("Socket send"); exit (0);
}
}
return 0;
}
Practical: - 06
Programming using RPC .

//client side program


#include"errno.h"
#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h" #include"math.h"
int main(int argc,char **argv){
CLIENT *cl; square_in in;
square_out *outp;
f(argc!=3)
{
printf("\n\n error:insufficient arguments!!!"); exit(-1);
}
cl=clnt_create(argv[1],SQUARE_PROG,SQUARE_VERS,"tcp"); in.arg1=atol(argv[2]);
if(cl==NULL)
{
printf("\nerror:%s",strerror(errno)); exit(-1);
}
if((outp=squareproc_1(&in,cl))==NULL)
{
printf("\nerror :%s",clnt_sperror(cl,argv[1])); exit(-1);
}
printf("\n\n result is : %ld",outp->res1); exit(0);
}
// .h FILENAME: square.h
struct square_in
{
/*input arg*/ long arg1; };
struct square_out
{
/*op result*/ long res1; };
//server side program
#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h" #include"math.h"
square_out *squareproc_1_svc(square_in *inp,struct svc_req *rqstp)
{
static square_out out; out.res1= inp->arg1 * inp-
>arg1; return(&out);
}
Practical: - 07
Aim: Simple DNS server.

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include "dns_protocol.h"
#include "server.h"
struct dns_server dns_server;
/*
* initialize dns_server
*/
void dns_init (void)
{
printf ("Initializing DNS server.\n");
dns_server.config.config_file = NULL;
dns_server.config.fg = 0;
dns_server.config.port = 53;
dns_server.config.host = "0.0.0.0";
dns_server.listenfd = 0;
}
/*
* open listen sockets and set flags
*/
void open_sockets (void)
{
struct sockaddr_in sin;
int sd;
printf ("Opening sockets.\n");
/* TODO - O_CLOEXEC and O_NONBLOCK */
assert ((sd = socket (AF_INET, SOCK_DGRAM, 0)) > 0);
memset ((char *) &sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (dns_server.config.port);
sin.sin_addr.s_addr = inet_addr (dns_server.config.host);
assert (bind (sd, (struct sockaddr *) &sin, sizeof (sin)) == 0);
dns_server.listenfd = sd;
}
/*after parsing configuration, start the dns_server */
void dns_start (void)
{
printf ("Starting DNS server.\n");
openlog ("simple-dns", LOG_PID, LOG_LOCAL0);
open_sockets ();
}
/*
* loop accepting new
requests */
void dns_loop (void)
{
int req_size;
char buf[PACKET_SIZE+4];
socklen_t from_len;
struct sockaddr_in from;
struct dns_packet *pkt;
from_len = sizeof (from);
printf ("Accepting connections...\n");
for (;;)
{
req_size = recvfrom (dns_server.listenfd, buf, PACKET_SIZE + 4, 0,
(struct sockaddr *) &from, &from_len);
printf ("client: %s %d\n", strerror (errno), req_size);
pkt = calloc (1, sizeof (struct dns_packet));
dns_request_parse (pkt, buf, req_size);
dns_print_packet (pkt);
free (pkt->data);
free (pkt);
}
}

You might also like