Rajkiya Engineering College Kannauj: Practical File
Rajkiya Engineering College Kannauj: Practical File
Kannauj
Practical File
CN Lab(RCS-651)
Session: - 2019-2020
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
printf("Server Exit...\n");
break;
}
}
}
/ Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
if (sockfd == -1) {
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
}
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);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
}
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;
/ 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
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 .
#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);
}
}