0% found this document useful (0 votes)
151 views

Full Lab Program CN2010

This document contains source code for a client-server program that allows for basic chatting over TCP sockets in C. The code defines functions for a client and server that can exchange messages by reading from and writing to the connected socket. The client prompts the user for input, sends it to the server, and displays any responses. The server continuously reads incoming messages and writes them back to the client before waiting for more. The programs establish a connection on port 43464 and terminate when either side inputs "exit".

Uploaded by

nivedha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Full Lab Program CN2010

This document contains source code for a client-server program that allows for basic chatting over TCP sockets in C. The code defines functions for a client and server that can exchange messages by reading from and writing to the connected socket. The client prompts the user for input, sends it to the server, and displays any responses. The server continuously reads incoming messages and writes them back to the client before waiting for more. The programs establish a connection on port 43464 and terminate when either side inputs "exit".

Uploaded by

nivedha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

int len;

Program 1: Program to implement Date and char buff[100];


Time using TCP Sockets sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
#include<netinet/in.h> {
#include<sys/socket.h> printf(“Error in Socket”);
main( ) exit(0);
{ }
struct sockaddr_in sa; else
struct sockaddr_in cli; printf(“Socket is Opened”);
int sockfd,coontfd; bzero(&sa,sizeof(sa));
int len,ch; sa.sin_family=AF_INET;
char str[100]; sa.sin_port=htons(5600);
time_t tick; if(connect(sockfd,(struct
sockfd=socket(AF_INET,SOCK_STREAM,0); sockaddr*)&sa,sizeof(sa))<0)
if(socket<0) {
{ printf(“Error in connection failed”);
printf(“error in socket\n”); exit(0);
exit(0); }
} else
else printf(“connected successfully”):
printf(“Socket Opened”); if(n=read(sockfd,buff,sizeof(buff))<0)
bzero(&sa,sizeof(sa)); {
sa.sin_port=htons(5600); printf(“Error in Reading”);
sa.sin_addr.s_addr=htonl(0); exit(0);
if(bind(sockfd,(struct }
sockaddr*)&sa,sizeof(sa))<0) else
{ {
printf(“Error in binding\n”); printf(“Message Read %s”,buff);
} buff[n]=’\0’;
else printf(“%s”,buff);
printf(“Binded Successfully”); }
listen(sockfd,50) }
for(;;)
{ Output
len=sizeof(ch); SERVER SIDE OUTPUT
conntfd=accept(sockfd,(struct
sockaddr*)&cli,&len); Socket Opened
printf(“Accepted”); Binded successfully
tick=ctime(NULL); Accepted
snprintf(str,sizeof(str),”%s”,ctime(&tick));
write(conntfd,str,100); CLIENT SIDE OUTPUT WITH SERVER TIME
} Socket is Opened
} Connected successfully
Message Read Mon Jan 22 15:09:19 2007
CLIENT PROGRAM Source code in C
#include<netinet/in.h>
#include<sys/socket.h>
main()
{
struct sockaddr_in sa,cli;
int n,sockfd;
if(connect(sockfd,(SA
Ex.No:2 Program to implement Echo Server *)&servaddr,sizeof(servaddr))!=0)
and Client using TCP Sockets {
Client Side printf("connection with the server
#include<stdio.h> failed..\n");
#include<netinet/in.h> exit(0);
#include<sys/socket.h> }
#include<netdb.h> else
#include<string.h> printf("connected to the server..\n");
#define MAX 80 func(sockfd);
#define PORT 43464 close(sockfd);
#define SA struct sockaddr }
Server Side
void func(int sockfd) #include<stdio.h>
{ #include<netinet/in.h>
char buff[MAX]; #include<sys/types.h>
int n; #include<sys/socket.h>
for(;;) #include<netdb.h>
{ #include<string.h>
bzero(buff,sizeof(buff)); #define MAX 80
printf("Enter the string"); #define PORT 43464
n=0; #define SA struct sockaddr
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff)); void func(int sockfd)
bzero(buff,sizeof(buff)); {
read(sockfd,buff,sizeof(buff)); char buff[MAX];
printf("From server:%s",buff); int n;
if((strncmp("exit",buff,4))==0) for(;;)
{ {
printf("Client Exit..\n"); bzero(buff,MAX);
break; read(sockfd,buff,sizeof(buff));
} printf("From client:%s\t",buff);
} write(sockfd,buff,sizeof(buff));
} bzero(buff,sizeof(buff));

int main()
{ if((strncmp(buff,"exit",4))==0)
int sockfd,connfd; {
struct sockaddr_in servaddr,cli; printf("server exit...\n");
sockfd=socket(AF_INET,SOCK_STREAM,0); }
if(sockfd==-1) }
{ }
printf("sockect creation failed"); int main()
exit(0); {
} int len,sockfd,connfd;
else struct sockaddr_in servaddr,cli;
printf("Sockect sucessfully created.\n"); sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr)); if(sockfd==-1)
servaddr.sin_family=AF_INET; {
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); printf("Sockect creation failed..\n");
servaddr.sin_port=htons(PORT); exit(0);
}
else
printf("Sockect successfully created.\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT); SERVER OUTPUT:
if((bind(sockfd,(SA
*)&servaddr,sizeof(servaddr)))!=0) [cseastaff@localhost cn]$ cc tcp_echo_server.c
{ [cseastaff@localhost cn]$ ./a.out
printf("socket bind failed.\n"); Sockect successfully created.
exit(0); Socket sucessfully binded.
} Server listening.
else server accept client.
printf("Socket sucessfully binded.\n"); From client:hai
if((listen(sockfd,5))!=0) From client:welcome
{ From client:exit
printf("Listen failed.\n"); Broken pipe
exit(0);
} CLIENT OUTPUT:
else
printf("Server listening.\n"); [cseastaff@localhost cn]$ cc tcp_echo_client.c
len=sizeof(cli); [cseastaff@localhost cn]$ ./a.out
connfd=accept(sockfd,(SA *)&cli,&len); Sockect sucessfully created.
if(connfd<0) connected to the server..
{ Enter the stringhai
printf("Server accept failed.\n"); From server:hai
exit(0); Enter the stringwelcome
} From server:welcome
else Enter the stringexit
printf("server accept client.\n"); From server:exit
Client Exit..
func(connfd);
close(sockfd);
}
*)&servaddr,sizeof(servaddr))!=0)
Ex.No:3 Program to implement Chat Server {
and Client using TCP Sockets printf("connection with the server
#include<stdio.h> failed.\n");
#include<stdlib.h> exit(0);
#include<netinet/in.h> }
#include<sys/socket.h> else
#include<netdb.h> printf("connected to the server.\n");
#include<string.h> func(sockfd);
#define MAX 80 close(sockfd);
#define PORT 43464 }
#define SA struct sockaddr
OUTPUT:
void func(int sockfd) [cseastaff@localhost cn]$ cc tcp_chat_client.c
{ [cseastaff@localhost cn]$ ./a.out
char buff[MAX]; Sockect sucessfully created.
int n; connected to the server.
for(;;) Enter the stringhai
{ From server:hai
bzero(buff,sizeof(buff)); Enter the stringhello
printf("Enter the string"); From server:exit
n=0; Client Exit.
while((buff[n++]=getchar())!='\n'); [cseastaff@localhost cn]$
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff)); TCP_CHAT_SERVER
printf("From server:%s",buff);
if((strncmp("exit",buff,4))==0) #include<stdio.h>
{ #include<stdlib.h>
printf("Client Exit.\n"); #include<netinet/in.h>
break; #include<sys/socket.h>
} #include<netdb.h>
} #include<string.h>
} #define MAX 80
#define PORT 43464
int main() #define SA struct sockaddr
{
int sockfd,connfd; void func(int sockfd)
struct sockaddr_in servaddr,cli; {
sockfd=socket(AF_INET,SOCK_STREAM,0); char buff[MAX];
if(sockfd==-1) int n;
{ for(;;)
printf("sockect creation failed"); {
exit(0); bzero(buff,MAX);
} read(sockfd,buff,sizeof(buff));
else printf("From client:%s\t To client:",buff);
printf("Sockect sucessfully created.\n"); bzero(buff,MAX);
bzero(&servaddr,sizeof(servaddr)); n=0;
servaddr.sin_family=AF_INET; while((buff[n++]=getchar())!='\n');
servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”); write(sockfd,buff,sizeof(buff));
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA
if(strncmp("exit",buff,4)==0)
{
printf("Server Exit..\n"); OUTPUT:
break; [cseastaff@localhost cn]$ cc tcp_chat_server.c
} [cseastaff@localhost cn]$ ./a.out
} Sockect successfully created.
} Socket sucessfully binded.
Server listening.
int main() server accept client.
{ From client:hai
int sockfd, connfd,len; To client:hai
struct sockaddr_in servaddr,cli; From client:hello
sockfd=socket(AF_INET,SOCK_STREAM,0); To client:exit
Server Exit..
if(sockfd==-1)
{
printf("Sockect creation failed..\n");
exit(0);
}
else
printf("Sockect successfully created.\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA
*)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed.\n");
exit(0);
}
else
printf("Socket sucessfully binded.\n");
if((listen(sockfd,5))!=0)
{
printf("Listen failed.\n");
exit(0);
}
else
printf("Server listening.\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA *)&cli,&len);
if(connfd<0)
{
printf("Server accept failed.\n");
exit(0);
}
else
printf("server accept client.\n");
func(connfd);
close(sockfd);
}
exit(0);
Ex.No: 4 Program to implement DNS to }
resolve Host Name using TCP Sockets
ClientSide:
#include<sys/socket.h> Server Side:
#include<netinet/in.h>
#include<stdio.h> #include<sys/socket.h>
#include<sys/types.h>
#include<string.h> #include<netinet/in.h>
#include<netdb.h>
int main(int argc,char *argv[]) #include<stdio.h>
{
int sockfd,portno,n,len; #include<sys/types.h>
struct sockaddr_in servaddr,cli;
struct hostent *server; #include<string.h>
char buffer[256];
if(argc<3)
{
fprintf(stderr,"type host name followed by port"); int main(int argc,char *argv[])
exit(0); {
} int sockfd,portno,clilen,n,flag=0;
portno=atoi(argv[2]); struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_DGRAM,0); char buffer[256],b2[256],b3[256];
if(sockfd<0) FILE *f;
printerror("error in sockect creation"); char ch='y';
server=gethostbyname(argv[1]); if(argc<2)
bzero((char*)&servaddr,sizeof(servaddr)); {
servaddr.sin_family=AF_INET; fprintf(stderr,"No port provided");
bcopy((char*)server->h_addr, exit(0);
(char*)&servaddr.sin_addr.s_addr,server- }
>h_length); sockfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_port=htons(portno); if(sockfd<0)
printf("Enter the host name"); printerror("Error in socket");
bzero(buffer,256); else
fgets(buffer,255,stdin); printf("Sockect created");
len=sizeof(cli); bzero((char*)&servaddr,sizeof(servaddr));
n=sendto(sockfd,buffer,strlen(buffer),0,(struct portno=atoi(argv[1]);
sockaddr *)&servaddr,len); servaddr.sin_family=AF_INET;
if(n<0) servaddr.sin_port=htons(portno);
printerror("Error in writing"); if(bind(sockfd,(struct
bzero(buffer,256); sockaddr*)&servaddr,sizeof(servaddr))<0)
printf("\nIP address is:"); printerror("Error in binding");
n=recvfrom(sockfd,buffer,sizeof(buffer),0,(struct else
sockaddr*)&servaddr,&len); printf("Binded Sucessfully");
if(n<0) clilen=sizeof(cli);
printerror("Error in reading"); bzero(buffer,256);
printf("\n%s",buffer); n=recvfrom(sockfd,buffer,255,0,(struct
return 0; sockaddr*)&cli,&clilen);
} if(n<0)
int printerror(char *msg) printerror("Error in reading");
{ printf("host name is %s",buffer);
perror(msg);
f=fopen("dns.txt","r"); [Staff@linux cnlab2010]$ ./a.out 127.0.0.1 6060
if(f==NULL) Enter the host namewww.google.com
printf("File read error"); IP address is:
fscanf(f,"%s %s",b2,b3); 202.192.66.102
while(!feof(f))
{
strcat(b2,"\n");
if(strcmp(buffer,b2)==0)
{
strcpy(buffer,b3); Ex.No.5 : Implementation of PING program
flag=1; #include <unistd.h>
} #include <stdio.h>
if(flag) #include <sys/socket.h>
break; #include <netinet/ip.h>
fscanf(f,"%s%s",b2,b3); #include <netinet/udp.h>
} // The packet length
if(flag==0) #define PCKT_LEN 8192
strcpy(buffer,"No such host"); // Can create separate header file (.h) for all
printf("ip address is %s",buffer); headers' structure
n=sendto(sockfd,buffer,sizeof(buffer),0,(struct // The IP header's structure
sockaddr*)&cli,clilen); struct ipheader {
if(n<0) unsigned char iph_ihl:5, iph_ver:4;
printerror("Error in writing"); unsigned char iph_tos;
return 0; unsigned short int iph_len;
} unsigned short int iph_ident;
int printerror(char *msg) unsigned char iph_flag;
{ unsigned short int iph_offset;
perror(msg); unsigned char iph_ttl;
exit(0); unsigned char iph_protocol;
} unsigned short int iph_chksum;
unsigned int iph_sourceip;
INPUT File: dns.txt unsigned int iph_destip;
www.google.com 202.192.66.102 };
www.yahoo.com 192.190.55.30
// UDP header's structure
struct udpheader {
OUTPUT:Client unsigned short int udph_srcport;
[Satff@linux cnlab2010]$ cc udp_dns_client.c unsigned short int udph_destport;
unsigned short int udph_len;
[Satff@linux cnlab2010]$ ./a.out 127.0.0.1 6060 unsigned short int udph_chksum;
};
Enter the host namewww.google.com int main(int argc, char *argv[])
{
int sd;
IP address is: char buffer[PCKT_LEN];
// Our own headers' structures
202.192.66.102[kujani@linux cnlab2010]$ struct ipheader *ip = (struct ipheader *) buffer;
struct udpheader *udp = (struct udpheader *)
(buffer + sizeof(struct ipheader));
OUTPUT: Server // Source and destination addresses: IP and port
[Staff@linux cnlab2010]$ cc udp_dns_client.c struct sockaddr_in sin, din;
int one = 1; // Destination port number
const int *val = &one; udp->udph_destport = htons(atoi(argv[4]));
memset(buffer, 0, PCKT_LEN); udp->udph_len = htons(sizeof(struct udpheader));
if(argc != 5) // Calculate the checksum for integrity
{ ip->iph_chksum =rand();
printf("- Invalid parameters!!!\n"); // Inform the kernel do not fill up the packet
printf("- Usage %s <source hostname/IP> structure. we will build our own...
<source port> <target hostname/IP> <target /*
port>\n", argv[0]); if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL,
exit(-1); val, sizeof(one)) < 0)
} {
// Create a raw socket with UDP protocol perror("setsockopt() error");
sd = socket(PF_INET, SOCK_RAW, exit(-1);
IPPROTO_UDP); }
if(sd < 0) else
{ printf("setsockopt() is OK.\n");
perror("socket() error\n"); */
// If something wrong just exit // Send loop, send for every 2 second for 100
exit(-1); count
} printf("Trying...\n");
else printf("Using raw socket and UDP protocol\n");
printf("socket() - Using SOCK_RAW socket and printf("Using Source IP: %s port: %u, Target IP:
UDP protocol is OK.\n"); %s port: %u.\n", argv[1], atoi(argv[2]), argv[3],
// The address family atoi(argv[4]));
sin.sin_family = AF_INET; int count;
din.sin_family = AF_INET; if(sendto(sd, buffer, ip->iph_len, 0, (struct
// Port numbers sockaddr *)&sin, sizeof(sin)) < 0)
sin.sin_port = htons(atoi(argv[2])); {
din.sin_port = htons(atoi(argv[4])); perror("sendto() error\n");
// IP addresses exit(-1);
sin.sin_addr.s_addr = inet_addr(argv[1]); }
din.sin_addr.s_addr = inet_addr(argv[3]); else
// Fabricate the IP header or we can use the {
// standard header structures but assign our own printf("Sendto() is OK\n");
values. sleep(1);
ip->iph_ihl = 5; }
ip->iph_ver = 4; close(sd);
ip->iph_tos = 16; // Low delay return 0;
ip->iph_len = sizeof(struct ipheader) + }
sizeof(struct udpheader);
ip->iph_ident = htons(54321);
ip->iph_ttl = 64; // hops OUTPUT:
ip->iph_protocol = 17; // UDP
// Source IP address, can use spoofed address
here!!!
ip->iph_sourceip = inet_addr(argv[1]);
// The destination IP address
ip->iph_destip = inet_addr(argv[3]);
// Fabricate the UDP header. Source port number,
redundant

udp->udph_srcport = htons(atoi(argv[2]));
Ex.No:6 Program to implement RPC using System.out.println("Exception:",e);
Java }
Program : AddClient.java }
import java.rmi.*; }
public class AddClient
{ Program: AddServerImpl.java
public static void main(String args[]) import java.rmi.*;
{
try import java.rmi.server.*;
{
String addServerURL="rmi://" + args[0] + public class AddServerImpl extends
"/AddServer"; UnicastRemoteObject implements AddServerIntf
AddServerIntf {
addServerIntf=(AddServerIntf)Naming.lookup(ad public AddServerImpl() throws RemoteException
dServerURL); {
}
System.out.println("The First number is public double add(double d1, double d2) throws
"+args[1]); RemoteException
double d1= {
Double.valueOf(args[1]).doubleValue(); return d1+d2;
}
System.out.println("The Secondnumber is }
"+args[2]); Program: AddServerintf.java
import java.rmi.*;
double d2= public interface AddServerIntf extends Remote
Double.valueOf(args[2]).doubleValue(); {
double add(double d1, double e2) throws
System.out.println("The Sum is :"+ RemoteException;
addServerIntf.add(d1,d2)); }
}
catch(Exception e) Output:
{ Client Side
System.out.println(Exception "+e); C:\jdk1.5.0_05\bin> javac AddClient.java
} Server Side
} C:\jdk1.5.0_05\bin> javac AddServer.java
} C:\jdk1.5.0_05\bin> javac AddServerInf.java
Program: AddServer.java C:\jdk1.5.0_05\bin> javac AddServerImpl.java
import java.net.*;
import java.rmi.*; Step1: After Compling
public class AddServer Server Side
{ C:\jdk1.5.0_05\bin>start rmiregistry
public static void main(String args[]) C:\jdk1.5.0_05\bin>rmic AddServerImpl
{ C:\jdk1.5.0_05\bin>java AddServer
try Client Side
{ C:\jdk1.5.0_05\bin>java AddClient 127.0.0.1 5
AddServerImpl addServerImpl =new 5
AddServerImpl(); The First number is 5
The Second number is 5
Naming.rebind("AddServer", addServerImpl); The Sum is : 10
}
catch(Exception e)
{
}
}
Ex.No: 7 Program to implement Border
Gateway Protocol printf("the output matrix is: \n" );
#include<stdio.h>
void main() for(i=0;i<n;i++)
{ {
int n,i,j,k,a[10][10],b[10][10]; for(j=0;j<n;j++)
clrscr(); {
printf("enter the no of node"); printf("%d\t",b[i][j]);
scanf("%d",&n); }
for(i=0;i<n;i++) printf("\n");
{ }
for(j=0;j<n;j++) }
{
printf("Enter the distance between the OUTPUT:
host %d%d\t",i+1,j+1); [Staff@localhost cnlab]$ cc bgp.c
scanf("%d",&a[i][j]); [Staff@localhost cnlab]$ ./a.out
} Enter the number of nodes 4
} Enter the distance between the host 11 0
for(i=0;i<n;i++) Enter the distance between the host 12 7
{ Enter the distance between the host 13 2
for(j=0;j<n;j++) Enter the distance between the host 14 3
{ Enter the distance between the host 21 2
printf("%d \t",a[i][j]); Enter the distance between the host 22 0
} Enter the distance between the host 23 6
printf("\n"); Enter the distance between the host 24 8
} Enter the distance between the host 31 1
for(k=0;k<n;k++) Enter the distance between the host 32 3
{ Enter the distance between the host 33 0
for(i=0;i<n;i++) Enter the distance between the host 34 2
{ Enter the distance between the host 41 2
for(j=0;j<n;j++) Enter the distance between the host 42 2
{ Enter the distance between the host 43 2
if(a[i][j]>a[i][k] Enter the distance between the host 44 0
+a[k][j])
{ The cost between each nodes in Matric form:
a[i][j]=a[i][k] 0 7 2 3
+a[k][j]; 2 0 6 8
} 1 3 0 2
} 2 2 2 0
}
} The Shortest path Output matrix is:
for(i=0;i<n;i++) 0 5 2 3
{ 2 0 4 5
for(j=0;j<n;j++) 1 3 0 2
{ 2 2 2 0
b[i][j]=a[i][j];
if(i==j)
{
b[i][j]=0;
}
Ex.No:8 {
Program for simulation of Sliding Window
Protocol case 1:
#include<stdio.h>
selective();
#include<stdlib.h>
break;
#include<math.h>
case 2:
int n,r;
goback();
struct frame
break;
{
case 3:
char ack;
exit(0);
int data;
break;
}frm[10];
}
int sender(void);
}
void recvfrm(void);
while(c!=4);
void resend(void);
return 0;
void resend1(void);
}
void goback(void);
void goback()
void selective(void);
{
int main()
sender();
{
recvfrm();
int c;
resend1();
do
printf("\n all packets sent successfully");
{
}
printf("\n 1.Selective repeat ARP \n 2. Goback
ARQ\n 3.Exit"); void selective()

printf("Enter the choice"); {

scanf("%d",&c); sender();

switch(c) recvfrm();
resend(); printf("\n The packet no %d is no received \n",r);

printf("\n All packets sent succesfully"); }

} }

int sender() }

{ void resend()

int i; {

printf("Enter the number of packets to be sent"); printf("\n Sending packet %d",r);

scanf("%d",&n); sleep(2);

for(i=0;i<n;i++) frm[r].ack='y';

{ printf("\n The received packet is


%d",frm[r].data);
printf("Enter the data for the packets[%d]:",i);
}
scanf("%d",&frm[i].data);
void resend1()
frm[i].ack='y';
{
}
int i;
return 0;
printf("\n Resending from packet %d",r);
}
for(i=r;i<n;i++)
void recvfrm()
{
{
sleep(2);
int i;
frm[i].ack='y';
random();
printf("\n Recieved data of packet %d is %d", i,
r=rand()%n; frm[i].data);

frm[r].ack='n'; }

for(i=0;i<n;i++) }

if(frm[i].ack=='n')
OUTPUT:
{ [Staff@linux cn]$ cc sliding.c
Recieved data of packet 1 is 20
[Staff@linux cn]$ ./a.out
Recieved data of packet 2 is 30

1.Selective repeat ARP All packets sent successfully

2. Goback ARQ 1.Selective repeat ARP

3.ExitEnter the choice1 2. Goback ARQ

Enter the number of packets to be sent4 3.Exit


Enter the choice
Enter the data for the packets[0]:1 3

Enter the data for the packets[1]:2

Enter the data for the packets[2]:3

Enter the data for the packets[3]:4

The packet no 2 is no received

Sending packet 2

The received packet is 3

All packets sent succesfully

1.Selective repeat ARP


Ex.No:10 Program to implement Address
2. Goback ARQ Resolution Protocol
Client Side:
3.Exit #include<stdio.h>
Enter the choice2
#include<stdlib.h>
Enter the number of packets to be sent3
#include<netinet/in.h>
Enter the data for the packets[0]:10
#include<sys/socket.h>
Enter the data for the packets[1]:20
#include<netdb.h>
Enter the data for the packets[2]:30
#include<string.h>

main()
The packet no 1 is no received
{

Resending from packet 1 int sockfd,cid,n,i=0,j=0,x,m=0;


char c,mesg[100],mesg1[100],mac[100],ip[100]; m++;

struct sockaddr_in servaddr; printf("Enter the IP address");

for(i=0;i<100;i++) c=getchar();

{ do

mesg[i]=0; {

mesg1[i]=0; mesg[j]=c;

} j++;

sockfd=socket(AF_INET,SOCK_STREAM,0); c=getchar();

if(sockfd<0) }

printf("Error in socket"); while(c!='\n');

else send(sockfd,mesg,sizeof(mesg),0);

printf("Sockect created"); recv(sockfd,mesg1,100,0);

printf("\nMAC address from server");

servaddr.sin_family=AF_INET; puts(mesg1);

servaddr.sin_addr.s_addr=htons(INADDR_ANY) mesg[i]=0;
;
}
servaddr.sin_port=htons(6500);
while(m!=1);
if(cid=connect(sockfd,(struct
sockaddr*)&servaddr,sizeof(servaddr))<0) close(sockfd);

{ }

printf("\nError in comnnection");

}
Server Side:
else #include<sys/socket.h>

printf("Comnnection sucesses"); #include<netinet/in.h>

m=0; #include<stdio.h>

do #include<sys/types.h>

{ #include<string.h>
printf("Error in binding");
main()
else
{
printf("Binded Sucessfully");
int sockfd,aid,n,flag=0,i=0,j=0,x,m=0;
listen(sockfd,5);
char c,mesg[100],mesg1[100];
aid=accept(sockfd,(struct
char ip[3] sockaddr*)NULL,NULL);
[20]={"192.168.11.100","172.16.5.57","172.16.5.
10"}; if(aid<0)

char mac[3][20]={"12-C3-11-F3-32-E3","A1-12- printf("error in accept");


C3-A2-B4-C4","B4-C4-11-23-42-22"};
m=0;
struct sockaddr_in servaddr;
do
char buffer[256],b2[256],b3[256];
{
for(i=0;i<100;i++)
recv(aid,mesg,100,0);
{
printf("\nIP address from client");
mesg[i]=0;
puts(mesg);
mesg1[i]=0;
printf("MAC address is sending");
}
for(i=0;i<3;i++)
sockfd=socket(AF_INET,SOCK_STREAM,0);
{
if(sockfd<0)
x=strcmp(mesg,ip[i]);
printf("Error in socket");
if(x==0)
else
{
printf("Sockect created");
strcpy(mesg1,mac[i]);

break;
servaddr.sin_family=AF_INET;
}
servaddr.sin_addr.s_addr=htons(INADDR_ANY)
; else

servaddr.sin_port=htons(6500);
{
if(bind(sockfd,(struct
sockaddr*)&servaddr,sizeof(servaddr))<0) strcpy(mesg1,"Enter the correct IP address");
}

send(aid,mesg1,sizeof(mesg1),0);

printf("\n");
Ex.No. : 9 Implementation of UNICAST
for(i=0;i<100;i++) routing protocol
Program:
mesg[i]=0; set ns [new Simulator]
#Define different colors for data flows (for NAM)
} $ns color 1 Blue
$ns color 2 Red
while(m!=1); #Open the Trace file
set file1 [open unicast.tr w]
close(sockfd); $ns trace-all $file1
#Open the NAM trace file
} set file2 [open unicast.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure
OUTPUT: proc finish {} {
ClientSide: global ns file1 file2
[Staff@linux cnlab2010]$ cc arpclient.c $ns flush-trace
close $file1
[Staff@linux cnlab2010]$ ./a.out close $file2
exec nam unicast.nam &
Sockect createdComnnection sucessesEnter the IP exit 0
address : 192.168.11.100 }
# Next line should be commented out to have the
static routing
Server Side : $ns rtproto DV
[kujani@linux cnlab2010]$ cc arpserver.c #Create six nodes
set n0 [$ns node]
[kujani@linux cnlab2010]$ ./a.out set n1 [$ns node]
set n2 [$ns node]
Sockect createdBinded Sucessfully set n3 [$ns node]
set n4 [$ns node]
IP address from client: 192.168.11.100 set n5 [$ns node]
#Create links between the nodes
MAC address is sending $ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail
IP address from client: $ns duplex-link $n1 $n4 0.3Mb 10ms DropTail
192.168.11.100 $ns duplex-link $n3 $n5 0.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail
MAC address is sending #Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
MAC address from server12-C3-11-F3-32-E3 $ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4
$ns rtmodel-at 4.5 up $n1 $n4 Ex: No: 11 Study of UDP Performance
$ns at 0.1 "$ftp start" set ns [new Simulator]
$ns at 6.0 "finish" $ns color 0 blue
$ns run $ns color 1 red
$ns color 2 white
OUTPUT set n0 [$ns node]
csestaff@csestaff-desktop:~$ ns ex1.tcl set n1 [$ns node]
set n2 [$ns node]
Screen Shot set n3 [$ns node]
set f [open out.tr w]
$ns trace-all $f
set nf [open out3.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1
$udp1 set class_ 0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
set null1 [new Agent/Null]
$ns attach-agent $n1 $null1
$ns connect $udp0 $null0
$ns connect $udp1 $null1
$ns at 1.0 "$cbr0 start"
$ns at 1.1 "$cbr1 start"
puts [$cbr0 set packetSize_]
puts [$cbr0 set interval_]
$ns at 3.0 "finish"
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
puts "running nam..." $ns flush-trace
exec nam out2.nam & close $f
exit 0 close $nf
} puts "running nam..."
$ns run exec nam out.nam &
exit 0
OUTPUT: }
csestaff@csestaff-desktop:~$ ns ex14.tcl
$ns run
210 OUTPUT:
csestaff@csestaff-desktop:~$ ns ex15.tcl
0.0037499999999999999

running nam... Screen Shot

Ex.No: 12.Study of TCP Performance


set ns [new Simulator]
$ns color 0 blue
$ns color 1 red
$ns color 2 white
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set f [open out.tr w]
$ns trace-all $f
set nf [open out.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set tcp [new Agent/TCP]
$tcp set class_ 1
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"
$ns at 1.35 "$ns detach-agent $n1 $tcp ; $ns
detach-agent $n3 $sink"
$ns at 3.0 "finish"
proc finish {} {
global ns f nf

You might also like