0% found this document useful (0 votes)
6 views57 pages

CN LAB File

The document outlines a Computer Networks Lab course for Bachelor of Technology students at KCC Institute of Technology and Management. It details various experiments focusing on networking hardware configuration, network commands, IP address study, packet analysis using Wireshark, and simulations using Cisco Packet Tracer. Each experiment includes objectives, procedures, and relevant commands to facilitate hands-on learning in networking concepts.

Uploaded by

tushar3191sh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views57 pages

CN LAB File

The document outlines a Computer Networks Lab course for Bachelor of Technology students at KCC Institute of Technology and Management. It details various experiments focusing on networking hardware configuration, network commands, IP address study, packet analysis using Wireshark, and simulations using Cisco Packet Tracer. Each experiment includes objectives, procedures, and relevant commands to facilitate hands-on learning in networking concepts.

Uploaded by

tushar3191sh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KCC INSTITUTE OF TECHNOLOGY AND

MANAGEMENT

“COMPUTER NETWORKS LAB – BCS 653”

BACHELOR OF TECHNOLOGY

Dr. A.P.J. Abdul Kalam Technical University,


LUCKNOW
SESSION (2025-2026)
Submitted By: Submitted To:
Ms. Sonal Yadav
Piyush Kumar

(CSIT)
2304920110103
Date of Date of Date of Faculty
S. No Experiment Name
Issue Done Check Signature

To learn handling and configuration of


1 networking hardware like RJ-45 connector,
CAT-6 cable, crimping tool, etc.

Running and using services/commands like


2
ping, traceroute, lookup, arp, telnet, ftp etc.

3 Study of a network of IP

Network Packet Analysis using tools like


4
Wireshark.

Simulation of network topologies using Packet


5
Tracer.

6 Router configuration using packet tracer.

WAP using TCP sockets (like date and time


7
server, echo server and client etc.)

8 WAP using UDP sockets (like simple DNS)

WAP using RAW sockets (packet capturing


9
and filtering)

10 WAP using RPC/RMI


Experiment -1
Objective: To learn handling and configuration of networking hardware like RJ-45 connector, CAT-6 cable,
crimping tool, etc.
(i). Straight Cable
(ii). Cross-wired Cable
(iii). Repeater
(iv). Hub
(v). Switch
(vi). Bridge
(vii). Router
(viii). Gate Way

Apparatus (Components): RJ-45 connector, Clamping Tool, Twisted pair Cable


Procedure: To do these practical following steps should be done:
1. Start by stripping off about 2 inches of the plastic jacket off the end of the cable. Be very careful at this
point, as to not nick or cut into the wires, which are inside. Doing so could alter the characteristics of your
cable, or even worse render is useless. Check the wires, one more time for nicks or cuts. If there are any,
just whack the whole end off, and start over.
2. Spread the wires apart, but be sure to hold onto the base of the jacket with your other hand. You do not
want the wires to become untwisted down inside the jacket. Category 5 cable must only have 1/2 of an
inch of 'untwisted' wire at the end; otherwise it will be 'out of spec'. At this point, you obviously have ALOT
more than 1/2 of an inch of un-twisted wire.
3. You have 2 end jacks, which must be installed on your cable. If you are using a pre-made cable, with
one of the ends whacked off, you only have one end to install - the crossed over end. Below are two
diagrams, which show how you need to arrange the cables for each type of cable end. Decide at this point
which end you are making and examine the associated picture below.
Diagram 1.1 shows you how to prepare straight through wired connect

Fig1.1 RJ-45 Straight Through Ethernet Connection


Diagram 1.2 Diagram shows you how to prepare Cross wired connection

Fig1.2 RJ-45 Cross Over Ethernet Connection


Experiment -2
Objective:
a) Connect the computers in Local Area Network.
b) Study of basic network command and Network configuration commands.

