Documentos de Académico
Documentos de Profesional
Documentos de Cultura
SEMESTER – V
NAME : Naren V
REG NO : RA1811003040173
1
BONAFIDE CERTIFICATE
Vadapalani.
2
Internal Examiner- I Internal
Examiner- II
LIST OF EXPERIMENTS
Exp.No: 01
Date: 07.08.2020
1. stdio.h:
Has standard input and output library providing simple and efficient
buffered stream IO interface.
2. unistd.h:
It is a POSIX standard for open system interface. [Portable
Operating System Interface]
3. string.h:
This header file is used to perform string manipulation operations on
NULL terminated strings.(Bzero -0 the m/y)
4. stdlib.h:
This header file contains the utility functions such as string
conversion routines, memory allocation routines, random number
generator, etc.
5. sys/types.h:
Defines the data type of socket address structure in unsigned long.
6. sys/socket.h:
The socket functions can be defined as taking pointers to the generic
socket address structure called sockaddr.
7. netinet/in.h:
Defines the IPv4 socket address structure commonly called Internet
socket address structure called sockaddr_in.
8. netdb.h:
5
Defines the structure hostent for using the system call gethostbyname
to get the network host entry.
9. time.h:
Has structures and functions to get the system date and time and to
perform time manipulation functions. We use the function ctime(),
that is defined in this header file , to calculate the current date and
time.
10. sys/stat.h:
Contains the structure stat to test a descriptor to see if it is of a
specified type. Also it is used to display file or file system status.stat()
updates any time related fields.when copying from 1 file to another.
11. sys/ioctl.h:
Macros and defines used in specifying an ioctl request are located in
this header file. We use the function ioctl() that is defined in this
header file. ioctl() function is used to perform ARP cache operations.
12. pcap.h:
Has function definitions that are required for packet capturing. Some
of the functions are pcap_lookupdev(),pcap_open_live() and
pcap_loop(). pcap_lookupdev() is used to initialize the network
device.The device to be sniffed is opened using the pcap_open_live().
Pcap_loop() determines the number of packets to be sniffed.
13. net/if_arp.h:
Contains the definitions for Address Resolution Protocol. We use
this to manipulate the ARP request structure and its data members
arp_pa,arp_dev and arp_ha. The arp_ha structure’s data member
sa_data[ ] has the hardware address.
14. errno.h:
It sets an error number when an error and that error can be
displayed using perror function. It has symbolic error names. The
error number is never set to zero by any library function.
6
15. arpa/inet.h:
This is used to convert internet addresses between ASCII
strings and network byte ordered binary values (values that are
stored in socket address structures). It is used for inet_aton,
inet_addr, inet_ntoa functions.
RESULT: The basic header files used for Socket Programming was
studied successfully.
Exp.No: 02
Date: 14.08.2020
1.man socket
NAME:
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
int socket(int domain,int type,int protocol);
DESCRIPTION:
FORMAT:
NAME PURPOSE
PF_UNIX,PF_LOCA Local Communication.
L
PF_INET IPV4 Internet Protocols.
PF_IPX IPX-Novell Protocols.
PF_APPLETALK Apple Talk.
8
The socket has the indicated type, which specifies the
communication semantics.
TYPES:
1.SOCK_STREAM:
2.man connect:
NAME:
connect – initiate a connection on a socket.
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
9
int connect(int sockfd,const (struct
sockaddr*)serv_addr,socklen_t addrlen);
DESCRIPTION:
RETURN VALUE:
ERRORS:
3.man accept
NAME:
10
accept destination(s)
reject[-t] [-h server] [-r reason] destination(s)
DESCRIPTION:
4.man send
NAME:
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
DESCRIPTION:
5.man recv
NAME:
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr
*from, socklen_t* from len);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
DESCRIPTION:
NAME:
7.man write
NAME:
12
write- send a message to another user.
SYNOPSIS:
write user[ttyname]
DESCRIPTION:
8. ifconfig
NAME:
SYNOPSIS:
ifconfig[interface]
ifconfig interface[aftype] options | address……
DESCRIPTION:
13
If no arguments are given, ifconfig displays the status of the
currently active interfaces.
9. man bind
SYNOPSIS:
NAME:
SYNOPSIS:
#include<netinet/in.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint32_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
DESCRIPTION:
NAME:
14
SYNOPSIS:
#include<unistd.h>
int gethostname(char *name,size_t len);
int sethostname(const char *name,size_t len);
DESCRIPTION:
NAME:
SYNOPSIS:
#include<netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#include<sys/socket.h>
struct hostent *gethostbyaddr(const char *addr)int len, int
type);
struct hostent *gethostbyname2(const char *name,int af);
DESCRIPTION:
15
The gethostbyname() returns a structure of type hostent for
the given hostname.
Name->hostname or IPV4/IPV6 with dot notation.
gethostbyaddr()- struct of type hostent / host address length
Address types- AF_INET, AF_INET6.
sethostent() – stay open is true(1).
TCP socket connection should be open during queries.
Server queries for UDP datagrams.
endhostent()- ends the use of TCP connection.
Members of hostent structure:
a. h_name
b. h_aliases
c. h_addrtype
d. h_length
e. h_addr-list
f. h_addr.
RESULT:
The basic functions used for Socket Programming was studied
successfully.
16
Exp.No: 03
Date: 21.08.2020
AIM:
To create a simple TCP/IP connection between server and client
using java code.
import java.io.*;
import java.net.*;
public class chatserver
{
{
ServerSocket sersock=new ServerSocket(3000);
System.out.println("Server ready for chatting"); Socket
sock=sersock.accept();
BufferedReader KeyRead= new BufferedReader(new
InputStreamReader(System.in));
OutputStream ofstream=sock.getOutputStream(); PrintWriter
pwrite=new PrintWriter(ofstream,true); InputStream
istream=sock.getInputStream();
BufferedReader receivedread=new BufferedReader(new
InputStreamReader(istream)); String receivemessage,sendmessage;
while(true)
{
if((receivemessage= receivedread.readLine())!=null)
17
System.out.println(receivemessage);
} sendmessage=KeyRead.readLine();
pwrite.println(sendmessage);
System.out.flush();
}
import java.io.*;
import java.net.*;
public class chatclient
{
{
Socket sock=new Socket("localhost",3000);
while(true)
{
18
sendmessage=KeyRead.readLine();
pwrite.println(sendmessage);
System.out.flush();
if((receivemessage=receivedread.readLine())!=null)
{ System.out.println(receivemessage);
}
OUTPUT:
RESULT:
19
Thus a simple TCP/IP connection between server and client was
created using java code.
Exp.No: 04
Date: 04.09.2020
import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String arg[])throws Exception
{
byte[] receiveData= new byte[1024];
byte[] sendData= new byte[1024];
DatagramSocket serversocket=new DatagramSocket(8000);
DatagramPacket receivePacket=new
DatagramPacket(receiveData,receiveData.length);
serversocket.receive(receivePacket);
BufferedReader b=new BufferedReader( new
InputStreamReader(System.in));
String s=b.readLine();
/*String sentence=new String(receivePacket.getData());
System.out.println("Received messege is: "+sentence.trim());
sentence=sentence.toUpperCase();
System.out.println("Converted messege is:
"+sentence.trim());*/
InetAddress ip=receivePacket.getAddress();
int port=receivePacket.getPort();
20
//sendData=sentence.getBytes();
sendData=s.getBytes();
DatagramPacket sendPacket=new
DatagramPacket(sendData,sendData.length,ip,port);
serversocket.send(sendPacket);
}
}
import java.io.*;
import java.net.*;
import java.util.*;
class UDPClient
{
public static void main(String arg[])throws Exception
{
byte[] receiveData=new byte[1024];
byte[] sendData=new byte[1024];
DatagramSocket clientSocket=new DatagramSocket();
DatagramPacket receivePacket=new
DatagramPacket(receiveData,receiveData.length);
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
String sentence=in.readLine();
sendData=sentence.getBytes();
InetAddress ip=InetAddress.getByName("localhost");
DatagramPacket sendPacket=new
DatagramPacket(sendData,sendData.length,ip,8000);
clientSocket.send(sendPacket);
clientSocket.receive(receivePacket);
sentence=new String(receivePacket.getData());
System.out.println("New Sentence is: "+sentence.trim());
}
}
21
OUTPUT:
RESULT:
Thus a UDP Echo connection between server and client was
created using java code.
22
Exp.No: 05
Date: 18.09.2020
23
System.out.println("client system IPaddress is:"+inet);
ps.close();
dis.close();
}}
catch(IOException e)
{
System.out.println("the ecxeption is"+e);
}}}
24
System.out.println("the exception is"+e);}}}
OUTPUT:
RESULT:
Thus the date and time was displayed by creating a concurrent
TCP/IP connection between server and client using java code.
25
Exp.No: 06
Date: 25.09.2020
Server
import java.io.*;
import java.net.*;
import java.lang.*;
class server1
{
public static void main(String a[])throws IOException
{
ServerSocket ss=new ServerSocket(8000);
Socket s=ss.accept();
PrintStream dos=new PrintStream(s.getOutputStream());
DataInputStream in=new DataInputStream(System.in);
DataInputStream inn=new DataInputStream(s.getInputStream());
while(true)
{
System.out.print("Enter the msg to send: ");
String str=in.readLine();
dos.println(str);
26
if(str.equals("end"))
{
ss.close();
break;
}
String str1=inn.readLine();
System.out.println("Messege received: "+str1);
if(str1.equals("end"))
{
ss.close();
break;
}
}
}
}
Client
import java.io.*;
import java.net.*;
import java.lang.*;
class client1
{
public static void main(String a[])throws IOException
{
Socket s=new Socket("LocalHost",8000);
27
DataInputStream in=new DataInputStream(s.getInputStream());
DataInputStream inn=new DataInputStream(System.in);
PrintStream dos=new PrintStream(s.getOutputStream());
while(true)
{
String str=in.readLine();
System.out.print("Messege received:"+str);
if(str.equals("end"))
{
s.close();
break;
}
System.out.print("\nEnter the msg to send: ");
String str1=inn.readLine();
dos.println(str1);
if(str1.equals("end"))
{
s.close();
break;
}
}
}
}
28
OUTPUT:
RESULT:
Thus a half duplex communication (transmit data in just one
direction at a time) was established using java code
29
Exp.No: 07
Date: 09.10.2020
UserInput.java
import java.io.IOException;
public interface UserInput {
void write(String input) throws IOException;
}
Keyboard.java
import java.io.*;
public class Keyboard {
static BufferedReader buffer;
static {
InputStreamReader bis = new InputStreamReader(System.in);
buffer = new BufferedReader(bis);
}
public static String readline() throws IOException {
return buffer.readLine();
30
}
}
Client.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class Client extends Thread implements UserInput {
public void connect(int port) throws IOException {
InetSocketAddress address = new
InetSocketAddress("localhost", 9999);
channel = SocketChannel.open(address);
channel.configureBlocking(false);
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
}
@Override
public void run() {
Iterator<SelectionKey> it;
31
while (channel.isConnected()) {
try {
if (selector.select() != 0) {
it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
handleKey(key);
it.remove();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleKey(SelectionKey key) throws IOException {
if (key.isReadable()) {
ByteBuffer buffer = ByteBuffer.allocate(80);
buffer.clear();
channel.read(buffer);
if (buffer.get(0) == 0) {
return;
32
}
System.out.printf("Server says: %s", new
String(buffer.array()));
}
}
@Override
public void write(String input) throws IOException {
channel.write(ByteBuffer.wrap(input.getBytes()));
}
SocketChannel channel;
Selector selector;
}
Server.java
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
33
import java.util.Iterator;
import java.util.List;
public class Server extends Thread implements UserInput {
public void listen(int port) throws IOException {
channel = ServerSocketChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(9999));
selector = Selector.open();
channel.register(selector, SelectionKey.OP_ACCEPT);
}
@Override
public void run() {
clients = new ArrayList<SocketChannel>();
Iterator<SelectionKey> it;
while (channel.isOpen()) {
try {
if (selector.select() != 0) {
it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
handleKey(key);
it.remove();
34
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleKey(SelectionKey key) throws IOException {
// System.out.println("Server.handleKey");
if (key.isAcceptable()) {
SocketChannel channelClient = channel.accept();
channelClient.configureBlocking(false);
channelClient.register(selector, SelectionKey.OP_READ |
SelectionKey.OP_WRITE);
System.out.println("Client is accepted");
clients.add(channelClient);
} else if (key.isReadable()) {
SocketChannel channelClient = (SocketChannel) key.channel();
if (!channelClient.isOpen()) {
System.out.println("Channel terminated by client");
}
ByteBuffer buffer = ByteBuffer.allocate(80);
35
buffer.clear();
channelClient.read(buffer);
if (buffer.get(0) == 0) {
System.out.println("Nothing to read.");
channelClient.close();
clients.remove(channelClient);
return;
}
System.out.printf("Client says: %s", new
String(buffer.array()));
} else if (key.isWritable()) {
}
}
List<SocketChannel> clients;
ServerSocketChannel channel;
Selector selector;
@Override
public void write(String input) throws IOException {
for(SocketChannel channelClient:clients) {
channelClient.write(ByteBuffer.wrap(input.getBytes()));
}
}
}
36
App.java
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class App {
public static void main(String[] args) throws InterruptedException,
IOException {
if (args.length == 0) return;
Thread worker;
final UserInput input;
if ("host".equals(args[0])) {
System.out.println("Starting server app...");
Server server = new Server();
server.listen(9999);
worker = server;
input = server;
} else if ("client".equals(args[0])) {
System.out.println("Starting client app...");
Client client = new Client();
client.connect(9999);
worker = client;
input = client;
37
} else {
System.exit(1);
return;
}
worker.start();
service.execute(new Runnable() {
@Override
public void run() {
System.out.println("Consuming user input..");
String line;
while (true) {
try {
line = Keyboard.readline();
input.write(line);
//System.out.println("Echo: " + line);
} catch (IOException e) {
38
worker.join();
}
static ExecutorService service =
Executors.newSingleThreadExecutor();}
OUTPUT:
RESULT:
Thus a full duplex communication (transmit data in just two
directions at a time) was established using java code.
39
Exp.No: 08
Date: 16.10.2020
AIM:
To transfer files from one host to another by implementing FTP.
Server:
import java.io.*;
import java.net.*;
class FTP_Server
{
public static void main(String[] args) throws Exception
{
ServerSocket s=new ServerSocket(6777);
Socket sr=s.accept();
FileInputStream fr=new FileInputStream("D:\\ftp_example.txt");
byte b[]=new byte[2002];
fr.read(b, 0, b.length);
OutputStream os=sr.getOutputStream();
os.write(b, 0, b.length);
}
}
40
Client
import java.io.*;
import java.net.*;
class FTP_Client
{
public static void main(String[] args) throws Exception
{
byte []b=new byte[2002];
Socket sr=new Socket("localhost", 6777);
InputStream is=sr.getInputStream();
FileOutputStream fr=new FileOutputStream("E:\\ftp_example.txt");
is.read(b, 0, b.length);
fr.write(b, 0, b.length);
}
}
OUTPUT:
41
RESULT:
Thus files were transfered from one host to another by
implementing FTP.
42
Exp.No: 09
Date: 30.10.2020
AIM:
To execute remote commands using UDP.
Server:
import java.io.*;
import java.net.*;
class RemoteServer11
{
public static void main(String args[])
{
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
try
{
while(true)
{
DatagramSocket serverSocket = new DatagramSocket(9876);
serverSocket.receive(receivePacket);
Client:
import java.io.*;
import java.net.*;
class RemoteClient11
{
public static void main(String args[])throws IOException
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length,IPAddress, 9876);
44
clientSocket.send(sendPacket);
//clientSocket.receive(receivePacket);
clientSocket.close();
inFromUser.close();
}
}
OUTPUT:
RESULT:
Thus remote commands were executed using UDP.
45
Exp.No: 10
Date: 06.11.2020
AIM:
To implement ARP using FTP.
ARP table
After obtaining a host's MAC address, the switch adds the IP-to-MAC
mapping to its own ARP table. This mapping is used for forwarding
packets with the same destination in future.
import java.io.*;
import java.util.*;
bw.close();
fw.close();
}
47
while ((line = br.readLine()) != null)
{
if (line.contains(ip))
{
System.out.println("Internet Address Physical Address
Type");
System.out.println(line);
break;
}
}
if((line == null))
System.out.println("Not found");
fr.close();
br.close();
}
public static void findIP(String mac) throws Exception
{
File fp = new File("ARPTable.txt");
FileReader fr = new FileReader(fp);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null)
{
if (line.contains(mac))
{
System.out.println("Internet Address Physical Address
Type");
System.out.println(line);
break;
}
}
48
if((line == null))
System.out.println("Not found");
fr.close();
br.close();
}
System.out.println("ARP Protocol.");
System.out.print("Enter IP Address: ");
String IP = S.nextLine();
findMAC(IP);
System.out.println("RARP Protocol.");
System.out.print("Enter MAC Address: ");
String MAC = S.nextLine();
findIP(MAC);
}
}
49
OUTPUT:
Type ipconfig in cmd to check the ip address of your system
RESULT:
50
Thus ARP was implemented using FTP.
Exp.No: 11
Date: 13.11.2020
Implementation of NAT
AIM:
To implement NAT using cisco packet tracer.
PROCEDURE:
OUTPUT:
NAT Overload or PAT:
51
52
STATIC NAT
Exp.No: 12
Date: 20.11.2020
Communication using HDLC
AIM:
To establish communication using HDLC in cisco packet tracer.
PROCEDURE:
53
Give connections and name those PCs, Routers and Gateways.
Set IP configuration for PCs.
Configure Router using CLI in R0 and R1.
Check Ping using Command terminal.
Using sh int s2/0 check for hdlc enabling.
If not enabled , enable hdlc.
OUTPUT:
54
RESULT:
55
Thus communication was established using HDLC in cisco
packet tracer.
Exp.No: 13
Date: 27.11.2020
Communication using PPP
AIM:
To establish communication using PPP in cisco packet tracer.
PROCEDURE:
OUTPUT:
56
57
RESULT: Thus communication was established using PPP in cisco
packet tracer.
58