By this Students have to understand basic networking commands e.g ping, tracert etc. All commands related
to Network configuration which includes how to switch to privilege mode and normal mode and how to
configure router interface and how to save this configuration to flash memory or permanent memory.
This commands includes
 Configuring the Router commands
 General Commands to configure network
 Privileged Mode commands of a router
 Router Processes & Statistics
 IP Commands
 Other IP Commands e.g. show ip route etc.

(A). ipconfig command- The IPConfig command will display basic IP address configuration information for
the device. Simply type IPConfig at the Windows command prompt, and you will be presented with the IP
address, subnet mask, and default gateway that the device is currently using.
(B).Command ipconfig/all- IPConfig/all causes Windows to display IP address configuration that is much
more verbose. This is also the command that you will have to use if you want to see which DNS server the
Windows device is configured to use.
(C).Command nslookup- NSLookup is a great utility for diagnosing DNS name resolution problems. Just type
the NSLookup command, and Windows will display the name and IP address of the device’s default DNS
server. From there, you can type host names in an effort to see if the DNS server is able to resolve the specified
host name.

(D).Command ping - Ping is used to test the ability of one network host to communicate with another. Simply
enter the Ping command, followed by the name or the IP address of the destination host. Assuming that there
are no network problems or firewalls preventing the ping from completing, the remote host will respond to the
ping with four packets. Receiving these packets rms that a valid and functional network path exists between the
two hosts.

(E).Command tracert- Tracert, or “Trace Route,” is a utility for examining the path to a remote host.
Functionally, Tracert works similarly to Ping. The major difference is that Tracert sends a series of ICMP echo
requests, and the request’s TTL increased by 1 each time. This allows the utility to display the routers through
which packets are passing to be identified. When possible, Windows displays the duration and IP address or
fully qualified domain name of each hop.
Experiment-3
Program Objective:
Study of network IP.
Procedure: To do this EXPERIMENT- follows these steps:
• Classification of IP address

As show in figure we teach how the IP addresses are classified and when they are used.

Class Address Range


Class A [Link] to [Link]
Class B [Link] to [Link]
Class C [Link] to [Link]
Class D [Link] to [Link]
Class E [Link] to [Link]

To Connect the computers in Local Area Network.

On the host computer, follow these steps to share the Internet connection:

1) Log on to the host computer as Administrator or as Owner.


2) Click Start, and then click Control Panel.
3) Click Network and Internet Connections.
4) Click Network Connections.
5) Right-click the connection that you use to connect to the Internet. For example, if you connect
to the Internet by using a modem, right-click the connection that you want under Dial-up / other network
available.
6) Click Properties.
7) Click the Advanced tab.
8) Under Internet Connection Sharing, select the Allow other network users to connect through
this computer's Internet connection check box.
9) If you are sharing a dial-up Internet connection, select the Establish a dial-up connection whenever
a computer on my network attempts to access the Internet check box if you want to permit your
computer to automatically connect to the Internet.
10) Click OK. You receive the following message:
When Internet Connection Sharing is enabled, your LAN adapter will be set to use IP address [Link].
Your computer may lose connectivity with other computers on your network. If these other computers have
static IP addresses, it is a good idea to set them to obtain their IP addresses automatically. Are you sure you
want to enable Internet Connection Sharing?
11) Click Yes.
The connection to the Internet is shared to other computers on the local area network (LAN).

The network adapter that is connected to the LAN is configured with a static IP address of [Link]
and a subnet mask of [Link]

On the client computer


To connect to the Internet by using the shared connection, you must confirm the LAN adapter IP
configuration, and then configure the client computer. To confirm the LAN adapter IP configuration,
follow these steps:

1) Log on to the client computer as Administrator or as Owner.


2) Click Start, and then click Control Panel.
3) Click Network and Internet Connections.
4) Click Network Connections.
5) Right-click Local Area Connection and then click Properties.
6) Click the General tab, click Internet Protocol (TCP/IP) in the connection uses the following
items list, and then click Properties.
7) In the Internet Protocol (TCP/IP) Properties dialog box, click Obtain an IP address
automatically (if it is not already selected), and then click OK.
Note: You can also assign a unique static IP address in the range of [Link] to [Link].
For example, you can assign the following static IP address, subnet mask, and default gateway:

8) IP Address [Link]
9) Subnet mask [Link]
10) Default gateway [Link]
11) In the Local Area Connection Properties dialog box, click OK.
12) Quit Control Panel.
Experiment-4
Program Objective: How to analyze the data over the network and keeping track of particular protocols
Program Prerequisites: System installed with Wireshark
Wireshark is a free and open-source packet analyzer. It is used for network troubleshooting, analysis, software
and communications protocol development, and education. Wireshark lets the user put network interface
controllers into promiscuous mode (if supported by the network interface controller), so they can see all the
traffic visible on that interface including unicast traffic not sent to that network interface controller's MAC
address.
Features of Wireshark.

Data can be captured "from the wire" from a live network connection or read from a file of already-captured
packets. Live data can be read from different types of networks, including Ethernet, IEEE 802.11, PPP,
and loopback. Captured network data can be browsed via a GUI, or via the terminal (command line) version of
the utility, TShark.

Captured files can be programmatically edited or converted via command-line switches to the "editcap"
program. Data display can be refined using a display filter. Plug-ins can be created for dissecting new protocols.
VoIP calls in the captured traffic can be detected. If encoded in a compatible encoding, the media flow can even
be played. Raw USB traffic can be captured. Wireless connections can also be filtered as long as they traverse
the monitored Ethernet.

Various settings, timers, and filters can be set to provide the facility of filtering the output of the captured traffic.

4.1 Apply No filter to see all the packets send and received.
4.2 Apply filter such as TCP/UDP to see packet from that particular protocol.

4.3 Open a website ([Link]) and analyze those packets.


4.4 Type a particular IP address and analyse packets of that address.
Experiment-5:
Program Objective: Simulation of connecting two or more devices with each other
Program Prerequisites: System installed with Cisco Packet Tracer
Packet Tracer is a cross-platform visual simulation tool designed by Cisco Systems that allows users to
create network topologies and imitate modern computer networks. The software allows users to simulate the
configuration of Cisco routers and switches using a simulated command line interface. Packet Tracer makes use
of a drag and drop user interface, allowing users to add and remove simulated network devices as they see fit.
Packet Tracer allows students to design complex and large networks, which is often not feasible with physical
hardware, due to costs. Packet Tracer is commonly used by CCNA Academy students, since it is available to
them for free. However, due to functional limitations, it is intended by CISCO to be used only as a learning aid,
not a replacement for Cisco routers and switches. The application itself only has a small number of features
found within the actual hardware running a current Cisco IOS version. Thus, Packet Tracer is unsuitable for
modelling production networks. It has a limited command set, meaning it is not possible to practice all of the
IOS commands that might be required. Packet Tracer can be useful for understanding abstract networking
concepts, such as the Enhanced Interior Gateway Routing Protocol by animating these elements in a visual
form. Packet Tracer is also useful in education by providing additional components, including an authoring
system, network protocol simulation and improving knowledge an assessment system
Steps to install Cisco Packet Tracer.
STEP 1 Download Cisco Packet tracer from the website [Link]
STEP-2 After Cisco Packet Tracer download, click on the downloaded exe file. Once below Window will
appear, click the “Next” option
STEP-3 On the next screen, select “I accept the agreement” and click on “Next”.
STEP-4 Setup will show the folder in which the program’s shortcuts will be created. If you want to change the
folder, you can change it. Click on “Next”.
STEP-5 Then the program will ask whether to create a Desktop icon and create a Quick Launch icon. Make
your choice and click on “Next”.
STEP-6 The summary of the settings we selected is displayed. Click on “Install”.
STEP-7 Cisco packet tracer Installation gets completed and the below screen is shown. Click on “Finish”.
Click “OK” on next popup asking you to close or restart your computer.
5.1 Design a network with two system connected to each other with cross-over cable and then assign IP
address to them.
5.2 Open Command prompt in each system and run all ip commands.
For PC-PT PC0
5.2.1 Ipconfig command
5.2.2 Ipconfig /all command

5.2.3 Ping command


5.3 For PC-PT PC1
5.3.1 ipconfig command

5.3.2 ipconfig /all command


5.3.3 ping command
Experiment-6:
Program Objective: Simulation of sending data from one device to another in a network using HUB
Program Prerequisites: System installed with Cisco Packet Tracer
1. There is a hub connected to four systems. Every system is connected separately to hub with the straight
cable.
2. All the systems are not connected with each other directly.
3. We assigned the IP address for each system uniquely.
4. When we send the data one system to another system through hub.
That is shown in the picture.
6.1 When one system send the data to other system – The data first goes to the hub and then hub broadcast
that data.

6.2 The data goes to all system which are connected to hub except sender but it can get that system for
which that data was sent and rest all will not get that it show cross on that system. The output is shown
in picture
6.3 When the receiver send the ack to the sender-that the data has been received it also go through the hub

6.4 The hub send the acknowledgement to the receiver.


EXPERIMENT- 7

PROGRAM USING TCP SOCKETS

1) DATE AND TIME SERVER


Program Objective: To implement date and time display from local host to server using TCP

Program Prerequisites: System installed with C and JAVA compiler (512 MB RAM, 32GB HDD)

ALGORITHM: CLIENT

1. Start the program

2. Include necessary package in java

3. To create a socket in client to server.

4. the client connection accept to the server and replay to read the system date and time.

5. Stop the program.

ALGORITHM: SERVER

[Link] the program

2. Include necessary package in java

3. To create a socket in server to client.

4. To display the current date and time to client

5. Stop the program.


Program Code:
DATECLIENT:
import [Link].*;
import [Link].*;
class dateclient
{
public static void main (String args[])
{
Socket soc;
DataInputStream dis;
String sdate;
PrintStream ps;
try
{
InetAddress ia=[Link]();
soc=new Socket(ia,8020);
dis=new DataInputStream([Link]());
sdate=[Link]();
[Link]("THE date in the server is:"+sdate);
ps=new PrintStream([Link]());
[Link](ia);
}
catch(IOException e)
{
[Link]("THE EXCEPTION is :"+e);
}
}
}
DATESERVER:
import [Link].*;
import [Link].*;
import [Link].*;
class dateserver
{
public static void main(String args[])
{
ServerSocket ss;
Socket s;
PrintStream ps;
DataInputStream dis;
String inet;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=[Link]();
ps=new PrintStream([Link]());
Date d=new Date();
[Link](d);
dis=new DataInputStream([Link]());
inet=[Link]();
[Link]("THE CLIENT SYSTEM ADDRESS IS :"+inet);
[Link]();
}
}
catch(IOException e)
{
[Link]("The exception is :"+e);
}
}
}
OUTPUT:

CLIENTSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateclient
THE date in the server is:Sat Jul 19 13:01:16 GMT+05:30 2008
C:\Program Files\Java\jdk1.5.0\bin>

SERVERSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateserver
THE CLIENT SYSTEM ADDRESS IS :com17/[Link]
1) CLIENT-SERVER APPLICATION FOR CHAT
Program Objective: To write a client-server application for chat using TCP

ALGORITHM: CLIENT

1. Start the program

2. Include necessary package in java

3. To create a socket in client to server.

4. The client establishes a connection to the server.

5. The client accept the connection and to send the data from client to server and vice versa

6. The client communicate the server to send the end of the message

7. Stop the program.

ALGORITHM: SERVER

1. Start the program

2. Include necessary package in java

3. To create a socket in server to client

4. The server establishes a connection to the client.

5. The server accept the connection and to send the data from server to client and vice versa

6. The server communicate the client to send the end of the message

7. Stop the program.

Program Code:
[Link]
import [Link].*;
import [Link].*;
public class TCPserver1
{
public static void main(String arg[])
{
ServerSocket s=null;
String line;
DataInputStream is=null,is1=null;
PrintStream os=null;
Socket c=null;
try
{
s=new ServerSocket(9999);
}
catch(IOException e)
{
[Link](e);
}
try
{
c=[Link]();
is=new DataInputStream([Link]());
is1=new DataInputStream([Link]);
os=new PrintStream([Link]());
do
{
line=[Link]();
[Link]("Client:"+line);
[Link]("Server:");
line=[Link]();
[Link](line);
}while([Link]("quit")==false);
[Link]();
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}

[Link]
import [Link].*;
import [Link].*;
public class TCPclient1
{
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("[Link]",9999);
}
catch(IOException e)
{
[Link](e);
}
try
{
os=new PrintStream([Link]());
is=new DataInputStream([Link]);
is1=new DataInputStream([Link]());
do
{
[Link]("Client:");
line=[Link]();
[Link](line);
[Link]("Server:" + [Link]());
}while([Link]("quit")==false);
[Link]();
[Link]();
}
catch(IOException e)
{
[Link]("Socket Closed!Message Passing is over");
}
}
OUT PUT :

Server
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]

Note: [Link] uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

C:\Program Files\Java\jdk1.5.0\bin>java TCPserver1

Client: Hai Server

Server:

Hai Client

Client: How are you

Server:

Fine

Client: quit

Server:

quit

Client
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]

Note: [Link] uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

C:\Program Files\Java\jdk1.5.0\bin>java TCPclient1

Client:

Hai Server

Server: Hai Client

Client:

How are you

Server: Fine

Client:

quit

Server: quit

2) IMPLEMENTATION OF TCP/IP ECHO


Program Objective: To implementation of echo client server using TCP/IP

ALGORITHM:

1. Start the program

2. Include necessary package in java

3. To create a socket in client to server.

4. The client establishes a connection to the server.


5. The client accept the connection and send data to server and the server to replay the echo message to the
client

6. The client communicate the server to send the end of the message

7. Stop the program.

Program :

[Link]

import [Link].*;
import [Link].*;
public class EServer
{
public static void main(String args[])
{
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{
[Link](e);
}
try
{
c=[Link]();
is=new DataInputStream([Link]());
ps=new PrintStream([Link]());
while(true)
{
line=[Link]();
[Link](line);
}
}
catch(IOException e)
{
[Link](e);
}
}
}

[Link]
import [Link].*;
import [Link].*;
public class EClient
{
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("[Link]",9000);
}
catch(IOException e)
{
[Link](e);
}
try
{
os=new PrintStream([Link]());
is=new DataInputStream([Link]);
is1=new DataInputStream([Link]());
while(true)
{
[Link]("Client:");
line=[Link]();
[Link](line);
[Link]("Server:" + [Link]());
}

}
catch(IOException e)
{
[Link]("Socket Closed!");
}
}
}

Output
Server
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EServer
C:\Program Files\Java\jdk1.5.0\bin>
Client
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EClient
Client:
Hai Server
Server:Hai Server
Client:
Hello
Server:Hello
Client:
end
Server:end
Client:
ds
Socket Closed!

EXPERIMENT- 8

Objective: Program using simple udp

ALGORITHM:
1. Create a new file. Enter the domain name and address in that file.
2. To establish the connection between client and server.
3. Compile and execute the program.
4. Enter the domain name as input.
5. The IP address corresponding to the domain name is display on the screen
6. Enter the IP address on the screen.
7. The domain name corresponding to the IP address is display on the screen.
8. Stop the program.
Program Code :

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc,char *argv[1])
{
struct hostent *hen;
if(argc!=2)
{
fprintf(stderr,"Enter the hostname \n");
exit(1);
}
hen=gethostbyname(argv[1]);
if(hen==NULL)
{
fprintf(stderr,"Host not found \n");
}
printf("Hostname is %s \n",hen->h_name);
printf("IP address is %s \n",inet_ntoa(*((struct in_addr *)hen->h_addr)));
}

Output
[cse5062@linuxserver ~]$cc dns.c –o c
[cse5062@linuxserver ~]$./c [Link]

Host name is [Link]

IP address is [Link]

PROGRAM USING UDP SOCKET


Program Objective: To write a client-server application for chat using UDP

ALGORITHM: CLIENT

1. Include necessary package in java

2. To create a socket in client to server.

3. The client establishes a connection to the server.

4. The client accept the connection and to send the data from client to server and vice versa

5. The client communicate the server to send the end of the message

6. Stop the program.

ALGORITHM: SERVER

1. Include necessary package in java

2. To create a socket in server to client

3. The server establishes a connection to the client.


4. The server accept the connection and to send the data from server to client and vice versa

5. The server communicate the client to send the end of the message

6. Stop the program.

Program :

[Link]
import [Link].*;
import [Link].*;
class UDPserver
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
ds=new DatagramSocket(clientport);
[Link]("press ctrl+c to quit the program");
BufferedReader dis=new BufferedReader(new InputStreamReader([Link]));
InetAddress ia=[Link]("localhost");
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,[Link]);
[Link](p);
String psx=new String([Link](),0,[Link]());
[Link]("Client:" + psx);
[Link]("Server:");
String str=[Link]();
if([Link]("end"))
break;
buffer=[Link]();
[Link](new DatagramPacket(buffer,[Link](),ia,serverport));
}
}
}

[Link]
import java .io.*;
import [Link].*;
class UDPclient
{
public static DatagramSocket ds;
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
byte buffer[]=new byte[1024];
ds=new DatagramSocket(serverport);
BufferedReader dis=new BufferedReader(new InputStreamReader([Link]));
[Link]("server waiting");
InetAddress ia=[Link]("[Link]");
while(true)
{
[Link]("Client:");
String str=[Link]();
if([Link]("end"))
break;
buffer=[Link]();
[Link](new DatagramPacket(buffer,[Link](),ia,clientport));
DatagramPacket p=new DatagramPacket(buffer,[Link]);
[Link](p);
String psx=new String([Link](),0,[Link]());
[Link]("Server:" + psx);
}
}
}
Output:
Server
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
C:\Program Files\Java\jdk1.5.0\bin>java UDPserver
press ctrl+c to quit the program
Client:Hai Server
Server:
Hello Client
Client:How are You
Server:
I am Fine what about you

Client
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
C:\Program Files\Java\jdk1.5.0\bin>java UDPclient
server waiting
Client:
Hai Server
Server:Hello Clie
Client:
How are You
Server:I am Fine w
Client:
end

EXPERIMENT- 9
Program Objective : To implement programs using raw sockets (like packet capturing and filtering)

Program Prerequisites: System installed with C and JAVA compiler (512 MB RAM, 32GB HDD)

ALGORITHM :
1. Start the program and to include the necessary header files

2. To define the packet length

3. To declare the IP header structure using TCPheader.

4. Using simple checksum process to check the process

5. Using TCP \IP communication protocol to execute the program

6. And using TCP\IP communication to enter the Source IP and port number and Target IP address and port
number.

7. The Raw socket () is created and accept the Socket ( ) and Send to ( ), ACK

8. Stop the program

Program Code:

//---cat rawtcp.c---
// Run as root or SUID 0, just datagram no data/payload
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
// Packet length
#define PCKT_LEN 8192

// May create separate header file (.h) for all


// headers' structures
// IP header's structure
struct ipheader {
unsigned char iph_ihl:5, /* Little-endian */
iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned char iph_flags;
unsigned short int iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};

/* Structure of a TCP header */


struct tcpheader {
unsigned short int tcph_srcport;
unsigned short int tcph_destport;
unsigned int tcph_seqnum;
unsigned int tcph_acknum;
unsigned char tcph_reserved:4, tcph_offset:4;
// unsigned char tcph_flags;
unsigned int
tcp_res1:4, /*little-endian*/
tcph_hlen:4, /*length of tcp header in 32-bit words*/
tcph_fin:1, /*Finish flag "fin"*/
tcph_syn:1, /*Synchronize sequence numbers to start a connection*/
tcph_rst:1, /*Reset flag */
tcph_psh:1, /*Push, sends data to the application*/
tcph_ack:1, /*acknowledge*/
tcph_urg:1, /*urgent pointer*/
tcph_res2:2;
unsigned short int tcph_win;
unsigned short int tcph_chksum;
unsigned short int tcph_urgptr;
};
// Simple checksum function, may use others such as Cyclic Redundancy Check, CRC
unsigned short csum(unsigned short *buf, int len)
{
unsigned long sum;
for(sum=0; len>0; len--)
sum += *buf++;
sum = (sum >> 16) + (sum &0xffff);
sum += (sum >> 16);
return (unsigned short)(~sum);
}

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


{
int sd;
// No data, just datagram
char buffer[PCKT_LEN];
// The size of the headers
struct ipheader *ip = (struct ipheader *) buffer;
struct tcpheader *tcp = (struct tcpheader *) (buffer + sizeof(struct ipheader));
struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;

memset(buffer, 0, PCKT_LEN);

if(argc != 5)
{
printf("- Invalid parameters!!!\n");
printf("- Usage: %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
exit(-1);
}

sd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);


if(sd < 0)
{
perror("socket() error");
exit(-1);
}
else
printf("socket()-SOCK_RAW and tcp protocol is OK.\n");
// The source is redundant, may be used later if needed
// Address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
// Source port, can be any, modify as needed
sin.sin_port = htons(atoi(argv[2]));
din.sin_port = htons(atoi(argv[4]));
// Source IP, can be any, modify as needed
sin.sin_addr.s_addr = inet_addr(argv[1]);
din.sin_addr.s_addr = inet_addr(argv[3]);
// IP structure
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16;
ip->iph_len = sizeof(struct ipheader) + sizeof(struct tcpheader);
ip->iph_ident = htons(54321);
ip->iph_offset = 0;
ip->iph_ttl = 64;
ip->iph_protocol = 6; // TCP
ip->iph_chksum = 0; // Done by kernel

// Source IP, modify as needed, spoofed, we accept through command line argument
ip->iph_sourceip = inet_addr(argv[1]);
// Destination IP, modify as needed, but here we accept through command line argument
ip->iph_destip = inet_addr(argv[3]);
// The TCP structure. The source port, spoofed, we accept through the command line
tcp->tcph_srcport = htons(atoi(argv[2]));
// The destination port, we accept through command line
tcp->tcph_destport = htons(atoi(argv[4]));
tcp->tcph_seqnum = htonl(1);
tcp->tcph_acknum = 0;
tcp->tcph_offset = 5;
tcp->tcph_syn = 1;
tcp->tcph_ack = 0;
tcp->tcph_win = htons(32767);
tcp->tcph_chksum = 0; // Done by kernel
tcp->tcph_urgptr = 0;
// IP checksum calculation
ip->iph_chksum = csum((unsigned short *) buffer, (sizeof(struct ipheader) + sizeof(struct tcpheader)));

// Inform the kernel do not fill up the headers' structure, we fabricated our own
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("setsockopt() error");
exit(-1);
}
else
printf("setsockopt() is OK\n");

printf("Using:::::Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3],
atoi(argv[4]));
// sendto() loop, send every 2 second for 50 counts
unsigned int count;
for(count = 0; count < 20; count++)
{
if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
// Verify
{
perror("sendto() error");
exit(-1);
}
else
printf("Count #%u - sendto() is OK\n", count);
sleep(2);
}
close(sd);
return 0;
}
OUT PUT :
[exam03@localhost 03]# gcc rawtcp.c -o rawtcp
[exam03@localhost 03]# ./rawtcp
- Invalid parameters!!!
- Usage: ./rawtcp <source hostname/IP> <source port> <target hostname/IP> <target port>
[exam03@localhost 03]# ./rawtcp [Link] 23 [Link] 8008
socket()-SOCK_RAW and tcp protocol is OK.
setsockopt() is OK
Using:::::Source IP: [Link] port: 23, Target IP: [Link] port: 8008.
Count #0 - sendto() is OK
Count #1 - sendto() is OK
Count #2 - sendto() is OK
Count #3 - sendto() is OK
Count #4 - sendto() is OK
EXPERIMENT- 10
Objective: Write a program using RPC/RMI.
Description
Remote Procedure Call (RPC) system enables you to call a function available on a remote server using the same
syntax which is used when calling a function in a local library. This is useful in two situations.

 You can utilize the processing power from multiple machines using rpc without changing the code for
making the call to the programs located in the remote systems.
 The data needed for the processing is available only in the remote system.

So in python we can treat one machine as a server and another machine as a client which will make a call to the
server to run the remote procedure. In our example we will take the localhost and use it as both a server and
client.
Running a Server
The python language comes with an in-built server which we can run as a local server. The script to run this
server is located under the bin folder of python installation and named as [Link]. We can run it in the python
prompt and check its running as a local server.

python bin/[Link]
When we run the above program, we get the following output −
INFO:SLAVE/18812:server started on [[Link]]:18812

Running a Client
Next we run the client using the rpyc module to execute a remote procedure call. In the below example we
execute the print function in the remote server.
import rpyc
conn = [Link]("localhost")
[Link]("print('Hello from Tutorialspoint')")
When we run the above program, we get the following output −
Hello from Tutorialspoint
Expression Evaluation through RPC
Using the above code examples we can use python’s in-built functions for execution and evaluation of
expressions through rpc.
import rpyc
conn = [Link]("localhost")
[Link]('import math')
[Link]('2*[Link]')
When we run the above program, we get the following output −
6.283185307179586
Method 2:
ALGORITHM:

1. Start the program and to include necessary packages

2. Using Add client to get the two values

3. Using Add server() to implement and Call the Add server impl

4. Using public interface to call the program in remotely

5. Finally to call and compile all the sub program

6. To Execute Start RMI registry

7. Stop the program

Program Code:

ADD CLIENT:
import [Link].*;
public class AddClient
{
public static void main(String args[])
{
try
{
String addServerURL="rmi://"+args[0]+"/AddServer";
AddServerIntf addServerIntf=(AddServerIntf)[Link](addServerURL);
[Link]("the first number is"+args[1]);
double d1=[Link](args[1]).doubleValue( );
[Link]("the Second number is"+args[2]);
double d2=[Link](args[2]).doubleValue();
[Link]("the sum is "+[Link](d1,d2));
}
catch(Exception e)
{
[Link]("Exception:"+e);
}
}
}
ADD SERVER
import [Link].*;
import [Link].*;
public class AddServer
{
public static void main(String args[])
{
try
{
AddServerImpl addServerImpl=new AddServerImpl();
[Link]("AddServer",addServerImpl);
}
catch(Exception e)
{
[Link]("Exception :"+e);
}
}
}

ADD SERVERIMPL:
import [Link].*;
import [Link].*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf
{
public AddServerImpl()throws RemoteException
{
}
public double add(double d1,double d2)throws RemoteException
{
return d1+d2;
}
}

ADDSERVERINTF:

import [Link].*;
public interface AddServerIntf extends Remote
{
double add(double d1,double d2)throws RemoteException;
}
OUTPUT:

You might also like